Javascript,意外标记“)”
Javascript, unexpected token ' ) '
这是我在 Whosebug 中的第一个问题。我正在学习 JS 基础知识。我试图从上到下重新检查我编写的代码,但 'unexpected token ')' ' 不断出现。我从视频中重写了整个代码,以确保我自己编写并理解代码的每一行以及让自己熟悉编写代码。我试图删除最后一行代码 '})' 但我想知道为什么错误仍然发生。我希望任何人都可以帮助我,这令人沮丧!
const secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
document.querySelector('.number').textContent = secretNumber;
document.querySelector('.check').addEventListener('click',
function() {
const guess = Number(document.querySelector('.guess').value);
console.log(guess, typeof guess);
if (!guess) {
document.querySelector('.message').textContent = 'No Number!';
} else if (guess === secretNumber) {
document.querySelector('.message').textContent = 'Correct Number!';
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector('.message').textContent = 'Too High!';
score--;
document.querySelector('.score').textContent = score;
} else {
document.querySelector('.message').textContent = 'You lost the game!';
document.querySelector('.score').textContent = 0;
}
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector('.message').textContent =
'Too low!';
score--;
document.querySelector('.score').textContent =
score;
} else {
document.querySelector('.message').textContent =
'You lost the game!';
document.querySelector('.score').textContent = 0;
};
});
const secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
document.querySelector('.number').textContent = secretNumber;
document.querySelector('.check').addEventListener('click',
function() {
const guess = Number(document.querySelector('.guess')
.value);
console.log(guess, typeof guess);
if (!guess) {
document.querySelector('.message').textContent =
'No Number!';
} else if (guess === secretNumber) {
document.querySelector('.message').textContent =
'Correct Number!';
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector('.message').textContent =
'Too High!';
score--;
document.querySelector('.score').textContent =
score;
} else {
document.querySelector('.message').textContent =
'You lost the game!';
document.querySelector('.score').textContent = 0;
}
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector('.message').textContent =
'Too low!';
score--;
document.querySelector('.score').textContent =
score;
} else {
document.querySelector('.message').textContent =
'You lost the game!';
document.querySelector('.score').textContent = 0;
};
}
});
您还没有关闭最后一个 if 语句 else if (guess > secretNumber)
一致的缩进和 Sublime text 都有助于解决这个问题,见下图...
你可以看到关闭函数 })
比开头更深一个缩进,表示未关闭的语句。我们也可以通过 Sublime Texts 错误高亮来判断。
这是我在 Whosebug 中的第一个问题。我正在学习 JS 基础知识。我试图从上到下重新检查我编写的代码,但 'unexpected token ')' ' 不断出现。我从视频中重写了整个代码,以确保我自己编写并理解代码的每一行以及让自己熟悉编写代码。我试图删除最后一行代码 '})' 但我想知道为什么错误仍然发生。我希望任何人都可以帮助我,这令人沮丧!
const secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
document.querySelector('.number').textContent = secretNumber;
document.querySelector('.check').addEventListener('click',
function() {
const guess = Number(document.querySelector('.guess').value);
console.log(guess, typeof guess);
if (!guess) {
document.querySelector('.message').textContent = 'No Number!';
} else if (guess === secretNumber) {
document.querySelector('.message').textContent = 'Correct Number!';
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector('.message').textContent = 'Too High!';
score--;
document.querySelector('.score').textContent = score;
} else {
document.querySelector('.message').textContent = 'You lost the game!';
document.querySelector('.score').textContent = 0;
}
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector('.message').textContent =
'Too low!';
score--;
document.querySelector('.score').textContent =
score;
} else {
document.querySelector('.message').textContent =
'You lost the game!';
document.querySelector('.score').textContent = 0;
};
});
const secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
document.querySelector('.number').textContent = secretNumber;
document.querySelector('.check').addEventListener('click',
function() {
const guess = Number(document.querySelector('.guess')
.value);
console.log(guess, typeof guess);
if (!guess) {
document.querySelector('.message').textContent =
'No Number!';
} else if (guess === secretNumber) {
document.querySelector('.message').textContent =
'Correct Number!';
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector('.message').textContent =
'Too High!';
score--;
document.querySelector('.score').textContent =
score;
} else {
document.querySelector('.message').textContent =
'You lost the game!';
document.querySelector('.score').textContent = 0;
}
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector('.message').textContent =
'Too low!';
score--;
document.querySelector('.score').textContent =
score;
} else {
document.querySelector('.message').textContent =
'You lost the game!';
document.querySelector('.score').textContent = 0;
};
}
});
您还没有关闭最后一个 if 语句 else if (guess > secretNumber)
一致的缩进和 Sublime text 都有助于解决这个问题,见下图...
你可以看到关闭函数 })
比开头更深一个缩进,表示未关闭的语句。我们也可以通过 Sublime Texts 错误高亮来判断。