...

View Full Version : simple code problem - adding variables acquired from prompt()



corpus
05-07-2006, 06:03 AM
I need help with this piece of code please:



for(count=1; count<4; count++)
{
number=prompt("Enter a number please");


total=(number+number+number);
}

document.write(total);

My problem lies in the line with total=.
I can't figure out how to add all the results aquired from number's prompt function. Can someone please help?

JUD
05-07-2006, 06:10 AM
Try this



var i, number, total = 0;

for(i = 1; i < 4; i++){
number = prompt("Please enter a number", "");

total += number;
}

document.write(total);

corpus
05-07-2006, 11:17 PM
Thanks, but it doesn't work...

Can anyone else help please?

JUD
05-09-2006, 11:09 AM
Sorry I missed something out



var i, number, total = 0;

for(i = 1; i < 4; i++){
number = prompt("Please enter a number", "");

total += parseInt(number);
}

document.write(total);

Kor
05-09-2006, 11:48 AM
parseInt(number,10)

or even better

Number(number)

corpus
05-09-2006, 10:03 PM
Thank you!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum