-1

This is my if/else if/ else statement:

var shirtWidth = 18;
var shirtLength = 33;
var shirtSleeve = 8.63;

// Small Shirts
if(shirtWidth >= 18 && shirtWidth < 20) {
        if(shirtLength >= 28 && shirtLength < 29)
        if(shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
            console.log("S");
        }
}
// Medium Shirts
else if(shirtWidth >= 20 && shirtWidth < 22) {
        if(shirtLength >= 29 && shirtLength < 30)
        if(shirtSleeve >= 8.38 && shirtSleeve < 8.63) {
            console.log("M");
        }
}
// Large Shirts
else if(shirtWidth >= 22 && shirtWidth < 24) {
        if(shirtLength >= 30 && shirtLength < 31)
        if(shirtSleeve >= 8.63 && shirtSleeve < 8.88) {
            console.log("L");
        }
}
// XL Shirts
else if(shirtWidth >= 24 && shirtWidth < 26) {
        if(shirtLength >= 31 && shirtLength < 33)
        if(shirtSleeve >= 8.88 && shirtSleeve < 9.63) {
            console.log("XL");
        }
}
// 2XL Shirts
else if(shirtWidth >= 26 && shirtWidth < 28) {
        if(shirtLength >= 33 && shirtLength < 34)
        if(shirtSleeve >= 9.63 && shirtSleeve < 10.13) {
            console.log("2XL");
        }
}
// 3XL Shirts
else if(shirtWidth === 28) {
        if(shirtLength === 34)
        if(shirtSleeve === 10.13) {
            console.log("3XL");
        }
}
// Does not match any shirt sizes
else {
    console.log("N/A");
}

Everything works except when I get to the else statement at the end. It only works for numbers greater than the 3XL shirts. However, if the numbers are combination of measurements from the 6 categories, the else statement does not print. Does anyone know what I am doing wrong?

8
  • This should be a switch to make it so much simpler. Commented Nov 30, 2017 at 17:35
  • 1
    And I'd strongly recommend not using actively misleading indentation on those inner stacked ifs. (And always using braces, rather than leaving them out sometimes.) No reason for stacking those inner ifs, either; us &&. Commented Nov 30, 2017 at 17:36
  • 1
    "This is my if/else if/ else statement" - There are many conditional statements in this code. What specifically isn't working? "The else statement at the end" - else doesn't check a condition, it executes when no previous condition was met. What condition did you expect to be met? What were the values of the variables at that time? Why did you expect the condition to be true with those values? Commented Nov 30, 2017 at 17:37
  • 1
    @ScottMarcus: You could do this with switch, but I doubt it would be simpler. :-) Commented Nov 30, 2017 at 17:41
  • 1
    @ScottMarcus: You're assuming discrete values for shirtWidth. Also, from the OP's question, it would appear that shirtWidth isn't the only criterion, that one has to consider multiple criteria. (That said, I suspect the logic is flawed.) Commented Nov 30, 2017 at 17:47

4 Answers 4

4

Consider this part from a logic perspective:

// Small Shirts
if(shirtWidth >= 18 && shirtWidth < 20) {
        if(shirtLength >= 28 && shirtLength < 29)
        if(shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
            console.log("S");
        }
}
else // ...

So if shirtWidth matches that first condition, you branch into the body of the if. But if shirtLength or shirtSleeve don't match the conditions there, you don't do anything. At all. Because the else isn't connected to them. It's connected to the if(shirtWidth >= 18 && shirtWidth < 20).

Combine your conditions with && and use only a single layer of ifs:

// Small Shirts
if (shirtWidth >= 18 && shirtWidth < 20 &&
    shirtLength >= 28 && shirtLength < 29 &&
    shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
    console.log("S");
}
// Medium Shirts
else // ...

Having said that, aside from syntax, I suspect the logic of the code isn't quite what you want. What if a shirtWidth is 19 but shirtLength is 30? None of your cases handles that.

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

Comments

1

T.J Crowder's answer is perfect (and should be accepted), but I might suggest separating your conditions to make it easier to read and manage:

var shirtWidth = 18;
var shirtLength = 33;
var shirtSleeve = 8.63;

