javascript dynamic arrays 1

javascript dynamic arrays

var input = []; // initialise an empty array
var temp = '';
do {
    temp = prompt("Enter a number. Press cancel or leave empty to finish.");
    if (temp === "" || temp === null) {
        break;
    } else {
        input.push(temp);  // the array will dynamically grow
    }
} while (1);

Here is what the above code is Doing:
1. We initialise an empty array called input.
2. We create a do-while loop.
3. We prompt the user to enter a number.
4. If the user presses cancel or leaves the input empty, we break out of the loop.
5. If the user enters a number, we push it into the array.
6. We repeat the loop until the user presses cancel or leaves the input empty.

Similar Posts