Add one more "else if" in 9/9


#1

I just found out that we can add one more "else if" inside the compare function, so that if player key in a different answer that paper, scissors and rock, it will ask user to confirm "play again".

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("Computer: " + computerChoice);



compare=function(choice1, choice2){
    if(choice1===choice2){
        return "The result is a tie!";
    }
    
    else if(choice1==="rock"){
        if(choice2==="scissors"){
            return "rock wins";
        }
        else{
            return "paper wins"
        }
    }
    
    else if(choice1==="paper"){
        if(choice2==="rock"){
            return "paper wins";
        }
        else{
        return "scissors wins"
        }
    }
        
    else if(choice1==="scissors"){
        if(choice2==="paper"){
            return "scissors wins";
        }
        else{
        return "rock wins"
        }  
    }
    
    else if(choice1!=="rock", "paper", "scissors"){
            return "Let's play again!"
    }    
    
}

confirm(compare(userChoice,computerChoice));

Edit: Formatting fixed by haxor789


#2

Yes, but unfortunately not like this:

else if(choice1!=="rock", "paper", "scissors"){
        return "Let's play again!"
}

As you can read here:
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Comma_Operator

You can use the comma operator when you want to include multiple
expressions in a location that requires a single expression. The most
common usage of this operator is to supply multiple parameters in a for loop.

and here:

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

Your comma use of comma operators will result in "scissors" because choice1!=="rock", "paper", "scissors" are all just values. Now if you have a look at this:

http://www.sitepoint.com/javascript-truthy-falsy/

you find that non-empty strings are treated as truthy when used in conditions so your whole condition does basically boil down to: true no matter what choice1 was. Of course it works and also does what you expect it to do, as you basically use it as a "if non other case matched than choose this one" but in this case you're probably faster with an else or else if(true). Last but not least if you really want to cover all not RPS choices as you might want to extend it later and want to be more specific about wrong inputs, then you need to go the long way:

else if(choice1!=="rock" && choice1!=="paper" && choice1!=="scissors"){
        return "Let's play again!"
}

where && can be translated with AND (OR would be ||). Hope this helps more then it confuses :wink:


#3

Why can't it just be instead:
else {
return "Inappropriate choice-Play again";
}

By having just else { and NOT else if {
I am already saying that else { applies when choice 1 is not rock, not paper and not scissors.
Thanks!


#4

Because the function is a bigger chunk of code and your faster if you cover invalid inputs earlier. But as this is barely important at this point of learning coding your right using an else in this row is a pretty good idea for solving the problem.


#5

What do you mean by faster? What could happen if I don't cover invalid inputs earlier(Not in this exercise but as you said in cases where the function is bigger)?
Also, when I write only else and then I type in in the window any word other than rock, paper, scissors, it does not display "inappropriate choice" but
Computer: scissors
The result is a tie!
The result is a tie!
The result is a tie!


#6
  • the same happens when I use else if(choice1!=="rock" && choice1!=="paper" && choice1!=="scissors"){
    return "Let's play again!"
    }

#7

You probably need to post your whole code to see this.

Well then you go on with the function in this case you:

check for tie -> fail
check for rock -> fail
check for paper -> fail
check for scissors -> fail 
else -> check

so instead of 1 check at the very beginning you have 4 checks which takes 4 times more time to do it. Here it is all in the range of miliseconds and less so barely important but if you have a bigger game with loading screen and graphics and stuff that really costs time and ressources you might want to check if everything is correct before you fire your engines :smile:


#8
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("Computer: " + computerChoice);
var compare=function(choice1, choice2) {
    
if (choice1 === choice2) {
        return "The result is a tie!";
}
    
else if(choice1 === "rock") {
       
       if(choice2 === "scissors") {
       return "rock wins";
   }
   else {
       return "paper wins";
   }
    }
    
else if(choice1 === "paper") {
        if(choice2 === "rock")
           { return "paper wins";
           }
         else {
             return "scissors wins";
              }
         }
else if (choise1 === "scissors") {
    if(choice2 ==="rock") {
        return "rock wins";
    }
    else if(choice2 === "paper") {
        return "scissors wins";
    }
    
}
else {
    return "Invalid choice";
}
};
    
 
    console.log(compare('rock','rock'));
console.log(compare('scissor','scissor'));
console.log(compare('paper','paper'));

#9

Well you're inappropriate choice else comes last in the row and what comes first is

if (choice1 === choice2) {
        return "The result is a tie!";
}

and these:

    console.log(compare('rock','rock'));
console.log(compare('scissor','scissor'));
console.log(compare('paper','paper'));

do match this condition even if scissor lacks a s. So the output is what one would expect isn't it? Try e.g. "rope" vs "paper" to see if your inappropriate choice mechanisms work. And of course "you" are the only one to make an inappropriate choice so "paper" vs "rope" wont work. Otherwise you would need to change the nested elses to else ifs so that a computer choice of e.g. "rope" is not excepted. Also what you can do is to just return invalid choice at the very end without the else because the returns before already make sure that this point cannot be reached unless it was an invalid choice.