470,815 Members | 1,740 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Problem with variables

I need a script which by dialog box takes first the number of the
marks of a student and then calculate what is the avarege mark. I
write such a script (The one which is above) but instead of treating
the result of the prompt as numerical it treats them like string.
Where is my mistake?

<script>
MarkN = 0;
MarkC = 0;
Total = '';

MarkN = prompt ("What is the count of Your marks?",0);
i = 0;
while (i<MarkN) {
MarkC = prompt ("What is your mark?",0);
Total = MarkC + Total;
i++;
}
MarkC = Total/MarkN;

</script>
Jul 20 '05 #1
1 955
zombay wrote:
I need a script which by dialog box takes first the number of the
marks of a student and then calculate what is the avarege mark. I
write such a script (The one which is above) but instead of treating
the result of the prompt as numerical it treats them like string.
Where is my mistake?

That's because prompt returns a string, which should be converted to a
number before you can use it.
MarkN = prompt ("What is the count of Your marks?",0);


Change this into:
MarkN = +prompt ("What is the count of Your marks?",0);

The `+` in front of prompt converts the string into a number. You should
also implement an additional check for the received input, because you will
now get a devision by zero error when 0 is entered.
JW

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by G-Factor | last post: by
6 posts views Thread by Scott Zabolotzky | last post: by
78 posts views Thread by Josiah Manson | last post: by
3 posts views Thread by stclaus | last post: by
9 posts views Thread by Jerim79 | last post: by
5 posts views Thread by tshad | last post: by
6 posts views Thread by efrenba | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.