0

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();
}
CRABOLO
  • 8,605
  • 39
  • 41
  • 68

5 Answers5

2

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.

Justin Wood
  • 9,941
  • 2
  • 33
  • 46
2

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!

ohiodoug
  • 1,493
  • 1
  • 9
  • 12
  • 1
    You need to look up how a switch/case statement works. This solution is doing EXACTLY what you need it to. After one of the cases evaluates to true, every statement after it will execute unless there is a break. Because there is no break in this statement, after 3 evaluates to true, every case will execute after it. – ohiodoug Dec 24 '13 at 22:02
1

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;
}
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1

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]);
}
linstantnoodles
  • 4,350
  • 1
  • 22
  • 24
  • This answer, while no answer to the original question, is by far the best because it teaches proper modelling. – Hugo Tunius Dec 24 '13 at 17:47
  • I'm not sure what you mean. If numberHands = 1, only list[0] will get called. – linstantnoodles Dec 25 '13 at 00:13
  • @linstantnoodles For a new programmer (as the OP seems to be), this might just be confusing and (IMHO) it really doesn't answer the question as asked! – drew_w Dec 26 '13 at 16:26
0

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();
}
José Gómez
  • 112
  • 7