Login Register






javascript calculating average filter_list
Author
Message
javascript calculating average #1
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 ?


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: javascript calculating average #2
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; } });
(This post was last modified: 02-05-2017, 04:20 PM by Hoss.)
this forum is dead

Reply

RE: javascript calculating average #3
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 .


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: javascript calculating average #4
(02-05-2017, 04:14 PM)Vi-Sion Wrote: 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 .

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>
this forum is dead

Reply

RE: javascript calculating average #5
(02-05-2017, 04:18 PM)Hoss Wrote:
(02-05-2017, 04:14 PM)Vi-Sion Wrote: 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 .

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>

No difference won't work as well .


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: javascript calculating average #6
(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
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 .

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>

No difference won't work as well .

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>
this forum is dead

Reply

RE: javascript calculating average #7
@Hoss yep it works no,had no idea there's listeners in javascript.
Thanks for the help much appreciated !!


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply