Problem with variables

Z

zombay

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>
 
J

Janwillem Borleffs

zombay said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top