JavaScript if / else if / else语句不起作用

时间:2017-11-30 17:34:37

标签: javascript if-statement

这是我的if / else if / else语句:

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");
}

一切都有效,除非我最后到达else语句。它仅适用于大于3XL衬衫的数字。但是,如果数字是来自6个类别的测量值的组合,则不会打印else语句。有谁知道我做错了什么?

4 个答案:

答案 0 :(得分:4)

从逻辑角度考虑这部分:

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

因此,如果shirtWidth与第一个条件匹配,则会转移到if的正文中。但如果shirtLengthshirtSleeve与那里的条件不符,那么您就什么也做不了。完全没有。因为else并未与它们相关联。它已连接到if(shirtWidth >= 18 && shirtWidth < 20)

将您的条件与&&结合使用,并且只使用if的单层:

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

话虽如此,除了语法之外,我怀疑代码的逻辑并不是你想要的。如果shirtWidth为19但shirtLength为30,该怎么办?你的案件都没有处理。

答案 1 :(得分:1)

T.J Crowder's answer是完美的(并且应该被接受),但我可能会建议将您的条件分开以便于阅读和管理:

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");
}

答案 2 :(得分:0)

您需要在每个return语句中添加console.log语句,或使用if运算符合并&&条件。

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");
}

这不是最好的方法,但这可以解决您的问题。

答案 3 :(得分:0)

这就是我最终做的事情:

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");
}