[英]My Script for Rock Paper Scissors isn't working. Console logs: Uncaught SyntaxError: Identifier 'playerSelection' has already been declared
I just started learning javaScript and my first proyect is to make a Rock Paper Scissors game that plays on the console, the code that I wrote is:我刚开始学习javaScript,我的第一个项目是制作一个在控制台上玩的石头剪刀布游戏,我写的代码是:
<!DOCTYPE html>
<html>
<body>
<script>
let RPS = function playRound (playerSelection, computerSelection) {
let playerSelection = promt ("What do you choose?", "");
let computerSelection = math.Random ();
if computerSelection (<0.34) {
computerSelection = "Rock";
} else if computerSelection (>=0.35 && <=0.66) {
computerSelection = "Paper";
} else {
computerSelection = "Scissors";
}
if (playerSelection === "Rock"){
if (computerSelection === "Scissors") {
console.log ("You win");
} else {
console.log ("You lose");
}
}
if (playerSelection === "Paper") {
if (computerSelection === "Scissors"){
console.log ("You win");
}else {
console.log ("You lose")
}
}
if (playerSelection === "Scissors") {
if (computerSelection ==== "Paper"){
console.log ("You win")
} else {
console.log ("You lose")
}
}
}
</script>
</body>
</html>
When I try to run this on the console I get当我尝试在控制台上运行它时,我得到
Uncaught SyntaxError: Identifier 'playerSelection' has already been declared
Why is that?这是为什么?
The error is very straight forward.错误非常直接。 You are passing arguments named
playerSelection
and computerSelection
then you immediately try to declare two local variables of the same name - however, arguments are considered local variables themselves, so you are indeed duplicating.您正在传递名为
playerSelection
和computerSelection
参数,然后您立即尝试声明两个同名的局部变量 - 但是,参数本身被视为局部变量,因此您确实在重复。 Either pass the variables to the function as arguments or declare them in the code using let
, not both.要么将变量作为参数传递给函数,要么使用
let
在代码中声明它们,而不是两者都使用。
Additionally, your code is riddled with syntax errors, you definitely need to proof-read this code because it will not run even if you fix the issue you ask about in the question.此外,您的代码充满了语法错误,您绝对需要校对此代码,因为即使您解决了您在问题中提出的问题,它也不会运行。
Here is an example of what you probably want:以下是您可能想要的示例:
let RPS = function playRound() { let playerSelection = prompt("What do you choose?", ""); let computerSelection = Math.random(); if (computerSelection < 0.34) { computerSelection = "Rock"; } else if (computerSelection >= 0.35 && computerSelection <= 0.66) { computerSelection = "Paper"; } else { computerSelection = "Scissors"; } if (playerSelection === "Rock") { if (computerSelection === "Scissors") { console.log("You win"); } else { console.log("You lose"); } } if (playerSelection === "Paper") { if (computerSelection === "Scissors") { console.log("You win"); } else { console.log("You lose") } } if (playerSelection === "Scissors") { if (computerSelection === "Paper") { console.log("You win") } else { console.log("You lose") } } } RPS()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.