【问题标题】:I am making a hangman game, i got the logic down but its not working,when i inspect it it says undefined touppercase我正在制作一个刽子手游戏,我把逻辑搞砸了,但它不起作用,当我检查它时,它说 undefined touppercase
【发布时间】:2020-09-03 05:16:08
【问题描述】:

你可以看到我只用 html 和 javascript 制作了一个简单的刽子手游戏...我把逻辑搞砸了,但它似乎总体上不起作用。我检查了元素,看看它说 undefined toUppercase 有什么问题。

我是一个新手程序员,所以请给我一些关于如何解决这个问题的指示,也可以指出我在其他任何错误的地方

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);
    }
<!doctype html>
<html>
<head>
<title>HangMan Game - Javascript</title>
<script>
</script>  
</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>

【问题讨论】:

  • 您的代码有很多问题,所以我建议您从头开始并使用console.log 来查看您正在使用的值并找出它们给出错误的原因。例如,在var num = Math.floor(keywords); 之后的console.log(num) 将立即显示在执行的第一行存在问题。逐行工作,您会发现需要解决的错误!
  • 好的,谢谢你的建议

标签: javascript html arrays


【解决方案1】:

让我们检查下面 3 行代码

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

这里的实际字是未定义的,这就是它给出错误的原因。 现在实际字未定义,因为关键字[nums] 没有返回正确的元素,那是因为 Math.floor(keywords) 返回 NaN

所以问题出在 Math.floor(keywords) 行。

【讨论】:

  • 啊,我看到了寻求帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 2022-08-15
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多