Question

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();
}
Was it helpful?

Solution

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.

OTHER TIPS

Give this a try...

function recap() {
    switch(numberHands) {
        case 3:
            wonOrLost(h3.cards, h3);
        case 2:
            wonOrLost(h2.cards, h2);
        case 1:
            wonOrLost(h1.cards, h1);
    }
    playAgainOption();
}

It looks like the order these functions execute doesn't matter as long as they all get called. To me, this solutions feels more elegant.

Good luck!

Well, it isn't nested, but it will still work the same way. That's because

  • If numberHands > 1 then it's by definition > 0 as well.
  • If numberHands > 2 then it's by definition > 1 and > 0 as well.

The proper syntax of a nested if statements would be

if (condition) {
    doSomething();
    if (anotherCondition) {
        doSomethingElse();
        if (aThirdCondition) {
            doSomethingDifferent();
        }
    }
}

In your case, you have several, separate if statements, which are not related to one another, aside for the fact that if one is true, all others behind it are true as well.


If you did not intend for all of them to run if numberHands is equal to 3 , then a switch/case structure is more suitable, and more readable: OP clarified that he did intend for all of them to run.

switch (numberHands) {
    case 1:
        wonOrLost(h1.cards, h1);
        break;
    case 2:
        wonOrLost(h2.cards, h2);
        break;
    case 3:
        wonOrLost(h3.cards, h3);
        break;
}

This isn't a nested if statement, but it's certainly an alternative if you plan on adding more conditions.

var list = [h1,h2,h3];
for (var i = 0; i < numberHands; i++) {
    wonOrLost(list[i].cards, list[i]);
}

I don't know if you're not used to write with the markdown editor, but that's incorrectly indented.

function recap() {
    if (numberHands > 0) { // 1 or more
        wonOrLost(h1.cards, h1);
        if (numberHands > 1) {
            wonOrLost(h2.cards, h2); // 2 or more
        }
        if (numberHands > 2) {
            wonOrLost(h3.cards, h3); // 3 or more
        }
    }
    playAgainOption();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top