Javascript -1); }否则{ 让newString=document.getElementById('words').innerHTML.split(''),indexesOfALetter=[]; for(设i=0;inewString[i]=letter); u
-1); }否则{ 让newString=document.getElementById('words').innerHTML.split(''),indexesOfALetter=[]; for(设i=0;iJavascript -1); }否则{ 让newString=document.getElementById('words').innerHTML.split(''),indexesOfALetter=[]; for(设i=0;inewString[i]=letter); u,javascript,onclick,event-handling,Javascript,Onclick,Event Handling,-1); }否则{ 让newString=document.getElementById('words').innerHTML.split(''),indexesOfALetter=[]; for(设i=0;inewString[i]=letter); updateWords(newString.join(“”)); } if(!/\*/.test(document.getElementById('words').innerHTML)){ hideAlphabet(); setGameStatu
正文{
保证金:0自动;
宽度:1000px;
字体大小:20px;
颜色:黑色;
}
#标题{
利润率:20px;
}
#字母表{
显示:无;
利润率:20px;
}
钮扣{
边框:2件纯蓝;
字体大小:20px;
}
#言语{
字体大小:30px;
利润率:10px;
字母间距:20px;
}
#玩{
利润率:20px;
}
欢迎来到命运之轮
您还有0个猜测
玩
A.
B
C
D
E
F
G
H
我
J
K
L
M
N
O
P
Q
R
s
T
U
v
W
X
Y
Z
谢谢。我尝试添加e.toElement.textContent,但它只是显示我单击了播放按钮。谢谢你。它确实可以工作,但代码对我来说有点高级,所以我将尝试理解它。我真的只是想使用我现在的代码来进入一个步骤,在这个步骤中,当用户猜到正确的答案时,我会得到用户单击显示的字母。这是一个课堂项目,我不想复制整个内容,否则我不会自己学习。不过我非常感谢你的帮助,你这么快就完成了!如果你有问题,我很乐意回答。祝您有个美好的一天!是的,我正在尝试找出如何让我的代码在页面上的u中显示匹配项。仅此而已。我知道你在ES6中使用了你的代码,但我不明白那里发生了什么。你需要找到单词中的每个字母-我用了来。然后,您需要从前面步骤中创建的数组中获取每个索引。我用了forEach
。为了缩小这部分代码,我使用了-这里简单的函数和函数没有什么不同,但它使用的代码更少。我们正在获取数字(它是来自chosenWord
)的索引,并用这些索引替换隐藏单词中的符号,以显示单击的字母。
<DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Wheel of Fortune</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="main.css">
</head>
<body>
<body>
<div id="container">
<div id="header">
<p class="welcome">Welcome to Wheel of Fortune!</p>
<!-- The letters will be tied to the number of letters in the random array -->
<p class="letters">There are <span id="numbers">0</span> letters in this word</p>
<!-- The user will be given a number of choices and with each wrong choice, they lose a turn -->
<p class="lives">You have <span id="guesses">0</span> guesses left</p>
<span id="words"></span><br>
<button id="play" onclick="playGame()">Play</button>
</div>
<div id="alphabet" onclick="buttonPress(event)">
<button>A</button>
<button>B</button>
<button>C</button>
<button>D</button>
<button>E</button>
<button>F</button>
<button>G</button>
<button>H</button>
<button>I</button>
<button>J</button>
<button>K</button>
<button>L</button>
<button>M</button>
<br>
<button>N</button>
<button>O</button>
<button>P</button>
<button>Q</button>
<button>R</button>
<button>S</button>
<button>T</button>
<button>U</button>
<button>V</button>
<button>W</button>
<button>X</button>
<button>Y</button>
<button>Z</button>
</div>
<span id="your-guess">You guessed </span>
</div>
<script src="main.js"></script>
</body>
</html>
body {
margin: 0 auto;
width: 1000px;
font-size: 20px;
background: url("http://res.cloudinary.com/angelrodriguez/image/upload/c_crop,h_624/v1534810805/wheeloffortune.jpg");
background-size: cover;
color: white;
}
#header {
margin: 20px;
}
#alphabet {
margin: 20px;
}
button {
border: 2px solid blue;
font-size: 20px;
}
#words {
font-size: 30px;
margin: 10px;
letter-spacing: 20px;
}
#play {
margin: 20px;
}
// 3. Quit the game if the player wants to.
// 5. Keep track of letters the player has guessed.
// 6. Show the player their progress.
// 7. Finish when the player has guessed the word.
// Create global variables
let gameOver = false;
var guessesLeft = 6;
var letterClicked;
var wordArray = ["JAVASCRIPT", "ARRAYS", "FUNCTIONS", "HOISTING", "RECURSION", "EVENTS", "KEYUP", "TERNARY"];
// 1. Pick a random word.
//Start a new game
function playGame() {
newGame.addEventListener("click", function() {
//fire the randomWord function to generate a new word
randomWord();
})
}
// Pass the letter event from buttonPress into the randomWord function
function randomWord(letter) {
var answerList = [];
// console.log(answerList);
// letterGuessed = "";
console.log(letterClicked);
var wordChoice = wordArray[Math.floor(Math.random() * wordArray.length)];tbw
var wordSplit = wordChoice.split('');
// console.log(wordSplit);
for (var i = 0; i < wordSplit.length; i++) {
answerList[i] = "_";
}
// if the letter is in the word
// Update the players progress with the guess
for (var z = 0; z < wordChoice.length; z++) {
if (wordChoice[z] === letterClicked) {
letterClicked = answerList[i];
// answerList[i].innerHTML = letterGuessed;
}
}
//Display underscores on page representing each word in the random word
wordDisplay.innerHTML = answerList;
//Display number of letters in the random word on the page
var remainingLetters = wordChoice.length;
letterCount.innerHTML = "The word is " + remainingLetters +
" letters long";
}
// 2. Take the player’s guess.
function buttonPress(e) {
letterClicked = e.target.textContent;
document.getElementById("your-guess").innerHTML = "You guessed the letter " + letterClicked;
//fix issue with clicking divs
}
// If the player wants to quite the game {
// Quit the game
// }
// Grab elements
var numbers = document.querySelector("#numbers");
var guesses = document.querySelector("#guesses");
var wordDisplay = document.querySelector("#words");
var letterCount = document.querySelector(".letters");
var newGame = document.querySelector("#play");
var letterBoxes = document.querySelector("#alphabet");