Nine Years of Service
Posts: 581
Threads: 41
javascript calculating average 02-05-2017, 01:21 PM
#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.
•
Nine Years of Service
Posts: 581
Threads: 41
RE: javascript calculating average 02-05-2017, 04:14 PM
#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.
•
Nine Years of Service
Posts: 581
Threads: 41
RE: javascript calculating average 02-05-2017, 04:33 PM
#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.
•
Nine Years of Service
Posts: 581
Threads: 41
RE: javascript calculating average 02-05-2017, 06:18 PM
#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.
•