WHAT WORKED FOR ME Computer Choice part 2 (step 4)


#1

Below is what worked for me. The thing that had me hung up was the last "else" statement block. I was inputting "(computerChoice === 0.67 -1)". Once I deleted this from the last else statement, my code worked. I hope this helps.

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

var computerChoice = Math.random();

if (computerChoice === 0 - 0.33) {
    console.log ("rock");
}
else if (computerChoice === 0.33 - 0.66) {
    console.log ("paper");
}
else {
    console.log ("scissors");
}

#2

[update]Okay, after seeing the next lesson I see that the code below isn't what the course used in the step 5. BUT I did get a "GO" for the code below.

Below is what worked for me. The thing that had me hung up was the last "else" statement block. I was inputting "(computerChoice === 0.67 -1)". Once I deleted this from the last else statement, my code worked. I hope this helps.

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

var computerChoice = Math.random();

if (computerChoice === 0 - 0.33) {
    console.log ("rock");
}
else if (computerChoice === 0.33 - 0.66) {
    console.log ("paper");
}
else {
    console.log ("scissors");
}

[/quote]


#3

Ok first of all please do not post solutions. The exercises should be doable and if they aren't tell about it but do not post plain code solutions without explanation.

Also this code is not correct anyway. First of all the general idea is that you assign 1 of 3 strings to a variable randomly therefore you take a random number and based on this number you assign a string. So it would have been

computerChoice = "rock";

instead of:

console.log ("rock");

and the second thing is that these conditions doesn't work:

computerChoice === 0 - 0.33

The first and obvious problem is that === is not a good idea here as it checks one particular case. The problem is Math.random() provides you a huge amount of numbers between 0 and 1 (not included). So if the if deals with 1 case and the else if deals with 1 case you have 99,9999% of the cases dealt with by the else case. That's because else deals with everything that is not dealt with by one of the other cases, btw this is also the reason why it doesn't need a specific condition on it's own as you've discovered yourself.

The other problem is that - doesn't mean in between but subtraction so 0-0.33 is -0.33 which could never be reached by the Math.random command (same for 0.33-0.66) and therefore you should get "scissors" all of the time.

The overall idea is to divide the range from 0 to 1 in 3 parts but that doesn't necessarily require to do this with in between statements. You can and if you want maybe search for && and || (AND and OR boolean operators). But they're not introduced yet and you can do it without them. For example for the range from 0 to 0.33 do you really require to check if it is above 0? No you don't as Math.random already provides numbers in a certain range from 0 to 1 so just asking for below 0.33 would be sufficient. As well as for 0.33 -0.66 do you really need to check if it is below 0.33? No as the if already catches this case you only have to deal with numbers below 0.66 and else takes all the rest. Hope this makes it a little clearer. And again please don't post solutions without explanation.


#4

thanks for the clarification. this was really helpful. is there a reason why my bad code came back as good? because now im wondering how much of the other course stuff I completed in other work was actually wrong.


#5

Well as the output depends on a random number it is not that easy to check it (maybe statistics but running it 10.000 times takes some time). But at least the fact that it is a number and not a string should have raised a warning thanks for mentioning this.


#6

Gotcha! thanks again