[英]Mechanics of the Javascript rock, paper, scissors game
Okay so something has been bugging me and I haven't been able to find the answer. 好吧,这一直困扰着我,而我一直找不到答案。 So here's Codeacademy's rock, paper, scissors game: 所以这是Codeacademy的石头,纸,剪刀游戏:
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";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2){
if(choice1 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
else if(choice1 === "paper") {
if(choice2 === "rock") {
return "paper wins"
}
else {
return "scissors wins"
}
}
else if(choice1 === "scissors") {
if(choice2 === "rock") {
return "rock wins"
}
else {
return "scissors wins"
}
}
};
compare(userChoice, computerChoice);
Now what I am wondering is, is how does the computer know what choice1 and choice2 is when it uses it for the if/else loop that determines the winner? 现在我想知道的是,当计算机将它用于确定赢家的if / else循环时,计算机如何知道choice1和choice2是什么? Is it declared on the userChoice and computerChoice functions and if so how? 它是在userChoice和computerChoice函数上声明的,如果是的话,如何声明? Links to any documentation would be helpful. 链接到任何文档将很有帮助。 I am just trying to understand how exactly JavaScript works. 我只是想了解JavaScript的工作原理。 Thank you! 谢谢!
compare
is a function that takes choice1
and choice2
as its arguments. compare
是一个以choice1
和choice2
为参数的函数。 Their values are determined when the function is called. 它们的值在调用函数时确定。 For example, if you have 2 variables foo
and bar
containing strings like this: 例如,如果您有两个包含以下字符串的变量foo
和bar
:
var foo = 'rock';
var bar = 'paper';
and you call compare
passing those 2 variables, like so: 然后调用compare
传递这两个变量,如下所示:
compare(foo, bar);
then, inside compare
, choice1
will be 'foo'
and choice2
will be 'bar'
. 然后,在compare
, choice1
将为'foo'
而choice2
将为'bar'
。
For strings, this is exactly the same as calling compare
like this: 对于字符串,这与调用compare
完全相同,如下所示:
compare('rock', 'paper');
If you look at the rest of the codecademy example you will see a call to compare
further down. 如果您查看本代码学院示例的其余部分,则会看到一个调用,以compare
进一步的compare
。
choice1
and choice2
are arguments of the function compare()
, so they take the value you pass to the function when you call it. choice1
和choice2
是函数compare()
,因此它们采用您在调用函数时传递给函数的值。
You call the compare function at the end of your script, using compare(userChoice, computerChoice);
您可以使用compare(userChoice, computerChoice);
在脚本末尾调用compare函数compare(userChoice, computerChoice);
. 。 So choice1
will have value of userChoice
and choice2
will have value of computerChoice
. 因此, choice1
将拥有的价值userChoice
和choice2
将拥有的价值computerChoice
。
computerChoice
is determined random at the start, and userChoice
by the user input 在开始时确定computerChoice
是随机的,而userChoice
由用户输入确定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.