I did it in my way and I think it's much easier


#1

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


#2

okay, and why is that easier then this:

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

you don't need an else wrong choice, given you generate the random number, so it can't be wrong. If it was user input, you should validate it. Now you don't have to, since it is a random number you generate

the && operator hasn't been explained, and as you can see you don't need it


#3

I did in that way because it says the number should be between 0-0.33,0.34-0.66,067-1.


#4

so? if you do <= 0.66 it is fine, given .33 and lower will trigger your if clause. so then using 0.66 will select the right range, without using the and operator


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.