我正在创建一个通灵游戏,当我在Google Chrome浏览器中检查代码并键入ID时,我会看到总的赢/输以及剩下的猜测数。但是,我在浏览器中看不到结果。我认为我缺少了一些东西,似乎无法隔离实际问题。
我不需要伪代码行,删除了可能是问题的代码行。在互联网上搜索此问题。 document.elementByID()。innerHTML我所有的if / else语句。
var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var losses = 0;
var wins = 0;
var guessesLeft = 10;
var guessesSoFar = []; // capture user input
var psychicGuess;
document.onkeypress = function(event) {
var userChoice = event.key;
guessesSoFar.push(userChoice);
var psychicGuess = letters[Math.floor(Math.random() * letters.length)];
if (userChoice === psychicGuess) {
wins++;
guessesLeft = 10;
guessesSoFar = [];
} else if (guessesLeft == 0) {
losses++;
guessesLeft = 10;
guessesSoFar = [];
}
if (userChoice !== psychicGuess) {
guessesLeft--;
}
}
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Psychic Game</title>
<link rel="stylesheet" type="text/css" href="assets/css/reset.css">
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
</head>
<body>
<div class="game">
<h1>Psychic Game</h1>
<br>
<p> Guess what letters I'm thinking of:</p>
<br>
<p id="wins">Wins:</p>
<br>
<p id='losses'>Losses:</p>
<br>
<p id="left">Guesses Left: 10</p>
<br>
<p id="guesses">Guesses:</p>
</div>
<script src="assets/javascript/game.js"></script>
</body>
</html>
检查->当我输入获胜/失败和猜测时,控制台会产生产品结果。