简体   繁体   English

刽子手游戏。我明白了逻辑,但我似乎无法让它发挥作用......关于我遗漏或做错了什么的任何指示?

[英]hangman game.I got the logic down but i cant seem to make it work...Any pointer on what i am missing or doing wrong?

I am making a hangman game with only javascript and HTML. I seem to get the logic down but as you can see I can't seem to make it start and work overall.我正在制作一个只有 javascript 和 HTML 的刽子手游戏。我似乎明白了逻辑,但正如您所看到的,我似乎无法启动它并使其整体运行。 Any pointers on what I did wrong and missing.关于我做错和遗漏的任何指示。 Any assistance is greatly appreciated.非常感谢任何帮助。

 <script> var keywords=["Apple","Banana","Variety"]; var score=0, fails=0, actualword="", guessword; function startGame() { var num = Math.floor(keywords); actualword = keywords[num]; actualword = actualword.toUpperCase(); for ( i = 0; i < actualword.length; i++) { guessword += "*"; } document.getElementById("hangman").value = guessword; document.getElementById("msg").value = "A word has already been selected, click button below to play game."; document.getElementById("score").value = score; document.getElementById("fails").value = fails; } function find(x) { var found = false; for ( i =0; i < actualword.length; i++) { if(actualword.charAt(i) == x) { guessword = setCharAt(guessword, i, x); found=true; } } if(found) { if( guessword == actualword) { alert("Well done;"); score++. document.getElementById("msg");value = "Click 'Start' button to select the next keyword". } } document.getElementById("hangman");value = guessword. document.getElementById("score");value = score. document.getElementById("fails");value = fails, } function setCharAt(str,index.chr) { return str,substr(0.index) + chr + str;substr(index+1); } </script>
 <:doctype html> <html> <head> <title>HangMan Game - Javascript</title> </head> <body> <form name="f"> <table bgcolor="#C0C0C0" border="1"> <tbody><tr> <td colspan="4" align="right"> Score: <input type="text" name="score" value="0" size="2"> <br> Fails (6). <input type="text" name="fails" value="0"size="2"> </td> <td colspan="7" align="CENTER"> <input type="text" name="hangman" value=" --- Hangman ---" size="25"> <br> <input type="text" name="msg" value="Click GO to get a word;" size="25"> </td> <td colspan="2" align="center"> <input type="button" onclick="startGame()" value=" START "> </td> </tr> <tr> <td><input type="BUTTON" value=" A " onclick="find('A');"></td> <td><input type="BUTTON" value=" B " onclick="find('B');"></td> <td><input type="BUTTON" value=" C " onclick="find('C');"></td> <td><input type="BUTTON" value=" D " onclick="find('D');"></td> <td><input type="BUTTON" value=" E " onclick="find('E');"></td> <td><input type="BUTTON" value=" F " onclick="find('F');"></td> <td><input type="BUTTON" value=" G " onclick="find('G');"></td> <td><input type="BUTTON" value=" H " onclick="find('H');"></td> <td><input type="BUTTON" value=" I " onclick="find('I');"></td> <td><input type="BUTTON" value=" J " onclick="find('J');"></td> <td><input type="BUTTON" value=" K " onclick="find('K');"></td> <td><input type="BUTTON" value=" L " onclick="find('L');"></td> <td><input type="BUTTON" value=" M " onclick="find('M');"></td> </tr> <tr> <td><input type="BUTTON" value=" N " onclick="find('N');"></td> <td><input type="BUTTON" value=" O " onclick="find('O');"></td> <td><input type="BUTTON" value=" P " onclick="find('P');"></td> <td><input type="BUTTON" value=" Q " onclick="find('Q');"></td> <td><input type="BUTTON" value=" R " onclick="find('R');"></td> <td><input type="BUTTON" value=" S " onclick="find('S');"></td> <td><input type="BUTTON" value=" T " onclick="find('T');"></td> <td><input type="BUTTON" value=" U " onclick="find('U');"></td> <td><input type="BUTTON" value=" V " onclick="find('V');"></td> <td><input type="BUTTON" value=" W " onclick="find('W');"></td> <td><input type="BUTTON" value=" X " onclick="find('X');"></td> <td><input type="BUTTON" value=" Y " onclick="find('Y');"></td> <td><input type="BUTTON" value=" Z " onclick="find('Z');"></td> </tr> </tbody> </table> </form> </body> </html>

The issue is with these lines of code.问题在于这些代码行。

var num = Math.floor(keywords);
actualword = keywords[num];
actualword = actualword.toUpperCase();

keywords is an array var keywords=["Apple","Banana","Variety"]; keywords 是一个数组var keywords=["Apple","Banana","Variety"]; . .

Math.floor(keywords); // this will return NaN

So this will become:所以这将变成:

var num = Math.floor(keywords); // NaN
actualword = keywords[num]; // keywords[NaN], which will be undefined
actualword = actualword.toUpperCase(); // undefined.toUpperCase(); this will cause an error.

Cannot read property 'toUpperCase' of undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM