Search code examples
javascriptarrayswhile-loop

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


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;
  }
}
 

Solution

  • 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;
      }
    }