bling5630
bling5630

Reputation: 361

Try to call break in nested if\else statement

Here is the front of my code, and I like to embed a break statement after the "else" if tha answer is over than 18

setTimeout(function(){
confirm("I am ready to play!");
age= prompt("What's your age?");
    if(age<18){
        alert('You are allow to play!');
    }
    else{
    alert("No!"); 
    };

Upvotes: 0

Views: 1840

Answers (2)

L105
L105

Reputation: 5419

break or continue can only be used in loops. If you want to stop executing the code of a function just add a return;

setTimeout(function(){
if (!confirm("I am ready to play!")) return; // I suppose that's you really want.
age= prompt("What's your age?");
    if(age<18){
        alert('You are allow to play!');
    }
    else{
        alert("No!");
        return; // Stop the function.
    }
}, timer);

Upvotes: 2

Anobik
Anobik

Reputation: 4899

You need a return to exit from a function not a break. check for false if required after that or simply write return over there

setTimeout(function(){
confirm("I am ready to play!");
age= prompt("What's your age?");
    if(age<18){
        alert('You are allow to play!');
    }
    else{
return false;
    };

Upvotes: 0

Related Questions