开始新游戏功能不工作

Start new game function not working

本文关键字:功能 工作 游戏 新游戏 开始      更新时间:2023-09-26

谁能帮帮我?我不知道我哪里出错了。主要的问题是我的开始新游戏功能,但我不知道我哪里出了问题。提前感谢。这是我的小提琴http://jsfiddle.net/Matt1990/jL7f3p39/25/

这是我的javascript
enter code here
var Array = ["monitor", "program", "keyboard", "gaming", "harddisk", "software",         "printer", "scanner", "firewall", "desktop", "system", "malware", "windows"];
var word;
var guessCount = 0;
var guess;
var input;
var wordLength;
var wordSubstring;
var currentWord;
function startGame() {
guess = "";
guessCount = 10;
word = Array[Math.floor(Math.random() * Array.length)];
currentWord = 0;
wordLength = word.length;
wordSubstring = currentWord.substring;
console.log(word);
var myElement = document.getElementById("button").innerHTML = "Click to guess";
    var myPicElement = document.getElementById("hangimage").src =                              "http://fetlar.kingston.ac.uk/pp/hangman10.jpg";
for (var count = 0; count < word.length; count++) {
    guess = guess + "-";
}
document.getElementById("guess").innerHTML = guess;
}
function guessLetter() {
var correct = 0;
var inputBox = document.getElementById("guessinput");
input = inputBox.value;

for (var count = 0; count < wordLength; count++) {
    if (input == word.substring(count, count + 1)) {
        correct++;
        guess = guess.substring(0, count) + input + guess.substring(count + 1,        guess.length + 1);
        document.getElementById("guess").innerHTML = guess;
    }
}
if (correct == 0) {
    guessCount--;
}
var url = document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman" + guessCount + ".jpg";
if (guess == word) {
    document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman_win.jpg";
    alert("You guessed the word correctly. You win!");
}

if (guess == 0 + word) {
    document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman0.jpg";
}
}
startGame();
document.getElementById("button").onclick = guessLetter;
function startNewGame(showMessage) {
guess = "";
guessCount = 10;
word = Array[Math.floor(Math.random() * Array.length)];
currentWord = 0;
wordLength = word.length;
wordSubstring = currentWord.substring;
console.log(word);
var myElement = document.getElementById("button").innerHTML = "Start New Game";
for (var count = 0; count < word.length; count++) {
    guess = guess + "-";

    document.getElementById("guess").innerHTML = guess;
}
}

这是我的HTML

<fieldset>
<div style="text-align: center;">
<h1>Hangman</h1>

<h2> Can you save the stick man? <h2>
<img id="hangimage" src="http://fetlar.kingston.ac.uk/pp/hangman9.jpg" />
<div id="guess" style="font-size: 2.5em; font-family: arial; color: red;">- - - - - - -    </div>
<input id="guessinput" type="text" size="2" />
<button id="button">Click to guess</button>
<br/>
<div>Hint:The word is a computer-related term</div>
<button id="button">Start New Game</button> 
</fieldset>    

如我所见

HTML

<button id="button2">Start New Game</button> 

JAVASCRIP

document.getElementById("button").onclick = guessLetter;
// add this
document.getElementById("button2").onclick = startNewGame;
http://jsfiddle.net/rnrlabs/jL7f3p39/30/