skip to Main Content

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

Answers


  1. 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;
      }
    }
    Login or Signup to reply.
    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);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search