Javascript prompt and alert inputting a number and it will loop and you will input numbers to get the average of it

前端 未结 3 771
悲&欢浪女
悲&欢浪女 2021-01-24 17:07

I have below javascript code with loop but I can\'t get the average of it. I\'m not sure what\'s wrong with my code. It\'s like the first prompt you will input a number and it w

相关标签:
3条回答
  • 2021-01-24 17:26

    I think you wanted to accomplish something like that:

    http://jsfiddle.net/3L8dL228/1/

    Just replace the console.log with your own document.write.

    Now, despite I totally hate using prompts and I'm not very used to them, here are you what I think you're missing in your script:

    1. CONTROL: your "n" and "g" variables HAS to be an integers, so force the user to insert an integer.
    2. Variables declaration: you're declaring total every single time you loop, therefore you're not storing anything at all.

    To fix these, the very first piece of your code becomes this:

    var n = prompt("Input a number: ", "Number here");
    while (!parseInt(n) )
    {
     n=prompt("Input a number: ", "Number here");   
    }
    

    In this way, you're asking the user to give you a NUMBER, but the script won't procede until it will effectively be able to parse an integer value.

    Therefore, inputs like "hey", "hello", "foo", "bar", "baz" won't be accepted.

    The second part of your code then becomes this one:

    var i=1;
        var total = 0;
        do
        {
            var g = prompt("Input grade: " );
            while (!parseInt(g)) {
                g = prompt("Input grade: " );
            }
            var grade = parseInt(g);
            total += grade;
            i++;
    
        }
        while(i<=n);
        var average=(total)/n;
        console.log("Average is: " +average);
    

    and console.log needs to be document.write in your case, but for testing purposes and because jsfiddle (of course) doesn't allow document.write you need to check your console to see the correct value.

    What changes from your script to this one is that we are declaring total as a global variable, not as a local variable inside the do loop that will be reset each time you loop.

    Next, we're using the same logic as the first prompt for the second one, because you want, again, an integer value, not a possible string like "hey".

    After that, we're ADDING that value to the total variable, by not redeclaring it.

    Finally, after the loop, we're dividing that global variable total by the global variable n, getting the average.

    0 讨论(0)
  • 2021-01-24 17:44

    Try below code to get the average of the entered number.

    numGrades = prompt("Enter number of grades to be entered: ");
    
    //number of grades to be entered LOOP 
    for (index = 1; index <= numGrades; index++) {
      numberGrades = prompt("Enter Grade " + index);
    }
    
    //Calculation
    gradePointAverage = numberGrades / numGrades;
    document.write("Your GPA is " + gradePointAverage );
    
    0 讨论(0)
  • 2021-01-24 17:47

    You are overriding your "total" variable in each interval with double the grade value.

    var grade=parseInt(g);
     var total=grade+grade;
    

    should be changed to

    var grade=parseInt(g);
     total=total+grade;
    

    Also, you need to initialize the "total" variable in the beginning of your code. See demo code: http://jsfiddle.net/56ouvan3/1/

    I would also recommend some input validation (such as checking that the number of grades requested to average are greater than 0, all grades are positive, etc.)

    0 讨论(0)
提交回复
热议问题