【发布时间】:2021-07-15 12:56:14
【问题描述】:
刚开始通过 Odin 项目学习 Javascript。构建石头剪刀布,我正在尝试构建我自己的逻辑,但有点卡住了。
我有电脑和玩家的输入。但是,一个返回一个字符串,而另一个返回一个数字。解决办法?
另外,我认为我的游戏逻辑有缺陷。
const rps = ["ROCK", "PAPER", "SCISSORS"];
let computerPlay = Math.floor(Math.random() * rps.length);
console.log(rps[computerPlay]);
let playerSelection = window.prompt("ROCK, PAPER, or SCISSORS?")
console.log(playerSelection, computerPlay);
/* Need to change both to strings? */
/* Game Logic*/
if (playerSelection === "ROCK") {
if (computerPlay === "ROCK")
return "TIE: Both selected ROCK";
else if (computerPlay === "PAPER")
return "You Lose! PAPER beats ROCK";
else (computerPlay === "SCISSORS")
return "You Won! ROCK beats SCISSORS";
}
else if (playerSelection === "PAPER") {
if (computerPlay === "PAPER")
return "TIE: Both selected PAPER";
else if (computerPlay === "ROCK")
return "You Won! PAPER beats ROCK";
else (computerPlay === "SCISSORS")
return "You Lose! SCISSORS beats PAPER";
} else if (playerSelection === "SCISSORS") {
if (computerPlay === "SCISSORS")
return "TIE: Both selected SCISSORS";
else if (computerPlay === "PAPER")
return "You Won! SCISSORS beats PAPER";
else (computerPlay === "ROCK")
return "You Lose! ROCK beats SCISSORS";}```
【问题讨论】:
-
您需要在某个时候将
computerPlay设置为rps[computerPlay],或者引入另一个变量。您正在比较计算机选择的 index 而不是值。
标签: javascript logic