1

I have a problem with the following exercise:

Enter N number of numbers by prompt. Show the average of the 1-digit numbers entered. End the program with the word “exit"/"salir”.

I also have to do the average calculation with a function. Here's what I have:

let i;
var sum = 0;
var avg = 0;

var average = function(i) {
    sum = sum + i;
    avg = sum/(i+1);
}

do {
    i = prompt("Ingrese un numero: ");
    if ((i < 10) && (i > -10)) {
        average(i);
    }
} while (i !== "salir");

alert(`el promedio de los numeros de un solo digito es: ${avg}`);

I tried limiting the program by stating that the average function will only be executed for one digit numbers, but the problem is that this is giving me the total average of all the numbers entered not the average of only the 1 digit numbers.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614

2 Answers2

2

You've succeeded in avoiding including multi-digit numbers from the sum and average. The problem isn't in that, it's here:

avg = sum/(i+1);

That's not how averages work. An average is the sum divided by how many values went into making that sum. (The number of numbers you added to sum.)

Keep track of how many numbers you added to sum, and use that count to calculate the average.


Side note: A couple of notes on the code, just for what they're worth:

  • Your code is currently relying on implicit conversion from string (what the user types in) to number (what you use with sum). Although it works in this example, I strongly recommend doing the conversion explicitly. My other answer here (also on SO) lists your various options for doing that.
  • You've used both let and var in your code. I suggest never using var, it has no place anymore in modern JavaScript. Use let if you need to let the variable's value change (as with sum), and const when you don't.
  • You're missing one ; (after the assignment statement assigning to average). (You can rely on Automatic Semicolon Insertion if you like [I don't recommend relying on it, but some others do], but whichever way you go, it's best to know the rules and then consistently do or don't include your ;.)
  • I suggest declaring your variables in the innermost scope you can declare them in. There's no reason for i to be global in your code, just make it local to the do-while loop body.
  • Obviously in a very simple exercise like this it's mostly fine, but I recommend not getting used to prompt and alert, they're relics from the 1990s and behave in very unusual (and sometimes problematic) ways compared to most functions in JavaScript.
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0
var i;
var sum = 0;
var avg = 0;

var average = function(num) {
    sum = sum + num;
    i+=1
    avg = sum/i;
}
var ans;
do {
    ans = prompt("Ingrese un numero: ");
    if ((ans < 10) && (ans > -10)) {
        average(ans);
    }
} while (ans !== "salir");

alert(`el promedio de los numeros de un solo digito es: ${avg}`);

Should remove this but will leave it here for any future visitors.

rowend
  • 35
  • 1
  • 4
  • That code uses `i` (but removes its declaration) for two completely different things. It won't correctly calculate an average, because `i` is overwritten by the code in the `do-while` loop. – T.J. Crowder Apr 13 '21 at 16:17
  • Separately, I don't recommend doing the work for someone when they are clearly asking a homework/assignment question. Just explain what they've done wrong and how they can do it right. See [*How do I ask and answer homework questions?*](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). – T.J. Crowder Apr 13 '21 at 16:18
  • I agree with mister Crowder, thanks both for the help. – Nicolás Zuleta Apr 13 '21 at 16:24