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
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
If you want the 5 choice version it is little more tricky:
because you are using an IIFE function ( function immediately invoked )
the exact code is this
you need to specify the parameters you will pass to the singleround function in round brackets at the end.
For example this iife function
returns "Hello World" in to console.