ts(1005) 错误和控制台日志错误“Uncaught SyntaxError:意外的标识符‘位置’”(第 32 行)

问题描述 投票:0回答:1

The bigger picture

我是一名新生,我面临着分支逻辑的挑战。到目前为止,我已经解决了这个问题,除了我在最后一行遇到的一个错误,第 32 行

第32行:

location === 'NK' ? console.log(BANNED_WARNING) : console.log('Price:', currency, shoes + batteries + pens + shirts + shipping)

**错误:**

  1. 在 VSCode 中,“位置”用红色下划线表示,并带有以下内容:
    : expected. ts(1005)
  2. 在我的 DevTools 控制台日志中:未捕获的语法错误:意外的标识符“位置”

**这是我的所有代码:**

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 的帮助下解决这个问题,但没有任何帮助。

我有:

  1. 在需要的地方添加分号
  2. 将“let location”更改为“let locationValue”,并在必要的地方进行更改。根据 Bito.AI 的说法,这是一个可能的解决方案。
  3. 将其从 Tenary 更改回正常的 if 循环。
javascript typescript syntax-error variable-assignment console.log
1个回答
0
投票
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)

© www.soinside.com 2019 - 2024. All rights reserved.