skip to Main Content

I am completing the odin projects rock paper scissors project and have a function that must log a response in the console depending on which else if function is true. If none of my else if functions are true it defaults to a separate message using the else conditional. The issue I am currently facing is that when the two values being tested don’t result in a draw and are passed through the conditionals; it is defaulting to one response even though the value being passed through the conditional should not result in that conditional being true.

I have tested to ensure the variables being tested in the conditionals are holding the correct values to produce a false response using console.log.
Help would be appreciated.

This is the function

for this example the variable player = scissors and compchoice = rock, paper, or scissor
the resulting console.log is "Paper! Hah, I knew beating you would be easy." which should only be possible if the player chooses rock and the computer chooses paper.

function playaround(PlayerEntry){
   let compchoice = getcomputerchoice();
    let player = PlayerEntry.toLowerCase();
    if (round = 1) {console.log("how about we do best of five?");} 
    console.log("rock...npaper...nscissors...");

    if (player == compchoice) {console.log ("A draw");}
    
    else if (player = "rock") {
        if (compchoice = "paper") {console.log ("Paper! I win!");
        } else {console.log("Scissors! I lose...");}
    }

    else if(player = "paper") {
        if (compchoice = "scissors") {console.log("Scissors!  I win!");}
         else {console.log("Rock! I lose...");}
    }

    else if (player = "scissors") {
        if (compchoice = "rock") {console.log("Rock! I win!");} 
        else {console.log("Paper! I lose...");}
    }

    else {console.log("someone does not know how to play rock paper scissors...");}
}

2

Answers


  1. You’re assigning the value on your conditionals, not comparing.

    You used = instead of ==.

    It’s a very very common mistake when we are starting ^^
    Keep training.

    Login or Signup to reply.
  2. I think a bulk of your problems come from how you are (or think you are) comparing values.

    Here’s break down of how comparison operators work in JavaScript:

    == is a loose comparison operator. This operator checks that the values are equal

    console.log(8 == 8) // true
    console.log(8 == "8") // true
    console.log(8 == 5) // false
    

    === is a strict comparison. This operator checks if the value AND the type are equal

    console.log(8 === 8) // true
    console.log(8 === "8") // false
    

    = is NOT a comparison operator. This operator is used for setting a value to a variable.

    const myVariable = "this is my variable content"
    

    As a general rule of thumb stick to the strict equality operator === as you will have a better time comparing values.

    Try updating your if/else if statements like so:

    if (round === 1) { console.log("how about we do best of five?"); }
    
    if (player === compchoice) { console.log ("A draw"); }
    

    etc…

    And see if you still have issues with how your logs are output.

    Hope this helps!

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