我正试图为多个函数参数编写一个简单的比较语句,它绕过了错误。tbh,我甚至不确定我是否能够比较这些论点。
我试图添加一个嵌套的if来提取错误,但它返回未知异常。(取消注释嵌套的if以查看行为(
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
if ((par || strokes) <= 0) {
/*if (par) {
return "Please enter a value greater than " + par + " for Par";
}
else if (strokes) { */
return "Please enter a value greater than " + strokes + " for Strokes";
}
else if (strokes == 1) {
return names[0];
}
else if (strokes <= (par - 2)) {
return names[1];
}
else if (strokes == (par - 1)) {
return names[2];
}
else if (strokes == (par)) {
return names[3];
}
else if (strokes == (par + 1)) {
return names[4];
}
else if (strokes == (par + 2)) {
return names[5];
}
return names[6];
}
// Change these values to test
console.log(golfScore(0,1));console.log(golfScore(5,4));
((par || strokes) <= 0)
||
使其评估par
或stroke
是否不是"false"数据类型(false、null、undefined(。基本上,您正在尝试将布尔值与数字进行比较。相反,您应该将每个变量与0 进行比较
(par <= 0 || strokes <= 0)