Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Code within an else if conditional is executing despite the condition being false

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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...");}
}

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading