JavaScript石头剪刀布游戏计分功能异常问题求助
Hey there! Let's break down exactly why your score isn't updating and get your game working properly. I see a couple of key issues in your code that are causing those scores to stay stuck at 0.
1. Return Statements Are Blocking Your Score Updates
The biggest problem is that you're placing your return before you update the scores. As soon as a return runs, the function stops executing—so all those playerScore++, computerScore++, and draws++ lines never actually run!
For example, in this snippet:
else if (playerSelection === "paper") { return "You won!" playerScore++; };
The playerScore++ is dead code—it will never execute because the function exits when return runs. You need to swap those lines: update the score first, then return the message.
2. You're Reusing the Same Computer Selection for All Rounds
You only generate computerSelection once at the top of your code:
const computerSelection = computerPlay();
That means all 5 calls to playRound are using the exact same computer choice, not a new random one each round. You need to generate a new computer selection every time you call playRound.
3. Your Draw Counter Isn't Updating Either
Same issue as the scores: draws++ comes after return, so it never runs. Move that line before the return too.
Here's the Fixed Code
I've adjusted all the issues above, and added comments to highlight the changes:
function computerPlay() { let gameWords = ["Rock", "Paper", "Scissors"]; let randomWord = gameWords[Math.floor(Math.random() * gameWords.length)]; return randomWord; }; let playerScore = 0 let computerScore = 0 let draws = 0 function playRound(playerSelection, computerSelection) { playerSelection = playerSelection.toLowerCase(); if (computerSelection === "Rock") { if (playerSelection === "rock") { draws++; // Moved before return return "Draw again." } else if (playerSelection === "paper") { playerScore++; // Moved before return return "You won!" } else if (playerSelection === "scissors") { computerScore++; // Moved before return return "You lost!" }; }; if (computerSelection === "Paper") { if (playerSelection === "paper") { draws++; return "Draw again." } else if (playerSelection === "rock") { computerScore++; return "You lost..." } else if (playerSelection === "scissors") { playerScore++; return "You won!" }; }; if (computerSelection === "Scissors") { if (playerSelection === "scissors") { draws++; return "Draw again." } else if (playerSelection === "paper") { computerScore++; return "You lost..." } else if (playerSelection === "rock") { playerScore++; return "You won" }; }; }; // Generate a new computer selection for each round now console.log(playRound("RoCk", computerPlay())); console.log(playRound("Scissors", computerPlay())); console.log(playRound("PAper", computerPlay())); console.log(playRound("paper", computerPlay())); console.log(playRound("PAPER", computerPlay())); function gameScore() { if (playerScore > computerScore) { return "You won the game!"; } else if (playerScore < computerScore) { return "You lost, try again."; } else { return "It is a tie."; } } console.log(gameScore());
What Changed?
- All score/draw updates now happen before the return statement, so they actually execute
- Each
playRoundcall now usescomputerPlay()directly, so the computer picks a new random choice every round - Simplified the final
elseingameScore()(no need to checkplayerScore === computerScoreexplicitly since it's the only remaining case)
Now when you run this, your scores will update correctly each round, and gameScore() will return the proper result based on those scores.
内容的提问来源于stack exchange,提问作者StavAk

