js写的猜单词的小游戏,先默认几组单词,然后根据提示,猜出是哪个单词,然后拼写出来,代码如下
-
<!DOCTYPE html>
-
<html>
-
<head>
-
<title>Hangman</title>
-
</head>
-
<body>
-
<h1>Hangman</h1>
-
<p>猜单词</p>
-
<div id="show"><div>
-
<script>
-
var words = [
-
"javascript",
-
"monkey",
-
"amazing",
-
"pancake"
-
];
-
var showText = '';
-
for(var x = 0;x < words.length;x++){
-
showText =showText + words[x]+'
';
-
console.log(showText);
-
-
}
-
var shows = document.getElementById("show");
-
shows.innerHTML = showText;
-
var word = words[Math.floor(Math.random()*words.length)];
-
var answerArray = [];
-
for(var i=0;i<word.length;i++){
-
answerArray[i] = "_";
-
}
-
var remainingLetters = word.length;
-
while(remainingLetters > 0){
-
alert(answerArray.join(" "));
-
-
var guess = prompt("Guess a letter,or click Cancel to stop playing.");
-
if(guess === null){
-
break;
-
}else if(guess.length !== 1){
-
alert("Please enter a single letter.");
-
}else{
-
for(var j =0;j<word.length;j++){
-
if(word[j] === guess){
-
answerArray[j] = guess;
-
remainingLetters--;
-
}
-
}
-
}
-
}
-
alert(answerArray.join(" "));
-
alert("Good job! The answer was " + word);
-
</script>
-
</body>
-
</html>
阅读(1061) | 评论(0) | 转发(0) |