![]() |
|
javascript calculating average - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: javascript calculating average (/Thread-javascript-calculating-average) |
javascript calculating average - Vi-Sion - 02-05-2017 Well i have a text box and an ok button, what i want to do is add a note click ok add a note click ok (it's a loop that ends when i enter -1). when i enter -1 i want it to calculate the average of the notes. how can i do this in javascript ? RE: javascript calculating average - Hoss - 02-05-2017 So you want the script to take a number from the box each time and when you hit Submit with -1 in the input, it give you the average?: Code: var times = 0;
var current = 0;
$('#submit-button').on('click', function() {
var value = parseInt($('#input-field').val());
if (value == -1) {
alert(current / times);
} else {
times++;
current += value;
}
});Without jQuery: Code: var times = 0;
var current = 0;
document.getElementById('#submit-button').addEventListener('click', function() {
var value = parseInt(document.getElementById('#input-field').value);
if (value == -1) {
alert(current / times);
} else {
times++;
current += value;
}
});RE: javascript calculating average - Vi-Sion - 02-05-2017 this is what my code looks like Spoiler:Code: <html>
<head>
<script>
function moyenne(){
var somme,moyenne,count;
somme=0;moyenne=0;count=0;
if(document.getElementById("note").value!=-1){
somme+=document.getElementById("note").value;
count++;
}
else{
moyenne=somme/count;
alert(moyenne);
}}
</script>
</head>
<body>
<input type="text" id="note" />
<input type="button" value="ok" onclick="moyenne()"/>
</body>
</html>the way i'm doing it it doesn't stock values what do i need to do to fix it . RE: javascript calculating average - Hoss - 02-05-2017 (02-05-2017, 04:14 PM)Vi-Sion Wrote: this is what my code looks like Code: <html>
<head>
<script>
function moyenne(){
var somme,moyenne,count;
somme=0;moyenne=0;count=0;
if(parseInt(document.getElementById("note").value) != -1){
somme += parseInt(document.getElementById("note").value);
count++;
}
else{
moyenne=somme/count;
alert(moyenne);
}}
</script>
</head>
<body>
<input type="text" id="note" />
<input type="button" value="ok" onclick="moyenne()"/>
</body>
</html>RE: javascript calculating average - Vi-Sion - 02-05-2017 (02-05-2017, 04:18 PM)Hoss Wrote:(02-05-2017, 04:14 PM)Vi-Sion Wrote: this is what my code looks like No difference won't work as well . RE: javascript calculating average - Hoss - 02-05-2017 (02-05-2017, 04:33 PM)Vi-Sion Wrote:(02-05-2017, 04:18 PM)Hoss Wrote:(02-05-2017, 04:14 PM)Vi-Sion Wrote: this is what my code looks like Sorry, this example works. You had variables in the function. Code: <!DOCTYPE html>
<html>
<head>
<script>
document.addEventListener('DOMContentLoaded', function() {
var count = 0;
var times = 0;
document.getElementById('btn').addEventListener('click', function() {
var value = parseInt(document.getElementById('note').value);
if (value === -1) {
alert(count / times);
} else {
count += value;
times++;
}
});
});
</script>
</head>
<body>
<input type="text" id="note">
<input type="button" id="btn" value="ok">
</body>
</html>RE: javascript calculating average - Vi-Sion - 02-05-2017 @Hoss yep it works no,had no idea there's listeners in javascript. Thanks for the help much appreciated !! |