How do you enter an array indefinitely until you hit cancel?

  • 0
    I have an empty array and I need to enter values ​​into it until you click cancel.
    I tried through while (true) but it only output null
    Here is the code itself
    var arr = [];
    while(true)
      {
        arr=prompt("Введите значение");
        if(!arr) {
        alert(arr);
        break;
      }
      }
    JavaScript Anonymous, Jun 5, 2020

  • 2 Answers
  • 0
    prompt returns a string, and on cancellation null .. You can use recursion

    function start() {
    let res = prompt("Введите значение", "");
    if(res !== null) {
    alert(res);
    start();
    }
    }
    start();




    If I understood correctly about arrays, then something like this:

    let arr = [];

    function start() {
    let res = prompt("Введите значение", "");
    if (res !== null) {
    arr.push(res);
    alert(arr);
    return start();
    }
    arr.length = 0;
    }
    start();
    Anonymous

  • 0
    something like that?



    const arr = [];

    while(true) {
    let value = prompt("Введите значение");

    if(!value) {
    alert(arr);
    break;
    }

    arr.push(value);
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!