0
good night!
I need to apply a loop for the user to always enter a number (via prompt), when typing 0 should appear an Alert stating the amount of numbers typed, not counting the zero.
I don’t understand what I’m doing wrong, because he is presenting an Alert with all the numbers typed, including zero.
Does anyone know how I can count just the amount of numbers excluding zero?
Thank you.
function TESTE2() {
var number = prompt("Enter a number, 0 to stop: ");
var result = 0;
while (number !== 0){
result = result+number;
number = +prompt("Enter another, chose 0 to stop");
}
while (number == 0){
alert("You entered "+ result+ " non-zero numbers!");
break;
}
}
You want to add the numbers together like this (https://jsfiddle.net/r4dbybLd/) or you want text with all the numbers?
– Sergio
Almost that, only I don’t want to add up the numbers typed, I want to add up the amount of numbers typed. For example, if you type 10, 7, 8 and 0 I don’t want to add 10 + 7 + 8, I want to add and show that I typed 3 numbers before 0
– Anderson Trugilio
Can you explain the context in which you need this feature? other than Alert where you will use what the function creates?
– Sergio
From what I understand your problem is happening because vc digital 0 at the first prompt and still enters the loop. there are several ways to solve this, one of them is by removing the second = of !== leaving so != this happening because in the programming languages the third comparator serves to compare also the type of the variable, in your example the variable number actually is not the type number, because the prompt returns a string. But on your exe
– Robson Gomes