Rock, Paper, Scissors Computer Choice part 2


#1


Rock, Paper, Scissors: Computer Choice part 2

The script recommended seems in error as a value of less than 0.33 for the var computerChoice could satisfy the criteria for both "rock" and "paper" associated values.


Codecademy recommended script from next excercise suggests this script:

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

Wouldn't the following script reduce confusion?

if (computerChoice <= 0.33) {
    console.log("rock");
}
else if (computerChoice >= 0.34 && computerChoice <= 0.67) {
    console.log("paper");
}
else {
    console.log("scissors");
}

I had to read elsewhere how to use the two ampersands. But, does this second script seem like a better solution?

Looking forward to everyone's input.

`


#2

no, i think this is a bad idea. Now, you don't have a variable called computerChoice, so now it is really difficult to compare userchoice with computerchoice and determine a winner.


#3

My apologies, I thought it would be understood that only the "IF" statement evaluating the var computerChoice was being referred to above. Of course that var must be created. Below is the entire script should you want to refer to it. I was hoping that this thread would promote comments regarding how to write a better else { } statement specifically.

var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();

if (computerChoice <= 0.33) {
console.log("rock");
}
else if (computerChoice >= 0.34 && computerChoice <= 0.67) {
console.log("paper");
}
else {
console.log("scissors");
}

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


#4

You can change that to

else if (computerChoice <= 0.67)

if/else statements are like filters. If your number gets caught in the first comparison it does not keep trying all the rest.


#5

Sorry, now i lost you. Could you explain? Let me explain something as well, you create a computerChoice here:

var computerChoice = Math.random();

now computerChoice is a variable holding a random number, agree? Then in the lessons code:

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

you use this number, to assign a actual rock, paper scissors string, which we can use to compare with the userChoice. In your code, you only log rock, paper or scissors to the console. Now computerChoice is still a random number. You could convert this later, but that is actually a bit impractical


#6

I see your point in terms of the change in value assigned to the var computerChoice, but I feel that you're again addressing something other than the reason why I started this thread.

Thanks for your feedback though.


#7

Ahhhh...... Got it. Thanks!! To be clear then, the order of conditions tested within an IF statement (and additional ELSE IF, ELSE statements) can influence the outcome. Am I correct in reading your explanation?


#8

Yes, it could. You can have fun testing in places like https://repl.it/languages/javascript_web -- paste your code in there and see what happens when you change things.


#9

@jnl2,

Maybe a diagram helps...

The construct IF ELSE-IF ELSE must be seen as one BIG-statement.

This BIG-statement will be left at the FIRST possibilty....
and this after executing the { code-block }


[input]
   |
   |
   v
   if (computerChoice < 0.34) {
            computerChoice = "rock";  - -> - - -
   }                                            |
   |                                            v
   v                                            | 
   |                                            |
   else if(computerChoice <= 0.67) {            |
   |        computerChoice = "paper"; - -> - - -
   }                                            |
   |                                            v
   v                                            |
   |                                            v
   else {                                       |
           computerChoice = "scissors";  - ->- -|
   }                                            |
                                                v
                                              [exit]