我是JavaScript新手并将其绑定但我收到错误
getluck(luckyNumber) {
switch (luckyNumber) {
case add:
console.log(1 + 1)
break;
case subtract:
console.log(2 - 1)
break;
case divide:
console.log(2 / 3)
break;
case multiply:
console.log(2 * 9)
break;
default:
console.log('I lose')
break;
}
}
将其称为getluck(subtract)
。我做错了吗?
答案 0 :(得分:1)
您正在尝试发送未定义的变量,并且case语句也在检查未定义的变量。请将它们设为字符串或定义这些变量。
function getluck(luckyNumber) {
switch(luckyNumber) {
case 'add':
console.log(1+1)
break;
case 'subtract':
console.log(2-1)
break;
case 'divide':
console.log(2/3)
break;
case 'multiply':
console.log(2*9)
break;
default :
console.log('I loose')
}
}
getluck('add');