“TOP ‘rock, paper, scissors’ 帮助:警告未定义”

huangapple go评论70阅读模式
英文:

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 = [&#39;rock&#39;, &#39;paper&#39;, &#39;scissors&#39;];
return choices[Math.floor(Math.random() * choices.length)];
}
function playRound(playerSelection, computerSelection) {
if ( (playerSelection == &quot;paper&quot; &amp;&amp; computerSelection === &quot;rock&quot;) ||
(playerSelection == &quot;scissors&quot; &amp;&amp; computerSelection == &quot;paper&quot;) ||
(playerSelection == &quot;rock&quot; &amp;&amp; computerSelection == &quot;scissors&quot;) ) {
playerScore += 1;
alert(&quot;You win! &quot; + playerSelection + &quot; beats &quot; + computerSelection);
} 
else if ( (playerSelection == computerSelection) ) {
alert(&quot;It&#39;s a tie! You both chose &quot; + playerSelection);
} else {
computerScore += 1;
alert(&quot;You lost! &quot; + computerSelection + &quot; beats &quot; + playerSelection);
}
}
function roundScore() {
alert(&#39;Scoreboard: Player &#39; + playerScore + &#39; x &#39; + computerScore + &#39; Computer&#39;);
}
function checkWinner() {
if (playerScore &gt; computerScore) {
return &#39;You beat the machine! The world isn\&#39;t doomed after all.&#39;;
}
else if (playerScore == computerScore) {
return &#39;Tie! Humans and robots are equally smart.&#39;
}
else{
return &#39;The computer wins. The Age of Ultron is coming.&#39;;
}    
}
function game() {
for (let i = 0; i &lt; 5; i++) {
let playerSelection = prompt(&#39;Choose your player! Rock, paper or scissors?&#39;); 
playerSelection = playerSelection.toLowerCase(); 
let computerSelection = getComputerChoice(); 
alert(&#39;The computer chose &#39; + 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 = [&#39;rock&#39;, &#39;paper&#39;, &#39;scissors&#39;];
return choices[Math.floor(Math.random() * choices.length)];
}
function playRound(playerSelection, computerSelection) {
if ( (playerSelection == &quot;paper&quot; &amp;&amp; computerSelection === &quot;rock&quot;) ||
(playerSelection == &quot;scissors&quot; &amp;&amp; computerSelection == &quot;paper&quot;) ||
(playerSelection == &quot;rock&quot; &amp;&amp; computerSelection == &quot;scissors&quot;) ) {
playerScore += 1;
return &quot;You win! &quot; + playerSelection + &quot; beats &quot; + computerSelection;
} 
else if ( (playerSelection == computerSelection) ) {
return &quot;It&#39;s a tie! You both chose &quot; + playerSelection;
} else {
computerScore += 1;
return &quot;You lost! &quot; + computerSelection + &quot; beats &quot; + playerSelection;
}
}
function roundScore() {
alert(&#39;Scoreboard: Player &#39; + playerScore + &#39; x &#39; + computerScore + &#39; Computer&#39;);
}
function checkWinner() {
if (playerScore &gt; computerScore) {
return &#39;You beat the machine! The world isn\&#39;t doomed after all.&#39;;
}
else if (playerScore == computerScore) {
return &#39;Tie! Humans and robots are equally smart.&#39;
}
else{
return &#39;The computer wins. The Age of Ultron is coming.&#39;;
}    
}
function game() {
for (let i = 0; i &lt; 5; i++) {
let playerSelection = prompt(&#39;Choose your player! Rock, paper or scissors?&#39;); 
playerSelection = playerSelection.toLowerCase(); 
let computerSelection = getComputerChoice(); 
let roundResult = playRound(playerSelection, computerSelection);
alert(&#39;The computer chose &#39; + computerSelection);
alert(roundResult);
roundScore();
}
}
game();
alert( checkWinner() );

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月23日 11:49:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定