Rock Paper Scissors Project :)

Hello everyone! This is my very fisrt post on a codecademy forum. I really liked how my project turned out so I am excited to share it with you all. :blush:
Please feel free to critique my work and comment on it. I’m looking forward to reading any type of comment.

console.log("Rock, Paper, Scisors Game");
// Select your choice
let playerChoice = ""

let compChoice = Math.floor(Math.random () * 4);
  //Translate the computer's choice
  switch (compChoice) {
    case 1:
    compChoice = "Rock";
    break;
    case 2:
    compChoice = "Paper";
    break;
    case 3:
    compChoice = "Scissor";
    break; 
  }
if (compChoice != 0) {
  console.log(game(compChoice, playerChoice));
  console.log(`The computer chose ${compChoice}, you chosed ${playerChoice}.`);
} else {
  console.log("The computer can't make a choice now. Try again.")
}


function game(compChoice, playerChoice) {
  //its a draw
  if (compChoice === playerChoice) {
    return "It's a draw!";
  }
  //computer wins
  if ("Rock", "Scissor" || "Paper","Rock" || "Scissor","Paper") {
    return "The computer wins";
  } else { //The player wins 
    return "You win!"
  }
}

Not sure how that is expected to work. Please explain with some examples.

oh yes of course! so the logic behind this is if the results are either Rock vs Scissor, Paper vs Rock, or Scissor vs Paper (the first value is the computers “choice”), then the computer will win, then the else just covers every other possible outcome (in which the computer loses).

How can we be sure it works. There is a definite problem with the if statement logic.

I think what @mtf is referring to is the fact that neither the player nor the computers choice is being used in

if ("Rock", "Scissor" || "Paper","Rock" || "Scissor","Paper") {
    return "The computer wins";
} else { //The player wins 
    return "You win!"
}

You are simply comparing strings which do not change and thus the actual choices of the computer and player are not being compared.

I ran the code a few times and it seems that even when the computer chooses scissors and the player chooses rock, the computer wins.

Maybe you could use a switch to solve the problem instead?

Another thing is the computer choosing. I can see that you have correctly implemented an if statement to stop execution if the computer chooses 0, however this could also be solved by looping the code if the choice is 0.

#some pseudo code to explain what I mean

if (choice == 0) {
  let choice = computer_choice()
} 

As it stands, barring any thrown error, the above if will always be true.

 > if ("Rock", "Scissor" || "Paper","Rock" || "Scissor","Paper") {
       console.log(true)
   } else {  
       console.log(false)
   }
   true
<- undefined

The why of it is difficult to explain on the face of the present construction except to say that ALL non-empty strings are truthy and assuming the whole front of the list were to be removed, we would be left with "Paper", which is truthy.

 > ("Rock", "Scissor" || "Paper","Rock" || "Scissor","Paper")
<- 'Paper'

Just to confirm, let’s change the letter case.

> ("Rock", "scissor" || "Paper","rock" || "Scissor","paper")
<- 'paper'

Let’s start peeling it back, first by removing the last comma and following string…

 > "Rock", "scissor" || "Paper","rock" || "Scissor"
<- 'rock'

At least now we can see the short-circuiting effect of true on OR.

Peel back some more…

 > "Rock", "scissor" || "Paper"
<- 'scissor'

And finally,

 > if ("Rock") console.log(true)
   true
<- undefined

Rather than spend the day trying to understand how JS comes to the conclusions it does, one can focus on writing a conditional that will work as expected. Back to the drawing board.