Javascript 更多的石头剪刀
我刚刚开始,我在构建RPS程序时遇到了一些问题。我应该遵循隐藏注释中提供的说明,我只是不明白我应该如何使用getPlayerMovemove和getComputerMovemove这两个函数 如果加载到index.html并通过浏览器中的控制台播放,我可以让程序正常工作,但如果使用repl,则无法正常工作Javascript 更多的石头剪刀,javascript,Javascript,我刚刚开始,我在构建RPS程序时遇到了一些问题。我应该遵循隐藏注释中提供的说明,我只是不明白我应该如何使用getPlayerMovemove和getComputerMovemove这两个函数 如果加载到index.html并通过浏览器中的控制台播放,我可以让程序正常工作,但如果使用repl,则无法正常工作 在这里,您可以定义您所说的getWinner函数 var getWinner = function(playerMove, computerMove) { ... } 这将在浏览器中工作,因
在这里,您可以定义您所说的getWinner函数
var getWinner = function(playerMove, computerMove) { ... }
这将在浏览器中工作,因为变量的默认范围是窗口对象。因此,在定义的上下文之外声明的变量被分配给窗口对象
当在这样的终端中运行时,我不确定默认的作用域是什么,或者是否存在这样的作用域,这样函数在声明之后可能就不存在了
对我有效的解决方案是将上述行更改为
function getWinner(playerMove, computerMove) { ... }
希望这对你有用。我会尝试找到更多的信息,当我对发生的事情有更具体的解释时,我会更新
编辑:
了解有关函数声明和函数表达式之间的差异的更多信息
在代码中设置变量getWinner的时间比调用时晚。在调用它之前,尝试移动它的声明。我已经让它在这里工作,但现在只需使用提示对部分进行微调 玩家的移动现在是硬编码的-你现在需要做的就是通过提示功能得到这个,一切正常
// ////////////////////////////////////////////////
/* Provided Code - Please Don't Edit */
/* IT WORKS!!! ///////////// */
//////////////////////////////////////////////////
'use strict';
rockPaperScissors();
function randomPlay() {
var randomNumber = Math.random();
if (randomNumber < 0.33) {
return "rock";
} else if (randomNumber < 0.66) {
return "paper";
} else {
return "scissors";
}
}
////////////////////////////////////////////////
/* Write Your Code Below */
////////////////////////////////////////////////
var playerMove;
var computerMove;
var winner;
function getComputerMove() {
var move = randomPlay();
if (move === null || 0) {
randomPlay();
} else {
console.log("The computer chose " + move);
computerMove = move;
}
}
function getPlayerMove() {
// Write an expression that operates on a variable called `move`
//////////////////// The PLAYER move is hard coded in the line below this one///////////////////////////
var move = 'scissors';
console.log("The player chose " + move);
playerMove = move;
}
function rockPaperScissors() {
console.log("Let's play Rock, Paper, Scissors.");
getComputerMove();
getPlayerMove();
getWinner();
}
function getWinner() {
var winner;
if ((computerMove === 'rock') && (playerMove === 'rock')) {
winner = "It's a tie!";
} else if ((computerMove === 'scissors') && (playerMove === 'rock')) {
winner = "Player. Congratulations you Win!!!";
} else if ((computerMove === 'paper') && (playerMove === 'rock')) {
winner = "The computer. You lose bitch!";
} else if ((computerMove === 'rock') && (playerMove === 'scissors')) {
winner = "The computer. You lose bitch!";
} else if ((computerMove === 'scissors') && (playerMove === 'scissors')) {
winner = "It's a tie!";
} else if ((computerMove === 'paper') && (playerMove === 'scissors')) {
winner = "The player. You win!";
}else if ((computerMove === 'rock') && (playerMove === 'paper')) {
winner = "The player. You win!";
} else if ((computerMove === 'scissors') && (playerMove === 'paper')) {
winner = "The computer. You lose bitch!";
} else if ((computerMove === 'paper') && (playerMove === 'paper')) {
winner = "It's a tie!";
}
console.log("The winner is: " + winner);
}
请更新您的问题,了解更多问题的细节……看起来伊西克的解释是有效的。我试着把函数声明移到调用函数的地方,但这似乎没有什么区别,我想它会把函数引入的地方移到另一个函数调用的地方,但我可能错了。谢谢你,谢谢你提供的参考资料。现在更合理了
// ////////////////////////////////////////////////
/* Provided Code - Please Don't Edit */
/* IT WORKS!!! ///////////// */
//////////////////////////////////////////////////
'use strict';
rockPaperScissors();
function randomPlay() {
var randomNumber = Math.random();
if (randomNumber < 0.33) {
return "rock";
} else if (randomNumber < 0.66) {
return "paper";
} else {
return "scissors";
}
}
////////////////////////////////////////////////
/* Write Your Code Below */
////////////////////////////////////////////////
var playerMove;
var computerMove;
var winner;
function getComputerMove() {
var move = randomPlay();
if (move === null || 0) {
randomPlay();
} else {
console.log("The computer chose " + move);
computerMove = move;
}
}
function getPlayerMove() {
// Write an expression that operates on a variable called `move`
//////////////////// The PLAYER move is hard coded in the line below this one///////////////////////////
var move = 'scissors';
console.log("The player chose " + move);
playerMove = move;
}
function rockPaperScissors() {
console.log("Let's play Rock, Paper, Scissors.");
getComputerMove();
getPlayerMove();
getWinner();
}
function getWinner() {
var winner;
if ((computerMove === 'rock') && (playerMove === 'rock')) {
winner = "It's a tie!";
} else if ((computerMove === 'scissors') && (playerMove === 'rock')) {
winner = "Player. Congratulations you Win!!!";
} else if ((computerMove === 'paper') && (playerMove === 'rock')) {
winner = "The computer. You lose bitch!";
} else if ((computerMove === 'rock') && (playerMove === 'scissors')) {
winner = "The computer. You lose bitch!";
} else if ((computerMove === 'scissors') && (playerMove === 'scissors')) {
winner = "It's a tie!";
} else if ((computerMove === 'paper') && (playerMove === 'scissors')) {
winner = "The player. You win!";
}else if ((computerMove === 'rock') && (playerMove === 'paper')) {
winner = "The player. You win!";
} else if ((computerMove === 'scissors') && (playerMove === 'paper')) {
winner = "The computer. You lose bitch!";
} else if ((computerMove === 'paper') && (playerMove === 'paper')) {
winner = "It's a tie!";
}
console.log("The winner is: " + winner);
}