我是一名新生,我面临着分支逻辑的挑战。到目前为止,我已经解决了这个问题,除了我在最后一行遇到的一个错误,第 32 行:
第32行:
location === 'NK' ? console.log(BANNED_WARNING) : console.log('Price:', currency, shoes + batteries + pens + shirts + shipping)
**错误:**
: expected. ts(1005)
**这是我的所有代码:**
const FREE_WARNING = 'Free shipping only applies to single customer orders'
const BANNED_WARNING = 'Unfortunately we do not ship to your country of residence'
const NONE_SELECTED = 0
let customers = 1
let location = 'RSA'
let currency = null
let shipping = null
if (location === 'RSA') {
shipping = 400, currency = 'R';
}
if (location === 'NAM') {
shipping = 600, currency = '$'
} else {
shipping = 800, currency = '$'
}
let shoes = 300 * 1
let toys = 100 * 5
let shirts = 150 * NONE_SELECTED
let batteries = 35 * 2
let pens = 5 * NONE_SELECTED
if (location === 'RSA' && shoes + batteries + pens + shirts >= 1000) { shipping = 0 }
if (location === 'NAM' && shoes + batteries + pens + shirts >= 60) {shipping = 0}
shipping === 0 && customers !== 1 ? console.log(FREE_WARNING)
**location === 'NK' ? console.log(BANNED_WARNING) : console.log('Price:', currency, shoes + batteries + pens + shirts + shipping)**
我尝试在 Bito.AI 和 ChatGPT 的帮助下解决这个问题,但没有任何帮助。
我有:
shipping === 0 && customers !== 1 ? console.log(FREE_WARNING) // <--- ERROR HERE
// equals to
function foo() {
if (shipping == 0 && customers !== 1) {
return console.log(FREE_WARNING)
} else // <--- ERROR HERE
}
foo()
使用
shipping === 0 && customers !== 1 ? console.log(FREE_WARNING) : 0 // OK now
// equals to
function foo() {
if (shipping == 0 && customers !== 1) {
return console.log(FREE_WARNING)
} else { // OK now
return 0
}
}
foo()
改为。 (MDN)
有时 MDN 比 ChatGPT 更有用:)
您也可以使用
&&
来替换 ?
。 (MDN)