Problem with variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zombay

    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>
  • Janwillem Borleffs

    #2
    Re: Problem with variables

    zombay wrote:[color=blue]
    > 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?
    >[/color]

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

    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



    Comment

    Working...