skip to Main Content

I am building Rock, Paper, Scissors in Javascript following along The Odin Project syllabus.

When creating a function to execute a single round of the game I used two parameters (playerSelection, compSelection), variables I thought were relevant to the function.

Instead, when executing with these parameters, the game would default to a tie message located on the last else if statement of the conditional block.

I used console.log to verify the equality of the two parameters as the condition for the tie message to occur would be (playerSelection === compSelection). It would recognise when the game was actually a tie or not as it output ‘true’ and ‘false’ when expected whilst verifying the equality.

It worked when I removed the two parameters and ran it as singleround() rather than singleround(playerSelection, compSelection).

I’m struggling to understand why, and cannot seem to understand the functionality of parameters.

Can anyone summarise my problem?

let randomNumber = Math.floor(Math.random() * 3) + 1;
let playerSelection = prompt("Please choose Rock, Paper or Scissors!").toLowerCase();
let compSelection;

(function getComputerChoice() {

  if (randomNumber === 1) {
    return compSelection = "rock";
  } else if (randomNumber === 2) {
    return compSelection = "paper";
  } else if (randomNumber === 3) {
    return compSelection = "scissors";
  }
})();

console.log(randomNumber);
console.log(compSelection);
console.log(playerSelection);

(function singleround(playerSelection, compSelection) {

  if (playerSelection === "rock" && compSelection === "paper") {
    console.log("Oh No! You lost, Paper beats Rock :(");
  } else if (playerSelection === "rock" && compSelection === "scissors") {
    console.log("Well done! You won, Rock beats Scissors!");
  } else if (playerSelection === "paper" && compSelection === "rock") {
    console.log("Well done! You won, Paper beats Rock!");
  } else if (playerSelection === "paper" && compSelection === "scissors") {
    console.log("Oh No! You lost, Scissors beats Paper :(");
  } else if (playerSelection === "scissors" && compSelection === "rock") {
    console.log("Oh No! You lost, Rock beats Scissors :(");
  } else if (playerSelection === "scissors" && compSelection === "paper") {
    console.log("Well done! You won, Scissors beats Paper!");
  } else if (playerSelection === compSelection) {
    console.log("It's a tie! Try again.");
  } else {
    console.log("Uh Oh! Something's gone wrong. Refresh the page to try again!");
  }
})();

console.log(playerSelection === compSelection);

2

Answers


  1. Your combinations of IIFEs and returning compSelection=something is not helping

    If you pass the choices to the IIFE it will work (see other answer)

    Here is a simpler and IMNHO more elegant version using a dictionary. The lookup gives the winner

    const choices = ["rock", "paper", "scissors"];
    
    function singleround(playerSelection, compSelection) {
        const winConditions = {
            "rock": "scissors",
            "paper": "rock",
            "scissors": "paper"
        };
    
        if (playerSelection === compSelection) {
            return "It's a tie! Try again.";
        } else if (winConditions[playerSelection] === compSelection) {
            return `Well done! You won, ${playerSelection.charAt(0).toUpperCase() + playerSelection.slice(1)} beats ${compSelection.charAt(0).toUpperCase() + compSelection.slice(1)}!`;
        } else {
            return `Oh No! You lost, ${compSelection.charAt(0).toUpperCase() + compSelection.slice(1)} beats ${playerSelection.charAt(0).toUpperCase() + playerSelection.slice(1)} :(`;
        }
    }
    
    let randomNumber = Math.floor(Math.random() * choices.length);
    let playerSelection = prompt("Please choose Rock, Paper, or Scissors!").toLowerCase();
    let compSelection = choices[randomNumber];
    
    console.log("Player selected:", playerSelection);
    console.log("Computer selected:", compSelection);
    
    console.log(singleround(playerSelection, compSelection));

    If you want the 5 choice version it is little more tricky:

    const choices = ["rock", "paper", "scissors", "lizard", "spock"];
    
    function singleround(playerSelection, compSelection) {
      const winConditions = {
        "rock": ["scissors", "lizard"],
        "paper": ["rock", "spock"],
        "scissors": ["paper", "lizard"],
        "lizard": ["paper", "spock"],
        "spock": ["scissors", "rock"]
      };
    
      if (playerSelection === compSelection) {
        return "It's a tie! Try again.";
      } else if (winConditions[playerSelection].includes(compSelection)) { // lookup to see the computer selection is in the list of losing conditions
        return `Well done! You won, ${capitalizeFirstLetter(playerSelection)} beats ${capitalizeFirstLetter(compSelection)}!`;
      } else {
        return `Oh No! You lost, ${capitalizeFirstLetter(compSelection)} beats ${capitalizeFirstLetter(playerSelection)} :(`;
      }
    }
    
    function capitalizeFirstLetter(string) {
      return string.charAt(0).toUpperCase() + string.slice(1);
    }
    
    let randomNumber = Math.floor(Math.random() * choices.length);
    let playerSelection = prompt("Please choose Rock, Paper, Scissors, Lizard, or Spock!").toLowerCase();
    let compSelection = choices[randomNumber];
    
    console.log("Player selected:", capitalizeFirstLetter(playerSelection));
    console.log("Computer selected:", capitalizeFirstLetter(compSelection));
    
    console.log(singleround(playerSelection, compSelection));
    <ul>
      <li>Rock crushes Scissors and crushes Lizard.</li>
      <li>Paper covers Rock and disproves Spock.</li>
      <li>Scissors cuts Paper and decapitates Lizard.</li>
      <li>Lizard eats Paper and poisons Spock.</li>
      <li>Spock smashes Scissors and vaporizes Rock.</li>
    </ul>
    Login or Signup to reply.
  2. because you are using an IIFE function ( function immediately invoked )

    the exact code is this

    let randomNumber = Math.floor(Math.random() * 3) + 1;
    let playerSelection = prompt("Please choose Rock, Paper or Scissors!").toLowerCase();
    let compSelection;
    
    (function getComputerChoice() {
    
      if (randomNumber === 1) {
        return compSelection = "rock";
      } else if (randomNumber === 2) {
        return compSelection = "paper";
      } else if (randomNumber === 3) {
        return compSelection = "scissors";
      }
    })();
    
    console.log(randomNumber);
    console.log(compSelection);
    console.log(playerSelection);
    
    (function singleround(playerSelection, compSelection) {
    
      if (playerSelection === "rock" && compSelection === "paper") {
        console.log("Oh No! You lost, Paper beats Rock :(");
      } else if (playerSelection === "rock" && compSelection === "scissors") {
        console.log("Well done! You won, Rock beats Scissors!");
      } else if (playerSelection === "paper" && compSelection === "rock") {
        console.log("Well done! You won, Paper beats Rock!");
      } else if (playerSelection === "paper" && compSelection === "scissors") {
        console.log("Oh No! You lost, Scissors beats Paper :(");
      } else if (playerSelection === "scissors" && compSelection === "rock") {
        console.log("Oh No! You lost, Rock beats Scissors :(");
      } else if (playerSelection === "scissors" && compSelection === "paper") {
        console.log("Well done! You won, Scissors beats Paper!");
      } else if (playerSelection === compSelection) {
        console.log("It's a tie! Try again.");
      } else {
        console.log("Uh Oh! Something's gone wrong. Refresh the page to try again!");
      }
    })(playerSelection, compSelection);
    
    console.log(playerSelection === compSelection);

    you need to specify the parameters you will pass to the singleround function in round brackets at the end.

    For example this iife function

    (function(a, b)) {
    
         var c = a + " " + b;
         console.log(c);
    
    }("Hello", "World"));
    

    returns "Hello World" in to console.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search