When I run the following code this error occurs:
Oops, try again. Did you use Math.random() to get a random number?
Using:
declare a variable and make it equal to Math.random(), that variable will equal a number between 0 and 1
var userChoice = prompt("Do you choose rock,paper or scissors ?");
var computerChoice = Math.random();
console.log (computerChoice);
if (computerChoice < 0.29) {
computerChoice = 'rock';
} else if (computerChoice > 0.30 && computerChoice < 0.60) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
I was looking up something completely different and stumbled across this question.
The OP was referring to exercise 2.4 of Build Rock Paper Scissors on Codecademy.
I believe the code he was looking for was:
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";
}
var compare = function(choice1, choice2){
if (choice1 == choice2){
return("The result is a tie!");
}
if (choice1 == "rock"){
if (choice2 == "paper"){
return("paper wins");
} else {
return("rock wins");
}
}
if (choice1 == "paper"){
if (choice2 == "rock"){
return("paper wins");
} else {
return("scissors wins");
}
}
if (choice1 == "scissors"){
if (choice2 == "rock"){
return("rock wins");
} else {
return("scissors wins");
}
}
};
compare (userChoice, computerChoice);
I'm sure the OP has long since found the answer to his lesson in the CC forum. But I thought I'd answer it in case anyone else doing the lesson, came here, saw the post and needed an answer.
Original Source of OP question I believe comes from: Link to Task
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.