2

I know there are similar questions on here to mine but I don't see the answer there.

Where am I going wrong with this JS code to find the average number from the user's input? I want to keep entering numbers until -1 is entered. I think -1 is being counted as an input/

    var count = 0;
    var input;
    var sum = 0;

        while(input != -1){
            input = parseInt(prompt("Enter a number"));
            count++;
            sum = sum + input;
            sum = parseInt(sum);
            average = parseFloat(sum/count);
        }

    alert("Average number is " + average);
0

5 Answers 5

4

This is the right order (without all the unnecessary parsing...)

var count = 0;
var input;
var sum = 0;
input = parseInt(prompt("Enter a number"));
while (input != -1) {

    count++;
    sum += input;
    average = sum / count;
    input = parseInt(prompt("Enter a number"));
}

alert("Average number is " + average);    

DEMO

Note that you can calculate the average once outside of the loop and save some CPU.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to check after you take the input from the user.

while(input != -1){
        input = parseInt(prompt("Enter a number")); 
       //The user enters -1 but still inside the while loop.
        if(input != -1)
           {
           count++;
           sum = sum + input;
           }
        sum = parseInt(sum);
        average = parseFloat(sum/count);
    }

Comments

1

Here is the function code you need.

var count = 0;
var input;
var sum = 0;

    while(true){
        input = parseInt(prompt("Enter a number"));

        if(input != -1)
        {
            count++;
            sum = sum + input;
            sum = parseInt(sum);
        }
        else
            break;
    }
average = parseFloat(sum/count);
alert("Average number is " + average);

Comments

0

This will grab the average...Simply accumulate the values and then divide the total 'sum' by the number of 'inputs made'(in our case we just use the count++)

var count = 0;
var input;
var sum = 0;

    while(input != -1){
        count++;
        input = prompt("Enter a number");
        sum += input;
        sum = parseInt(sum);
    }
        average = (sum/count);
alert("Average number is " + average);

Comments

0

The best and most understandable code for you

     let number= 0;
      let sum = 0;
      let counter = 0;



         while (true) {
            number= +prompt( "Enter as many numbers as you want and then press 
            cancel to get their average ");
            sum += number;
            if (number === 0) {
            break;
           }
            counter++;
        }
 


alert("Average : " + sum / counter);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.