var isSmall = shirtWidth >= 18 && shirtWidth < 20 && shirtLength >= 28 && shirtLength < 29 && shirtSleeve >= 8.13 && shirtSleeve < 8.38;
var isMedium = shirtWidth >= 20 && shirtWidth < 22 && shirtLength >= 29 && shirtLength < 30 && shirtSleeve >= 8.38 && shirtSleeve < 8.63;
var isLarge = shirtWidth >= 22 && shirtWidth < 24 && shirtLength >= 30 && shirtLength < 31 && shirtSleeve >= 8.63 && shirtSleeve < 8.88;
var isXL = shirtWidth >= 22 && shirtWidth < 24 && shirtLength >= 31 && shirtLength < 33 && shirtSleeve >= 8.88 && shirtSleeve < 9.63;
var isXXL = shirtWidth >= 26 && shirtWidth < 28 && shirtLength >= 33 && shirtLength < 34 && shirtSleeve >= 9.63 && shirtSleeve < 10.13;
var is3XL = shirtWidth === 28 && shirtLength === 34 && shirtSleeve === 10.13;

if(isSmall) {
  console.log("S");
} else if(isMedium) {
  console.log("M");
} else if(isLarge) {
  console.log("L");
} else if(isXL) {
  console.log("XL");
} else if(isXXL) {
  console.log("2XL");
} else if(is3XL) {
  console.log("3XL");
} else {// Does not match any shirt sizes
    console.log("N/A");
}

1 Comment

Makes it clearer, although it does a fair bit of extra work. (Not that I imagine this is in a tight loop running hundreds of thousands of times...) I think I'd use isSmall, isMedium, etc. functions.
0

You will need to add a return statement at each console.log statement or combine the if conditions using the && operator.

var shirtWidth = 18;
var shirtLength = 33;
var shirtSleeve = 8.63;

// Small Shirts
if (shirtWidth >= 18 &&
    shirtWidth < 20 &&
    shirtLength >= 28 &&
    shirtLength < 29 &&
    shirtSleeve >= 8.13 &&
    shirtSleeve < 8.38) {
    console.log("S");
}
// Medium Shirts
else if (shirtWidth >= 20 &&
    shirtWidth < 22 &&
    shirtLength >= 29 &&
    shirtLength < 30 &&
    shirtSleeve >= 8.38 &&
    shirtSleeve < 8.63) {
    console.log("M");
}
// Large Shirts
else if (shirtWidth >= 22 &&
    shirtWidth < 24 &&
    shirtLength >= 30 &&
    shirtLength < 31 &&
    shirtSleeve >= 8.63 &&
    shirtSleeve < 8.88) {
    console.log("L");
}
// XL Shirts
else if (shirtWidth >= 24 &&
    shirtWidth < 26 &&
    shirtLength >= 31 &&
    shirtLength < 33 &&
    shirtSleeve >= 8.88 && shirtSleeve < 9.63) {
    console.log("XL");
}
// 2XL Shirts
else if (shirtWidth >= 26 &&
    shirtWidth < 28 &&
    shirtLength >= 33 &&
    shirtLength < 34 &&
    shirtSleeve >= 9.63 &&
    shirtSleeve < 10.13) {
    console.log("2XL");
}
// 3XL Shirts
else if (shirtWidth === 28 &&
    shirtLength === 34 &&
    shirtSleeve === 10.13) {
    console.log("3XL");

}
// Does not match any shirt sizes
else {
    console.log("N/A");
}

Not the best possible way to do this, but this should solve your problem.

Comments

0

This is what I ended up doing:

var shirtWidth = 24;
var shirtLength = 28;
var shirtSleeve = 10;

// Small Shirts
if(shirtWidth >= 18 && shirtWidth < 20 &&
    shirtLength >= 28 && shirtLength < 29 &&
    shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
        console.log("S");
    }
// Medium Shirts
else if(shirtWidth >= 20 && shirtWidth < 22 &&
    shirtLength >= 29 && shirtLength < 30 &&
    shirtSleeve >= 8.38 && shirtSleeve < 8.63) {
        console.log("M");
    }
// Large Shirts
else if(shirtWidth >= 22 && shirtWidth < 24 &&
    shirtLength >= 30 && shirtLength < 31 &&
    shirtSleeve >= 8.63 && shirtSleeve < 8.88) {
        console.log("L");
    }
// XL Shirts
else if(shirtWidth >= 24 && shirtWidth < 26 &&
    shirtLength >= 31 && shirtLength < 33 &&
    shirtSleeve >= 8.88 && shirtSleeve < 9.63) {
        console.log("XL");
    }
// 2XL Shirts
else if(shirtWidth >= 26 && shirtWidth < 28 &&
    shirtLength >= 33 && shirtLength < 34 &&
    shirtSleeve >= 9.63 && shirtSleeve < 10.13) {
        console.log("2XL");
    }
// 3XL Shirts
else if(shirtWidth === 28 &&
    shirtLength === 34 &&
    shirtSleeve === 10.13) {
        console.log("3XL");
    }
// Does not match any shirt sizes
else {
    console.log("N/A");
}

Comments

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.