Closed Thread Icon

Topic awaiting preservation: How to add up prompt value??? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8706" title="Pages that link to Topic awaiting preservation: How to add up prompt value??? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: How to add up prompt value??? <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-09-2003 06:21

Hi, guys. How are you doing? My body is very stiff because of tennis. Never mind. It was good exercise.
BTW, may I ask you something???
My mission is:

quote:
write a prgram that asks you how many numbers you have, asks you to enter them and works out the total of the numbers entered.



Here is my code:

code:
<html>
<head>
<script type="text/javascript">
function cal(){
var Num=parseInt(document.theForm.num.value);
var sum=parseInt(document.theForm.result.value);
for(i=1; i<=Num; i++){
ans=parseInt(prompt("Enter the number" + i));
sum = sum + ans;
}

return document.theForm.result.value=sum;
}
</script>
</head>
<body>
<h3>Mission:</h3>
<p>Write a program that asks you how myny numbers you have,<br /> asks you to enter them and

works out the total and the biggest of the numbers entered. </p>

<form name="theForm">
<p>How many numbers?
<input type="text" name="num" onBlur="cal()">
</p>

<p>The total value is:
<input type="text" name="result">
</p>
<input type="reset" value="Reset">
</form>
</body>
</html>



Loop seems to work fine, but in the total number box, it said Nan.
I don't know how to add up prompt box value. Would you know something about it???
Here is my uploaded file. Many many thanks in advance. Cheers.

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-09-2003 17:34

This is because you initially assign

var sum=parseInt(document.theForm.result.value);

But document.theForm.result.value is "", since nothing has been entered in that text box. This causes sum to be NaN, because "" cannot be turned into a number.

Simply replace that line with:

var sum = 0;

And I think everything will work.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-09-2003 23:59

Hi, Slime. Good morning.
How are you today?
Many many thanks for yoru reply.
It is working fine now.
Great to solve problem, isn't it?
Have a good day.
Cya.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu