英文:
TOP 'rock, paper, scissors' help: alert undefined
问题
我想知道是否有人能帮我解决这个小问题。
我目前正在进行TOP的剪刀石头布项目,我的代码按照预期工作,但在得分提示之前会出现“undefined”,我不确定原因。
当我查看控制台时,还会打印以下消息:
未捕获的类型错误:无法读取 null 的属性(读取 'toLowerCase')
在 game-2.js:47:43 处
在 game-2.js:55:1 处
let playerScore = 0;
let computerScore = 0;
function getComputerChoice() {
let choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)];
}
function playRound(playerSelection, computerSelection) {
if ((playerSelection == "paper" && computerSelection === "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors")) {
playerScore += 1;
alert("You win! " + playerSelection + " beats " + computerSelection);
} else if (playerSelection == computerSelection) {
alert("It's a tie! You both chose " + playerSelection);
} else {
computerScore += 1;
alert("You lost! " + computerSelection + " beats " + playerSelection);
}
}
function roundScore() {
alert('Scoreboard: Player ' + playerScore + ' x ' + computerScore + ' Computer');
}
function checkWinner() {
if (playerScore > computerScore) {
return 'You beat the machine! The world isn\'t doomed after all.';
} else if (playerScore == computerScore) {
return 'Tie! Humans and robots are equally smart.';
} else {
return 'The computer wins. The Age of Ultron is coming.';
}
}
function game() {
for (let i = 0; i < 5; i++) {
let playerSelection = prompt('Choose your player! Rock, paper or scissors?');
playerSelection = playerSelection.toLowerCase();
let computerSelection = getComputerChoice();
alert('The computer chose ' + computerSelection);
playRound(playerSelection, computerSelection);
roundScore();
}
}
game();
alert(checkWinner());
如果有人能帮忙,那太棒了,谢谢!我尝试了不同的写法,例如 prompt().toLowerCase()
,但都没有生效。
英文:
I was wondering if someone could help me with this small bug
I'm currently working on TOP rock paper scissors project and my code works as it's suppose to but it alerts 'undefined' before the Scoreboard alert and I'm not sure why
When I look at the console it also prints the following message:
Uncaught TypeError: Cannot read properties of null (reading 'toLowerCase')
at game (game-2.js:47:43)
at game-2.js:55:1
let playerScore = 0;
let computerScore = 0;
function getComputerChoice() {
let choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)];
}
function playRound(playerSelection, computerSelection) {
if ( (playerSelection == "paper" && computerSelection === "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors") ) {
playerScore += 1;
alert("You win! " + playerSelection + " beats " + computerSelection);
}
else if ( (playerSelection == computerSelection) ) {
alert("It's a tie! You both chose " + playerSelection);
} else {
computerScore += 1;
alert("You lost! " + computerSelection + " beats " + playerSelection);
}
}
function roundScore() {
alert('Scoreboard: Player ' + playerScore + ' x ' + computerScore + ' Computer');
}
function checkWinner() {
if (playerScore > computerScore) {
return 'You beat the machine! The world isn\'t doomed after all.';
}
else if (playerScore == computerScore) {
return 'Tie! Humans and robots are equally smart.'
}
else{
return 'The computer wins. The Age of Ultron is coming.';
}
}
function game() {
for (let i = 0; i < 5; i++) {
let playerSelection = prompt('Choose your player! Rock, paper or scissors?');
playerSelection = playerSelection.toLowerCase();
let computerSelection = getComputerChoice();
alert('The computer chose ' + computerSelection);
alert(playRound(playerSelection, computerSelection));
roundScore();
}}
game();
alert( checkWinner() );
If anyone could help me that would be awesome, thank you!
I tried different ways of writing prompt().toLowerCase() but nothing works
答案1
得分: 2
这一行的功能是:
alert(playRound(playerSelection, computerSelection));
你的 playRound
函数没有返回任何内容,因此相当于 alert(undefined)
。
英文:
This line does it:
alert(playRound(playerSelection, computerSelection));
Your playRound
function doesn’t return anything, so this is the equivalent of alert(undefined)
.
答案2
得分: 0
alert(playRound(playerSelection, computerSelection)) 这行代码会触发一个弹窗显示游戏结果。不需要翻译。
以下是您代码中的一些注释部分,也无需翻译:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!-- end snippet -->
英文:
alert(playRound(playerSelection, computerSelection));
This does not return anything. Fix it for ya
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
let playerScore = 0;
let computerScore = 0;
function getComputerChoice() {
let choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)];
}
function playRound(playerSelection, computerSelection) {
if ( (playerSelection == "paper" && computerSelection === "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors") ) {
playerScore += 1;
return "You win! " + playerSelection + " beats " + computerSelection;
}
else if ( (playerSelection == computerSelection) ) {
return "It's a tie! You both chose " + playerSelection;
} else {
computerScore += 1;
return "You lost! " + computerSelection + " beats " + playerSelection;
}
}
function roundScore() {
alert('Scoreboard: Player ' + playerScore + ' x ' + computerScore + ' Computer');
}
function checkWinner() {
if (playerScore > computerScore) {
return 'You beat the machine! The world isn\'t doomed after all.';
}
else if (playerScore == computerScore) {
return 'Tie! Humans and robots are equally smart.'
}
else{
return 'The computer wins. The Age of Ultron is coming.';
}
}
function game() {
for (let i = 0; i < 5; i++) {
let playerSelection = prompt('Choose your player! Rock, paper or scissors?');
playerSelection = playerSelection.toLowerCase();
let computerSelection = getComputerChoice();
let roundResult = playRound(playerSelection, computerSelection);
alert('The computer chose ' + computerSelection);
alert(roundResult);
roundScore();
}
}
game();
alert( checkWinner() );
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论