Help with Rock Paper Scissors JS


#1

Hello,

Can someone please help me with this? I’m only getting “You tie!” and undefined responses from the console. I know the case types don’t match i.e. ROCK and rock, but I thought it wouldn’t matter with ==. Any advice would be greatly appreciated.

function computerPlay() {
let hands = [‘rock’, ‘paper’, ‘scissors’]
return hands[Math.floor(Math.random() * hands.length)].toUpperCase()
}
function playRound(playerSelection, computerSelection) {

playerSelection = playerSelection.toUpperCase()

if (playerSelection == computerSelection) {
return ('You tie!') }

else if (playerSelection == 'rock' && computerSelection === 'paper') {
return ('You Lose! Paper beats Rock')}

else if (playerSelection == 'rock' && computerSelection == 'scissors') {
return ('You Win! Rock beats Scissors')}

else if (playerSelection == 'rock' && computerSelection == 'scissors') {
return ('You win!!')}

else if (playerSelection == 'paper' && computerSelection == 'scissors') {
return ('You Lose, Scissors cuts Paper')}

else if (playerSelection == 'paper' && computerSelection == 'rock') {
return ('You Win, Paper wraps Rock!')}

else if (playerSelection == 'scissors' && computerSelection == 'paper') {
return ('You Win, Scissors cuts Paper!')}

else if (playerSelection == 'scissors' && computerSelection == 'rock') {
return ('You Lose! Rock Smashes scissors')}

}

const playerSelection = ‘rock’
const computerSelection = computerPlay()
console.log(playRound(playerSelection, computerSelection))


#3

Try like that.
https://codepen.io/BojoZahariev/pen/ZEEzmKg?editors=0010

The problem is that ‘rock’ is not == ‘ROCK’ it returns false. More on == and === https://codeburst.io/javascript-double-equals-vs-triple-equals-61d4ce5a121a


#4

You convert the playerSelection to uppercase. I think you may have meant to convert it to lowercase.


#5

Great, Thanks for the comments! got it now.