Postby Warrior4Christ » Sun May 07, 2006 11:12 pm
Javascript is a bad language IMHO, because variable can take any type of data and even turn from one data type to another, as you have just demonstrated.
The prompt function returns a *string* entered by the user, which you have stored in num. So num stores the two digits, say '8' and '2' instead of 82. Then the "+" operator appends two (three!) strings together rather than performing addition. So you must convert num from string to a number. To do that, you use:
num = parseInt(prompt("Please enter a number:"));
But also, if you are intending to display the sum of three numbers entered by the user, it wouldn't work as expected. It would just display 3 times the last number entered. What you want to do is add num onto what is already stored in total. So:
total = total + num;
And unless the top initialisation list initialises all three variables to 0, total will need to be initialised there too.
Also, it's a bit amiguous to say "count < 4" for the loop condition when you want it to go up to 3. So I prefer to say "count <= 3" if I want to count up to 3.
Everywhere like such as, and
MOES.
"Expect great things from God; attempt great things for God." - William Carey