我在正确的轨道上吗?剪刀石头布 (Javascript)

Am I on the right track? Rock Paper Scissors (Javascript)

我完全是 JavaScript 的新手,目前正在研究 The Odin Project 的 Rock Paper Scissors project

  1. Your game is going to play against the computer, so begin with a function called computerPlay that will randomly return either ‘Rock’, ‘Paper’ or ‘Scissors’. We’ll use this function in the game to make the computer’s play. Tip: use the console to make sure this is returning the expected output before moving to the next step!

  2. Write a function that plays a single round of Rock Paper Scissors. The function should take two parameters - the playerSelection and computerSelection - and then return a string that declares the winner of the round like so: "You Lose! Paper beats Rock"

    a. Make your function’s playerSelection parameter case-insensitive (so users can input rock, ROCK, RocK or any other variation).

  3. Important note: you want to return the results of this function call, not console.log() them. [...]

  4. Write a NEW function called game(). Call the playRound function inside of this one to play a 5 round game that keeps score and reports a winner or loser at the end.

    a. Remember loops? This is a great opportunity to use one to play those five rounds

    b. At this point you should be using console.log() to display the results of each round and the winner at the end.

    c. Use prompt() to get input from the user. Read the docs here if you need to.

    d. Feel free to re-work your previous functions if you need to. Specifically, you might want to change the return value to something more useful.

    e. Feel free to create more “helper” functions if you think it would be useful.

我尽力不去看别人是如何解决它的,但我已经这样做了将近 2 天了,我只想知道我是否走在正确的轨道上?还是我应该完全改变我的做法?

function computerPlay() {
   const pick = ['rock', 'paper', 'scissors'];
   return pick[Math.floor(Math.random() * pick.length)];
}

function playRound(playerSelection, computerSelection) {
   if (playerSelection === computerSelection) {
      return `It's a tie! you both picked ${playerSelection}`;
   } else if (playerSelection === "rock" && computerSelection === "scissors") {
      return "You win! Rock beats Scissors";
   } else if (playerSelection === "paper" && computerSelection === "rock") {
      return "You win! Paper beats Rock";
   } else if (playerSelection === "scissors" && computerSelection === "paper") {
      return "You win! Scissors beats Paper";
   } else {
      return `You lose! ${computerSelection} beats ${playerSelection}`;
   }
}

const playerSelection = prompt("Start the game by picking among 'Rock, Paper, Scissors'").toLowerCase();
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection))

嘿嘿嘿嘿。

对于每个条件,您 return 一个结果。这意味着您不需要使用 else if,只需使用 if 语句即可。我个人讨厌 else if 条件 也许 switch 更利于可读性。

你走在正确的轨道上。

您根据挑战中给出的要求创建了函数 computerPlayplayRound

现在您已经可以正常工作了,您应该继续下一步:创建 game 函数。您已经完成了 prompt 部分。

当你到达需要保持分数的部分时,你会发现 playRound 到 return 一个值会更好,比如 -1, 0, 1取决于结果,因为这样更容易更新分数。然后该值也可用于输出消息。