石头剪刀布游戏中判断值相等的if语句恒返回true问题求助
Hey there! Let's figure out why your Rock-Paper-Scissors game's match check is always returning true, even when the console shows different choices. Based on what you've shared, here are the most common culprits and fixes:
First, Check for the "Single Equals" Mistake
This is the #1 gotcha for this kind of issue. If you accidentally used a single equals sign (=) instead of a comparison operator (== or ===) in your if statement, you're doing an assignment instead of checking equality.
For example:
// ❌ Wrong: This assigns computer's choice to player's choice, and the expression always evaluates to true (since non-empty strings are truthy) if (player() = computer()) { console.log("Match!"); } // ✅ Correct: Uses strict equality to compare the actual values if (player() === computer()) { console.log("It's a tie!"); }
Make Sure You're Comparing the Right Values
You mentioned console.log shows the correct values, so double-check that your if statement is using the same values you're logging. A common mistake is forgetting to call your choice functions (if they're set up like your player() function):
// ❌ Wrong: Comparing the function references themselves, not their returned values const player = function() { return "rock"; }; const computer = function() { return "paper"; }; if (player === computer) { console.log("Match!"); // This won't work—functions are different references } // ✅ Correct: Call the functions to get their actual choice values const playerChoice = player(); const computerChoice = computer(); console.log(playerChoice, computerChoice); // Confirm these are the values you expect if (playerChoice === computerChoice) { console.log("It's a tie!"); }
Verify Your Computer Choice Logic
Even if your console logs look right, double-check that your computer's choice function isn't secretly returning the same value as the player every time. For example, if you accidentally hardcoded "rock" for the computer too, that would explain the constant matches (but you said logs show different values, so this is less likely—still worth a quick check!).
Example of Working Logic
Here's a quick, fixed version of your code to reference:
// Player choice function (fixed for now—you can expand this to user input later) function player() { const playerSelects = "rock"; return playerSelects; } // Computer random choice function function computer() { const choices = ["rock", "paper", "scissors"]; const randomPick = Math.floor(Math.random() * choices.length); return choices[randomPick]; } // Get both choices const playerPick = player(); const computerPick = computer(); // Log to confirm values console.log(`Player chose: ${playerPick}`); console.log(`Computer chose: ${computerPick}`); // Proper match check if (playerPick === computerPick) { console.log("It's a tie!"); } else { // Add your win/loss logic here }
If you're still stuck, share the full code for your computer choice logic and your if statement—we can dig deeper!
内容的提问来源于stack exchange,提问作者Cool Spot

