Not sure if this is right


#1

var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log (userChoice);
var computerChoice = Math.random();
console.log (computerChoice);
if (0 > computerChoice >= .33) {
    console.log ("rock")
} else if (.34 > computerChoice >= .66){
    console.log ("paper")
} else if (.67 > computerChoice >= 1) {
    console.log ("scissors")
};

This is the code i Used at it worked but I feel like it shouldn't have


#2

I can see how this would work in theory yes. This is a possibility that works in JavaScript but I'm assuming the way it was expecting you to do it was more along the lines of:

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

The reason you don't need to put anything before computerChoice is because it was covered earlier. In an if-Else if-else statement, it'll run from first to last. So if something is true in two else ifs in an if-else if statement line then the FIRST else-if or if will be run, but not the second one.

If that doesn't make sense please let me know and I'll go into greater detail.

--EDIT--

This code will only work in exercise "4. Computer Choice: Part 2"


#3

This kind of makes sense to me but I was thinking along the lines of the computer thinking the first is true then it doesn't need to run the rest of the code, that was my worry.


#4

Right, so whenever you want to have several if statements run through their code with the same statement you can just declare them as if statements.

But in this case it's best to have an "else if" because there'll be no reason for the code to run in the else if statement if it's true in an if statement that's before it.


#5

I just reread your code and realized what you did, that makes sense now. Is my code wrong or is it just overcomplicating a simple task?


#6

Overcomplicated. Also I'd suggest you add a " ; " semi-colon to the end of your console.log statements, that way when you're adding more code to those if statements you don't start running into errors.


#7

Some further input:
The task here is to reassign computerChoice with a new value so rather use computerChoice = "rock" instead of just console.log it. And a word to this one:

else if (.67 > computerChoice >= 1) {

This kind of condition seems tempting but unfortunately this is not how it works: If you want to chain two condition you need to use the && (AND) or || (OR) operators like for example this:

.67 > computerChoice  && computerChoice >= 1

Btw as I just see it the whole condition doesn't make this much sense, does it? ComputerChoice being both bigger than 1 and smaller than 0.67? But as @lolman already suggested their is an even more simple way to do it.

But back to the value < variable < value conditions the problem is that the computer does not understand them and just works through them from left to right so:

.67 > computerChoice >= 1
(.67 > computerChoice) >= 1
(true/false) >= 1
true/false

where the last becomes true/false because true and false have numerical values of 0 and 1.