While loop - number counter minus 0

Asked

Viewed 666 times

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?

  • 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

  • Can you explain the context in which you need this feature? other than Alert where you will use what the function creates?

  • 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

3 answers

1

Just do it like this:

(function () {
    var number = prompt("Enter a number, 0 to stop: ");
    var quantity = 0;

    while (number != 0) {
        quantity++;
        number = Number(prompt("Enter another, chose 0 to stop"));
    }

    alert("You entered " + quantity + " non-zero numbers!");
}());

I hope I’ve helped!

  • It helped a lot, replacing !== 0 with != 0. I’m still trying to understand Javascript. If I wanted instead of adding up all the results minus 0, I wanted to separate the positive numbers from the negative ones, excluding 0, how would it look? For example, I typed 1, 2, -3 and 0. "You typed 2 positive and 1 negative numbers". I don’t know if I’m making this too complicated, but I’m trying to get my head around it.

  • It would look like this: (function () {&#xA; var number = prompt("Enter a number, 0 to stop: ");&#xA; var positiveQuantity = 0, negativeQuantity = 0;&#xA;&#xA; while (number !== 0) {&#xA; if (number < 0) {&#xA; negativeQuantity++;&#xA; } else {&#xA; positiveQuantity++;&#xA; }&#xA; &#xA; quantity++;&#xA; number = Number(prompt("Enter another, chose 0 to stop"));&#xA; }&#xA;&#xA; alert("You typed: " + quantity + " numbers, " + negativeQuantity + " negatives and " + positiveQuantity + " positives");&#xA;}());

1

you can use a array, do the push whenever the value is not 0 and, at the end, return the length of array:

var numbers = [];

function loopPrompt() {
  var input = prompt('Number? [0 escapes]');

  if (!input || parseInt(input,10) === 0) {
    alert(numbers.length + ' numbers were inserted: ' + numbers.toString());
    return;
  }

  numbers.push(input);
  loopPrompt();
}

loopPrompt();

1

I believe that in the example of hpedrorodrigues solves your problem however exchanging !== for != the reason I commented before

(function () {
    var number = prompt("Enter a number, 0 to stop: ");
    var quantity = 0;

    while (number != 0) {
        quantity++;
        number = Number(prompt("Enter another, chose 0 to stop"));
    }

    alert("You entered " + quantity + " non-zero numbers!");
}());

  • If I wanted instead of adding up all the results minus 0, I wanted to separate the positive numbers from the negative ones, excluding 0, how would it look? For example, I typed 1, 2, -3 and 0. "You typed 2 positive and 1 negative numbers". I don’t know if I’m making this too complicated, but I’m trying to get my head around it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.