为了使用特定尺寸进行限定,我创建了条件语句来验证尺寸范围。
var shirtWidth = 22; // size L (large)
var shirtLength = 30 ; // size L (large)
var shirtSleeve = 8.79; // size L (large)
if((shirtWidth === 18 || shirtWidth === 19) && (shirtLength === 28) && (shirtSleeve >= 8.13 && shirtSleeve <= 8.37)){
console.log('S');
} else if ((shirtWidth === 20 || shirtWidth === 21) && (shirtLength === 29) && (shirtSleeve >= 8.38 && shirtSleeve <= 8.62)){
console.log('M');
} else if ((shirtWidth === 22 || shirtWidth === 23) && (shirtLength === 30) && (shirtSleeve >= 8.63 && shirtSleeve <= 8.87)){
console.log('L');
} else if ((shirtWidth === 24 || shirtWidth === 25) && (shirtLength === 31 || shirtLength === 32) && (shirtSleeve >= 8.88 && shirtSleeve <= 9.62)){
console.log('XL');
} else if ((shirtWidth === 26 || shirtWidth === 27) && (shirtLength === 33) && (shirtSleeve >= 9.63 && shirtSleeve <= 10.12)){
console.log('2XL');
}else if ((shirtWidth === 28) && (shirtLength === 34) && (shirtSleeve >= 10.13)){
console.log('3XL');
}else {
console.log('N/A');
}
似乎这仍然是错误的。知道我在这里做错了什么吗?我在这里错过了逻辑表达式吗?
更新
答案 0 :(得分:1)
您的代码似乎在这里:
来自@Asad Saeeduddin的示例和数据中的但是我从an answer days earlier得到了一些线索。在该答案的第一个片段中,我使用DOM数据集来处理所有这些问题。但是,从数据集中检索数据时,它似乎不属于Number
类型(实际上是string
)。如果检索到的数据是存储在其他类型中的数字并且您使用严格测试(===
),则可能会出现问题。 (更多猜测,您应该检索网页中的数据,这应该将所有内容存储为string
和Node
。)
如果将要在if-else-if语句中确定的每个数字放入Number(/* ... */)
都不起作用,则应该包含一个最小代码来重现问题。 (请摘录)
答案 1 :(得分:1)
var rules = [
{ size: "S", width: 18, length: 28, sleeve: 8.13 },
{ size: "M", width: 20, length: 29, sleeve: 8.38 },
{ size: "L", width: 22, length: 30, sleeve: 8.63 },
{ size: "XL", width: 24, length: 31, sleeve: 8.88 },
{ size: "2XL", width: 26, length: 33, sleeve: 9.63 },
{ size: "3XL", width: 28, length: 34, sleeve: 10.33 }
]
var shirt = { width: 22, length: 31, sleeve: 8.79 };
var i, rule, final_rule_index = -1;
for (i = 0; i < rules.length; i++) {
rule = rules[i];
if (
final_rule_index < i &&
shirt.width >= rule.width &&
shirt.length >= rule.length &&
shirt.sleeve >= rule.sleeve
)
{
final_rule_index = i;
}
}
if (rules[final_rule_index]) {
console.log(rules[final_rule_index].size);
} else {
console.log("N/A");
}
如果您想要更改或添加一些项目到规则,通过这种方式您不需要重写您的计算代码