Videos
You are overriding your "total" variable in each interval with double the grade value.
var grade=parseInt(g);
var total=grade+grade;
should be changed to
var grade=parseInt(g);
total=total+grade;
Also, you need to initialize the "total" variable in the beginning of your code. See demo code: http://jsfiddle.net/56ouvan3/1/
I would also recommend some input validation (such as checking that the number of grades requested to average are greater than 0, all grades are positive, etc.)
I think you wanted to accomplish something like that:
http://jsfiddle.net/3L8dL228/1/
Just replace the console.log with your own document.write.
Now, despite I totally hate using prompts and I'm not very used to them, here are you what I think you're missing in your script:
- CONTROL: your "n" and "g" variables HAS to be an integers, so force the user to insert an integer.
- Variables declaration: you're declaring
totalevery single time you loop, therefore you're not storing anything at all.
To fix these, the very first piece of your code becomes this:
var n = prompt("Input a number: ", "Number here");
while (!parseInt(n) )
{
n=prompt("Input a number: ", "Number here");
}
In this way, you're asking the user to give you a NUMBER, but the script won't procede until it will effectively be able to parse an integer value.
Therefore, inputs like "hey", "hello", "foo", "bar", "baz" won't be accepted.
The second part of your code then becomes this one:
var i=1;
var total = 0;
do
{
var g = prompt("Input grade: " );
while (!parseInt(g)) {
g = prompt("Input grade: " );
}
var grade = parseInt(g);
total += grade;
i++;
}
while(i<=n);
var average=(total)/n;
console.log("Average is: " +average);
and console.log needs to be document.write in your case, but for testing purposes and because jsfiddle (of course) doesn't allow document.write you need to check your console to see the correct value.
What changes from your script to this one is that we are declaring total as a global variable, not as a local variable inside the do loop that will be reset each time you loop.
Next, we're using the same logic as the first prompt for the second one, because you want, again, an integer value, not a possible string like "hey".
After that, we're ADDING that value to the total variable, by not redeclaring it.
Finally, after the loop, we're dividing that global variable total by the global variable n, getting the average.
Prompt always returns a string, you could try to convert it to a number and then check to see if it NaN's on you which means it's a string. It would be better to use a regexp here though. You may also want to return the name if you are logging it.
function EnterName() {
var name = prompt("Enter your name here:");
var isNum = !isNaN(Number(name));
if (!isNum) {
alert("thank you " + name);
} else {
alert("you didn't enter a valid name");
}
return name;
}
console.log(EnterName());
You always enter strings into prompts. SO you can try to make it a int below and if it succeeds then give an error message otherwise say hello.
function EnterName() {
var name = prompt("Enter your name here:");
if(parseInt(name)) {
alert("you didn't enter a valid name");
}
else if (!parseInt(name)) {
alert("thank you " + name);
}
}