Confused as to how I can start a new condition with an if


#1



https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#


It works... but I can't seem to finish the exercise. I know you need to use the following format of...

if{
}
else if{
if{
}
else{
}
}

But how would I do that? I would normally handle all the conditions with AND's and be done with it. But I really want to know how to nest these conditions.

So I can sum up my problems into two different issues:

1.) When I input paper, and the computer chooses rock, scissors wins. That's because its not getting past the else "scissors wins" condition.

2.) The output is expecting 'undefined' instead of the outcome of the game? That doesn't make any sense. Perhaps I am not understanding how to output the result? However, it comes out fine in the console but the exercise doesn't like it for some reason.


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} 
else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} 
else {
	computerChoice = "scissors";
} 

console.log("User: " + userChoice);
console.log("Computer: " + computerChoice);

var compare = function(userChoice, computerChoice){
    if( userChoice == computerChoice ){
        return "The result is a tie!";
    }
    
    else if( userChoice == "rock" ){
        if( computerChoice == "scissors" ){
            return "rock wins";
        }
        else{
            return "paper wins";
        }
    }
    
    else if( userChoice == "rock" ){
        return "paper wins";
    }
    
    else{
            return "scissors wins";
    }
    
    if( userChoice == "scissors"){
        if( computerChoice == "rock" ){
            return "rock wins";
        }
        else if( computerChoice == "paper"){ 
            return "scissors wins";
        }
    }
    
    if( userChoice == "paper"){
        if( computerChoice == "scissors" ){
            return "scissors wins";
        }
        else if( computerChoice == "rock" ){
            return "paper wins";
        }
    }
}

compare(userChoice,computerChoice);

Thanks, any help you can provide would be great.


#2

you start by using the right pattern:

 else if( userChoice == "rock" ){
        if( computerChoice == "scissors" ){
            return "rock wins";
        }
        else{
            return "paper wins";
        }
    }

then you have these lines:

else if( userChoice == "rock" ){
        return "paper wins";
    }
    
    else{
            return "scissors wins";
    }

you don't need them. What purpose do they serve?


#3

The funny thing is, the previous exercise said that, that syntax was OK and it let me get through to the 8th exercise which I am on now.

But thank you for revising that, I will add it to the exercise.

Here's what I was talking about from Exercise 7... that code you mentioned which was 'unnecessary' was flagged as OK by the exercise.


#4

the exercise rely on regular expressions, sometimes something slips through the cracks. The endlessly balance between freedom (so you can have some creativity in the lesson) and only allowing an exact match (by exact i mean: till the letter)


#5

Entirely understandable, I just wanted to make sure you knew where I was coming from.

Thanks