How to loop the dialogue box in java script?

Viewed 31

I have been learning javascript for the past month and today we had loops. We have a task which I just can't solve properly and I simply can't see what i do wrong. Task is:

Make a program that displays a confirm dialog box with the text "Do you want to continue?" again and again until the user clicks the Cancel button in the dialog box. If the user clicks the OK button in the dialog box, then the program shows an alert dialog box with the text "Hello!". After the user has closed the alert dialog box the program shows the confirm dialog box again.

So far the best solution I could come up with is:

confirm("Do you want to continue?")
alert("Hello!")
while(confirm !== true){
    confirm;
}

Problem is that confirm box loops but loop goes to alert box no matter the button you press. And I just simply don't get how you make the loop begin again when pressing "ok" on alert box.

Thank you in advance for the advice!

1 Answers

The window.confirm function returns the result of the confirmation (true or false), but your code isn't capturing that result. You can capture it in a variable:

let confirmed = confirm("Do you want to continue?");

Also, take a look at what you're doing here:

while(confirm !== true)

We already know that confirm is a function. And we know that you append parentheses when you want to call a function. So this isn't calling the function, it's just referencing it. And the function itself will never equal true, so this loop is infinite (and meaningless).

Additionally, this doesn't do anything:

confirm;

Again, we know what confirm is already and we know how to call a function already. So this doesn't call the function, it's just a reference to the function. Sitting there. Not doing anything.


Instead, the loop can check the variable:

while (confirmed) {

And in the loop you can repeat the call to confirm from before the loop:

confirmed = confirm("Do you want to continue?");

Which leaves us with:

let confirmed = confirm("Do you want to continue?");
alert("Hello!");
while (confirmed) {
  confirmed = confirm("Do you want to continue?");
}

There's a lot of repetition here. This can be simplified. This also doesn't do what the assignment asks, because the alert operation isn't being repeated. Let's fix that logic before simplifying:

let confirmed = confirm("Do you want to continue?");
while (confirmed) {
  alert("Hello!");
  confirmed = confirm("Do you want to continue?");
}

We can remove the duplicate confirm operation by simply swapping the operations in the loop, which would make the one outside the loop superfluous:

let confirmed = true;
while (confirmed) {
  confirmed = confirm("Do you want to continue?");
  alert("Hello!");
}

There's also a lot of repetition of this confirmed variable, which we don't really need. The condition being checked by the loop doesn't need to be a variable, it just needs to evaluate to a boolean value. And the confirm function already returns that. So just use it directly:

while (confirm("Do you want to continue?")) {
  alert("Hello!");
}
Related