最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript find average from user's input - Stack Overflow

programmeradmin2浏览0评论

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);

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);
Share Improve this question asked Nov 13, 2014 at 11:10 PizzamanPizzaman 4255 gold badges11 silver badges19 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 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.

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);
    }

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);

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);

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);
发布评论

评论列表(0)

  1. 暂无评论