Javascript Rock Paper Scissors

Having a little trouble with the return statements here. The code (in the console) always evaluates to the else statement, returning “You lose!.” Tried double checking all spelling and capitalization. Any guidance is much appreciated - please don’t just give me the answer, but try and guide me there. Thank you in advance!

// Choices //
let choice = ['rock', 'paper', 'scissors'];
// Randomly Generate a computer choice //
function getComputerChoice(choice){

    return choice[Math.floor(Math.random() * choice.length)];

}

console.log(getComputerChoice(choice));

// Playing the round ; determining winners //

function round (playerSelection,computerSelection){
if (playerSelection===computerSelection){
return "It's a tie! You both picked the same choice!" ;
}
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!"
}

}

const playerSelection = prompt("Please enter value").toLowerCase;
const computerSelection = getComputerChoice(choice)
console.log(round(playerSelection,computerSelection))

I didn’t even notice this, thank you for pointing it out!

The line now reads as the following;

Although this did allow other return values to be pushed to the console, there were still some instances where it returned faulty values. What might be causing this?

const playerSelection = prompt("Please enter value").toLowerCase();

Which instances in particular?

As long as you fixed the issue with calling the function, your code looks fine to me.

Note that what gets logged here may or may not be the same value that gets assigned to computerSelection in the line below, which eventually gets sent to the round function.

const computerSelection = getComputerChoice(choice);

If you really want to see what is getting compared in the round function, then add the following as the first line in the round function’s code block:

console.log(playerSelection, computerSelection);
1 Like

My apologies for the late response, I was away from my desktop.

console.log(playerSelection, computerSelection);


The issue im seeing after entering that line of code is that the initial declaration and console push of the computerSelection variable is not consistent. In the console, it might display “scissors” as the initial value, then once the round commences, the computerSelection variable is now “paper”, for example. Could this be related to the structure of the code? In other words, should I re-arrange the else if chain to be above the getComputerChoice function?

I see what you are saying now regarding the following line;

const computerSelection = getComputerChoice(choice);

Would there be a way to ensure this is consistent with what is being sent to the round function?

I think im understanding a bit better; this does not hurt the code, its just deceptive if pushed to the console log since it pulls from the initial function? Or am I misunderstanding? Because in theory, if i remove the following code:

console.log(getComputerChoice(choice));


Then technically this is functioning correctly right?

Gotcha that makes sense. I appreciate all your help!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.