Programming help
I don't understand this code. Could anyone help me understand what each line of code does?
*This is from a book but the book just confuses me
!DOCTYPE html>
<html>
<head>
<title>Hangman</title>
</head>
<body style="background-color:pink;">
<script>
var words = ["venture", "exaggerate", "guilt", "agent", "love", "happy", "film", "waist", "weeds", "movies", "clap", "acting", "lion", "programming", "family", "drop", "rainbow", "sprinkles", "computer", "chair", "theater", "blanket", "cookies", "guests", "television", "rabbit", "tornado", "house", "popcorn"];
var word = words[Math.floor(Math.random() * words.length)];
var answerArray = [];
for (var i = 0; i < word.length; i++) {
answerArray= "_";
}
var remainingLetters = word.length;
// ******************** THE MAIN GAME LOOP ********************
while (remainingLetters > 0) {
alert(answerArray.join(" "));
var guess = prompt("Guess the letter or Click cancel to stop playing");
if (guess == null) {
break;
} else if (guess.length !== 1) {
// Alert them to guess only one letter
alert("Please enter a single letter");
}
else {
for (var j = 0; j < word.length; j++) {
if (word[j] === guess) {
answerArray[j] = guess;
remainingLetters--;
}
}
}
// ******************** END OF GAME LOOP ********************
}
alert(answerArray.join(" "));
alert("Good job! If you guessed the word...That's great! if not don't worry you will get the HANG of it. The word was " + word);
</script>
</body>
</html>
Iniciar sesión para dejar un comentario.