通过创建石头剪刀布游戏回顾 DOM 操作 - 步骤4

告诉我们发生了什么:

in my code:
if this is wrong:
playerScoreSpanElement.innerText = playerScore;
computerScoreSpanElement.innerText = computerScore;
roundResultsMsg.innerText = getRoundResults(userOption);
but this is right?:
roundResultsMsg.innerText = getRoundResults(userOption);
playerScoreSpanElement.innerText = playerScore;
computerScoreSpanElement.innerText = computerScore;

到目前为止你的代码

<!-- file: index.html -->

/* file: script.js */
// User Editable Region

const playerScoreSpanElement = document.getElementById("player-score");
const computerScoreSpanElement = document.getElementById("computer-score");
const roundResultsMsg = document.getElementById("results-msg");

function showResults(userOption) {
  playerScoreSpanElement.innerText = playerScore;
  computerScoreSpanElement.innerText = computerScore;
  roundResultsMsg.innerText = getRoundResults(userOption);
};

showResults("Rock");

// User Editable Region
/* file: styles.css */

你的浏览器信息:

用户代理是: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

挑战信息:

通过创建石头剪刀布游戏回顾 DOM 操作 - 步骤4

Hi @chenhaian

The getRoundResults function call updates the score, then the text for the two scores is updated.

Otherwise, if the scores are updated before the function call, they do not change.

Happy coding

but in this code ,function getRoundResults just update to the innerText , if don’t need to update roundResultsMsg.innerText , function getRoundResults will not be execute?

Updating

by calling the getRoundResults function, updates the roundResultsMsg element.

Remember, that playerScore and computerScore are global variables, so when the function is called, those variables are also updated.

Which is why the function call needs to go as the first line in the showResults function.

thanks a lot , i gotta it!

1 Like