2

For some reason loops are just throwing me for a loop. I have issues creating and identifying the right variables to be increasing and what not.

Anyways, I have to use a loop to constantly prompt for a user to enter a number until they enter 0 and then adding all the inputs together.

So the base code I have for this is:

let numPrompt = +prompt("Enter a number");
if (isNaN(numPrompt)) {
  numPrompt = +prompt("Enter a number");
}
while (numPrompt != 0) {
  numPrompt = +prompt("Enter another number");

  console.log(numPrompt);
}

which the loops works to keep asking for the input till 0. However I'm having an issue thinking of a way to keep track of the inputs and then sum them all together into 1 value.

Andrei CACIO
  • 2,101
  • 15
  • 28
Donald L
  • 35
  • 5

4 Answers4

0
var input = prompt("Enter a number");
var min = parseInt(input);

while (input !== "0") {
input = parseInt(input);
if (input < min) {
    min = input;
}
input = prompt("Enter a number");
}
alert(min);

http://jsfiddle.net/3Jtxu/6/

Hamed Javaheri
  • 538
  • 3
  • 13
0

Good attempt! There are a couple of things I would like to point out though. When you invoke prompt(), anything the user enters will be of type string. Therefore when you validate with isNaN() it will always return true.

Next, since prompt() is returning strings, you need to convert that to an integer with parseInt() as you can see in the code below.

Finally, in your loop you are re-assigning a new value to numPrompt every iteration. You need to either change your equal sign to '+=' in order to increment numPrompt, or declare another variable (total) to hold the total of all the user's input, with the '+=' operator again, which is what is shown below.

Then we log the total outside the loop once it is complete.

let total = 0;
let numPrompt;

while (numPrompt != '0'){
  numPrompt = prompt("Enter another number")
  total += parseInt(numPrompt)

  // Log each input
  console.log(numPrompt)
} 

// Log the total
console.log(total)
MackoyokcaM
  • 157
  • 6
  • Thanks, that's for sure what i was forgetting, the +=, as well I think I'm okay on the integer because I have it as +prompt? Am I right on that command or would user Number or parseInt be better? – Donald L Sep 27 '17 at 04:45
  • I am acutally not sure, I never see the plus symbol used much so I had to do some digging. https://stackoverflow.com/questions/17106681/parseint-vs-unary-plus-when-to-use-which has some good info on this. – MackoyokcaM Sep 27 '17 at 05:05
  • Using `+expression` turns `expression` into an integer. That's an obscure method I would rather avoid in favor of using `parseInt(expression)`. – ichigolas Sep 27 '17 at 05:13
0

You can write a version like:

n=0;
while (n<10) {
     n+=parseInt(prompt('enter a number'));
}

which continues until n>=10.

If you want to terminate if 0 is inputed, you need an extra variable to store the entered value:

n=0;
pr=-1;
while (pr!=0) {
     pr=parseInt(prompt('enter a number'));
     n+=pr;
}
JMP
  • 4,417
  • 17
  • 30
  • 41
0
let inputArray = new Array();
let sum = 0;
let numPrompt;

do{
    numPrompt = prompt( "Enter a number" );
  var value = parseInt( numPrompt );

  if( Number.isInteger( value ) ){
    inputArray.push( value );
    sum += value;
    console.log( "Number: " + value );
  }
}
while( numPrompt != '0' );

console.log( sum );

In case a user enters a character instead of a integer, it needs to be handled.

The inputArray is used to keep a track of all the user inputs( you can skip it, if you don't need to save them)

http://jsfiddle.net/3Jtxu/25/

moonwalker7
  • 1,122
  • 3
  • 11
  • 29