Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  31] [ 6]  / answers: 1 / hits: 11244  / 11 Years ago, tue, december 24, 2013, 12:00:00

numberHands will be equal to 1,2, or 3. It will never make it this far in the program if not. So I see no reason for else statements.



But, is this the correct syntax for writing nested if statements in JavaScript? I feel like the closing brackets are incorrect, but I'm new to js. How would you write this?



function recap() {
if (numberHands > 0) {
wonOrLost(h1.cards, h1);
}
if (numberHands > 1) {
wonOrLost(h2.cards, h2);
}
if (numberHands > 2) {
wonOrLost(h3.cards, h3);
}
playAgainOption();
}

More From » nested-if

 Answers
5

You are right, the closing brackets are in the wrong place. What you are looking for is



function recap() {
if (numberHands > 0) {
wonOrLost(h1.cards, h1);
if (numberHands > 1) {
wonOrLost(h2.cards, h2);
if (numberHands > 2) {
wonOrLost(h3.cards, h3);
}
}
}
playAgainOption();
}


NOTE



This is functionally identical to what you currently have.


[#49245] Monday, December 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bobbyw

Total Points: 456
Total Questions: 102
Total Answers: 113

Location: Bahrain
Member since Fri, Sep 16, 2022
2 Years ago
;