How would you create a basic loop using User input that breaks when the user enters a word to end the program?

41 Views Asked by At

Im looking to create an empty array that is updated forever via a loop until the user enters "stop". I really cannot figure out how to do it.

var array = [];

while (true) {
  array = prompt("Say something?");
  if (array) == "stop" {
    break;
  }
}
 
2

There are 2 best solutions below

1
flyingfox On BEST ANSWER

You need to change var array = []; to var array;,make sure the variable array is not a array

var array;

while (true) {
  array = prompt("Say something?");
  if(array == "stop") {
    break;
  }
}

0
mr.loop On
  1. Your syntax for if is incorrect
  2. You are not updating the array instead overwriting it with the latest prompt
const array = [];

while (true) {
  const val = prompt("Say something?");
  if (val == "stop") {
    break;
  }
  array.push(val);
}