[英]Math.random error
When I run the following code this error occurs: 当我运行以下代码时,会发生此错误:
Oops, try again.
糟糕,再试一次。 Did you use Math.random() to get a random number?
您是否使用Math.random()获得随机数?
Using: 使用方法:
declare a variable and make it equal to Math.random(), that variable will equal a number between 0 and 1
声明一个变量并使它等于Math.random(),该变量将等于0到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. OP指的是Codecademy上的Build Rock Paper Scissors练习2.4。
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. 我敢肯定,OP在CC论坛上找到了他的课程的答案已经很久了。 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 我认为OP问题的原始来源来自: 链接到任务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.