I've tried using do-while loops but it doesn't seem to be working properly:
let rep; //this variable tells the loop whether to run or not
let nOfTimesTheLoopRuns = 0;
do {
nOfTimesTheLoopRuns++;
console.log(`This loop has run ${nOfTimesTheLoopRuns} time(s).`);
setTimeout( () => {
rep = confirm("Repeat?");
}, 2000); //a delay is set so that the answer can be printed on the console before the code runs again
} while (rep);
The console prints: "This loop has run 1 time(s).", but it doesn't repeat as it should when I press "Ok" in the confirm(); dialog box.
I've also tried this:
let rep = []; //this variable tells the loop whether to run or not
let nOfTimesTheLoopRuns = 0;
do {
rep.pop();
nOfTimesTheLoopRuns++;
console.log(`This loop has run ${nOfTimesTheLoopRuns} time(s).`);
setTimeout( () => {
rep.push(confirm("Repeat?"));
}, 2000); //a delay is set so that the answer can be printed on the console before the code runs again
} while (rep[0]);
In the end, the console prints "This loop has run 1 time(s)." And the value of nOfTimesTheLoopRuns is 1. How can I make it so that it keeps running every time the user presses "Ok" in the confirm(); dialog box?