1

How do I make this if statement stop after the correct answer is chosen? Do I need to change it to if else, or is there something else I can try?

if

if (!arraysEqual(answerKey, answers)) {
    alert('Please Try Again');
    return false;
}

if (arraysEqual(answerKey, answers)) {
    $("#bin ol").append('<a href="index.html" name="modal"><img id="correct" src="images/visSelect/seq_correct.png"></a>');
    return false;
}
});

if else (updated)

if (arraysEqual(answerKey, answers)) {
    $("#bin ol").append('<a href="index.html" name="modal"><img id="correct" src="images/visSelect/seq_correct.png"></a>');
    return false;
}else{
    alert('Please Try Again');
    return false;
}
2
  • 1
    if..else looks better in your case for the case that you call arraysEquals twice. Commented Nov 9, 2012 at 15:30
  • Regarding some basics in programming with Javascript there is some good resources here:Mozilla Developer Network - JavaScript. Regarding if..else specific see this: JavaScript Refeence - if...else Commented Nov 9, 2012 at 15:47

1 Answer 1

2

if..else looks better considering the case that you would be calling arraysEquals twice if it returns true.

In the first case, the function arraysEquals will be called twice if it returns true..

but in the second case.. the function will be called only once in all cases and overall it looks more clear to me.

Sign up to request clarification or add additional context in comments.

4 Comments

I see what you mean. Does it look a bit better now? I took your advice and cleaned it up a bit.
@user1791449 You don't need an return false if that condition is the only code in that function.
How do I keep the else alert from running if the array is equal?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.