JavaScript: rock paper scissors. Function error Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JavaScript: rock paper scissors. Function error without wasting too much if your time.

The question is published on by Tutorial Guruji team.

im trying to create the game “rock, paper or scissors” in JavaScript, but im stuck on the last function “game()”, it should repeat 5 times the function playRound() and throw a result each of those 5 times. But doesnt work.

function computerPlay(){
    let optionsList = ["rock", "paper", "scissors"];
    let option = optionsList[Math.floor(Math.random() * optionsList.length)];
    return option
}

function playRound(playerSelection, computerSelection){
    var playerSelection = prompt("Choose: rock, paper or scissors").toLowerCase();
    var computerSelection = computerPlay();

    if (playerSelection = "rock"){
        switch(computerSelection){
            case "rock":
                return "T";
            case "paper":
                return "M";
            case "scissors":
                return "Y";
        }
    }else if (playerSelection = "paper"){
        switch(computerSelection){
            case "rock":
                return "Y";
            case "paper":
                return "T";
            case "scissors":
                return "M";
        }
    }else if (playerSelection = "scissors"){
        switch(computerSelection){
            case "rock":
                return "M";
            case "paper":
                return "Y";
            case "scissors":
                return "T";
        }
    }
}

function game(){
    let i = 0
    for(i=1; i<=5; i++){
        playRound()
        if (playRound()= "M") {
            console.log("Machine Wins");
        }else if (playRound() = "Y"){
            console.log("You Win")
        }else if (playRound() = "T"){
            console.log("You Win!")
        }
    }
}

Answer

You have a couple errors:

  • The first is that you are trying to use the = operator to compare values. You need to use == or ===.
  • Secondly, you need to get the return value of playRound() and then check its value to determine the outcome of the game. But instead, you are calling playRound() initially in each iteration of your loop, and then again each time you try to determine the outcome.

Try:

function computerPlay() {
  let optionsList = ["rock", "paper", "scissors"];
  let option = optionsList[Math.floor(Math.random() * optionsList.length)];
  return option
}

function playRound(playerSelection, computerSelection) {
  var playerSelection = prompt("Choose: rock, paper or scissors").toLowerCase();
  var computerSelection = computerPlay();

  if (playerSelection === "rock") {
    switch (computerSelection) {
      case "rock":
        return "T";
      case "paper":
        return "M";
      case "scissors":
        return "Y";
    }
  } else if (playerSelection === "paper") {
    switch (computerSelection) {
      case "rock":
        return "Y";
      case "paper":
        return "T";
      case "scissors":
        return "M";
    }
  } else if (playerSelection === "scissors") {
    switch (computerSelection) {
      case "rock":
        return "M";
      case "paper":
        return "Y";
      case "scissors":
        return "T";
    }
  }
}

function game() {
  let i = 0
  for (i = 1; i <= 5; i++) {
    let outcome = playRound()
    if (outcome === "M") {
      console.log("Machine Wins");
    } else if (outcome === "Y") {
      console.log("You Win")
    } else if (outcome === "T") {
      console.log("You Win!")
    }
  }
}

game();
We are here to answer your question about JavaScript: rock paper scissors. Function error - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji