[英]Javascript if statement only returns true
我正在寫剪刀石頭布游戲。
播放器的console.log顯示所選值。
計算機的console.log顯示所選值。
即使控制台日志顯示它們是否不同,用於檢查值是否匹配的if語句也始終返回true。
我嘗試用幾種不同的方式編寫它,但總是會遇到這個問題。
// Player Choice Selection var player = function() { const playerSelects = "rock"; return playerSelects; }; console.log(player()); // Computer Choice Selection var computer = function() { const computerSelects = ['rock', 'paper', 'scissors']; let randomSelection = Math.floor(Math.random() * 3); return computerSelects[randomSelection]; }; console.log(computer()); // Game var game = function(player, computer) { if (player == computer) { const draw = "its a draw"; return draw; } }; console.log(game());
誰能告訴我我要怎么做?
謝謝
游戲是具有兩個參數的函數:
var game = function(player, computer){
if(player == computer){
因此,如果您將其稱為
game()
...如果不傳遞任何值,則player
和computer
都將是undefined
。可能這樣做:
game(computer(), player())
...但是您實際上不應該遮蓋這些變量名...
您沒有為game()
函數提供任何內容。
我認為這就是您想要的。
如前所述,由於命名的原因, player == computer
也不直觀。 人們不會期望玩家能像計算機一樣。 最好有一些類似playerValue == computerValue
東西。
// Player Choice Selection var player = function() { const playerSelects = "rock"; return playerSelects; }; // Computer Choice Selection var computer = function() { const computerSelects = ['rock', 'paper', 'scissors']; let randomSelection = Math.floor(Math.random() * 3); return computerSelects[randomSelection]; }; // Game var game = function(playerValue, computerValue) { if (playerValue === computerValue) { return "its a draw"; } else { return "It's not a draw"; } }; var playerValue = player(); var computerValue = computer(); console.log(playerValue); console.log(computerValue); console.log(game(playerValue, computerValue));
如果您不需要函數表達式,則最好使用常規函數,如下所示:
// Player Choice Selection function getPlayerChoice() { const playerSelects = "rock"; return playerSelects; }; // Computer Choice Selection function getComputerChoice() { const computerSelects = ['rock', 'paper', 'scissors']; let randomSelection = Math.floor(Math.random() * 3); return computerSelects[randomSelection]; }; // Game function playGame(playerChoice, computerChoice) { if (playerChoice === computerChoice) { return "its a draw"; } else { return "It's not a draw"; } }; var playerChoice = getPlayerChoice(); var computerChoice = getComputerChoice(); console.log(playerChoice); console.log(computerChoice); console.log(playGame(playerChoice, computerChoice));
您有幾處錯誤。 其他一些問題已部分解決,請允許我解釋一下:
// Player Choice Selection
var player = function() {
const playerSelects = "rock";
return playerSelects;
};
// Don't log the result here, because it will change in the game instance.
// Computer Choice Selection
var computer = function() {
const computerSelects = ['rock', 'paper', 'scissors'];
let randomSelection = Math.floor(Math.random() * 3);
return computerSelects[randomSelection];
};
// Don't log the result here, because it will change in the game instance.
// Game
// Don't pass the player and computer functions in, because they're in global scope already.
var game = function() {
// Call an instance of the player, and computer functions.
// We do this so that each game we play, has new results.
var playerResult = player();
var computerResult = computer();
// Log each result
console.log(playerResult, computerResult)
if (playerResult == computerResult) {
const draw = "its a draw";
return draw;
}
};
console.log(game());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.