2017-10-28 15 views
0

私はhtmlとjava-scriptが初めてです。私のコードのために、私の関数は定義されていないと私は理解できない理由を把握する。スクリプトはheadタグに移動しようとしました。関数の名前を既に使用されていないことを確認するために変更しようとしました。私は小さいと愚かな何かを見逃していると思う。すべてのヘルプはJavascript - 定義されていない関数のエラー

<html> 

    <script type= "text/javascript" > 

    var d = new Array(0,0,0,0,0); 

    function aaa() 
    { alert("hello"); 
     for (var i = 0; i<5; i++){ 
     var da = Math.floor(Math.random()*6) +1; 
     var di document.getElementById("d" + i); 
     di.src = "pix/dice" + d + ".gif"; 
     } 
    } 

    function newGame(){ 
    } 

    function click1() { 
     var total = 0; 
     for (var i = 0; i <5; i++){ 
     if (d[i] == 1) total +=1;} 
     var loco = document.getElementById("s1"); 
     loco.innerHTML = total; 

     } 

    </script> 

    <h1>Yahtzee</h1> 

    <div style = "text-align:center;"> 
     <img src = "pix/dice0.gif" id = "d0"> 
     <img src = "pix/dice0.gif" id = "d1"> 
     <img src = "pix/dice0.gif" id = "d2"> 
     <img src = "pix/dice0.gif" id = "d3"> 
     <img src = "pix/dice0.gif" id = "d4"> 
    </div> 
    <table border =2 align =center> 
     <tr> 
      <td class = "c1"> 1's </td> 
      <td class = "c2"><div class = "scr" id = "s1" onclick= "click1()" > </div> </td> 
      <td class = "c3" rowspan = "6"><button id = "roll;" onclick = "aaa()";>Roll</button> </td> 
     </tr> 
     <tr> 
     <td class = "c1"> 2's </td> 
      <td class = "c2"><div class = "scr" id = "s2"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 3's </td> 
      <td class = "c2"><div class = "scr" id = "s3"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 4's </td> 
      <td class = "c2"><div class = "scr" id = "s4"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 5's </td> 
      <td class = "c2"><div class = "scr" id = "s5"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 6's </td> 
      <td class = "c2"><div class = "scr" id = "s6"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 3 of a kind </td> 
      <td class = "c2"><div class = "scr" id = "k3"> </div></td> 
      <td class = "c3" rowspan = "8"><button id = "ng" onclick="newGame()"; >New Game </button> </td> 
     </tr> 
     <tr> 
      <td class = "c1"> 4 of a kind </td> 
      <td class = "c2"><div class = "scr" id = "k4"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Full House </td> 
      <td class = "c2"><div class = "scr" id = "fh"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Sm Straight </td> 
      <td class = "c2"><div class = "scr" id = "ss"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Lg Straight </td> 
      <td class = "c2"><div class = "scr" id = "ls"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Yahtzee </td> 
      <td class = "c2"><div class = "scr" id = "yah"> </div></td>  
     </tr> 
     <tr> 
      <td class = "c1"> Chance </td> 
      <td class = "c2"><div class = "scr" id = "cha"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Total</td> 
      <td class = "c2"><div class = "scr" id = "tot"> </div></td> 
     </tr> 

    </table> 

</body> 

+2

di=を追加することを忘れていることだったが見えますあなたの 'aaa'関数内でのエラー:' var di = document.getElementById( "d" + i) '。等号がありません。 – Nick

+0

そして私はコンストラクタの代わりに配列リテラル '[0,0,0,0]'を好む。 –

+0

JavaScriptの学習(そしてその後のコーディング)の最も重要な部分の1つは、ブラウザコンソールを使用することです。 ( 'console.log'と' console.error')あなたのコードを通して、これらの行はデバッグに役立ちます。また、これらを使用しなくても、エラーが発生したときにエラーと警告が表示され、エラーが発生した正確な行番号が表示されます。最近のブラウザでは、開発ツールを使ってコンソールを開くことができます(私たちのキーボードショートカットは 'Ctrl + Shift + i 'です)。コンソールを愛することを学んで、SOにようこそ。 – Dellirium

答えて

0

エラーはあなたがライン

var di document.getElementById("d" + i); 

var d = new Array(0,0,0,0,0); 
 
\t \t 
 
\t \t function aaa() 
 
\t \t { \t 
 
     var i = 0 
 
     alert("hello"); 
 
\t \t \t for(i; i<5; i++){ 
 
     var randomn = Math.random(); 
 
\t \t \t var da = Math.floor(randomn*6) +1; 
 
     var elid = "d" + i; 
 
\t \t \t var di = document.getElementById(elid); 
 
\t \t \t di.src = "pix/dice" + d + ".gif"; 
 
\t \t \t } 
 
\t \t } 
 
\t \t 
 
\t \t function newGame(){ 
 
\t \t } 
 
\t \t 
 
\t \t function click1() \t { 
 
\t \t \t var total = 0; 
 
\t \t \t for (var i = 0; i <5; i++){ 
 
\t \t \t if (d[i] == 1){total +=1;} 
 
\t \t \t var loco = document.getElementById("s1"); 
 
\t \t \t loco.innerHTML = total; 
 
\t \t \t } 
 
\t \t \t }
<h1>Yahtzee</h1> 
 
\t \t 
 
\t \t <div style = "text-align:center;"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d0"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d1"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d2"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d3"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d4"> 
 
\t \t </div> 
 
\t \t <table border =2 align =center> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 1's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s1" onclick= "click1()" > </div> \t </td> 
 
\t \t \t \t <td class = "c3" rowspan = "6"><button id = "roll;" onclick = "aaa();">Roll</button> </td> 
 
\t \t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t <td class = "c1"> 2's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s2"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 3's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s3"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 4's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s4"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 5's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s5"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 6's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s6"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 3 of a kind </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "k3"> </div> \t </td> 
 
\t \t \t \t <td class = "c3" rowspan = "8"><button id = "ng" onclick="newGame()"; >New Game </button> </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 4 of a kind </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "k4"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Full House </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "fh"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Sm Straight </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "ss"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Lg Straight </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "ls"> </div></td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Yahtzee </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "yah"> </div> \t </td> 
 
\t \t \t \t 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Chance </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "cha"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Total</td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "tot"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t </table>

+0

私はそれを見てとても多くの時間を費やし、私は信じていませんが、それはとてもシンプルなものだったと思います。ありがとうございました –

0

を理解されるであろう次のコードでは、あなたの構文エラーを修正しており、今の機能は、(必要に応じて、あなたが「こんにちは」のアラートを見ることができます)と呼ばれています。あなたは構文eを持っているよう

var d = [0,0,0,0,0]; 
 

 
    function aaa() 
 
    { alert("hello"); 
 
     for (var i = 0; i<5; i++){ 
 
     var da = Math.floor(Math.random()*6) +1; 
 
     var di = document.getElementById("d" + i); 
 
     di.src = "pix/dice" + d + ".gif"; 
 
     } 
 
    } 
 

 
    function newGame(){ 
 
    } 
 

 
    function click1() { 
 
     var total = 0; 
 
     for (var i = 0; i <5; i++){ 
 
     if (d[i] == 1) total +=1;} 
 
     var loco = document.getElementById("s1"); 
 
     loco.innerHTML = total; 
 

 
     }
<html> 
 

 
    <body> 
 
    
 
    <h1>Yahtzee</h1> 
 

 
    <div style = "text-align:center;"> 
 
     <img src = "pix/dice0.gif" id = "d0"> 
 
     <img src = "pix/dice0.gif" id = "d1"> 
 
     <img src = "pix/dice0.gif" id = "d2"> 
 
     <img src = "pix/dice0.gif" id = "d3"> 
 
     <img src = "pix/dice0.gif" id = "d4"> 
 
    </div> 
 
    <table border =2 align =center> 
 
     <tr> 
 
      <td class = "c1"> 1's </td> 
 
      <td class = "c2"><div class = "scr" id = "s1" onclick= "click1()" > </div> </td> 
 
      <td class = "c3" rowspan = "6"><button id = "roll;" onclick = "aaa()";>Roll</button> </td> 
 
     </tr> 
 
     <tr> 
 
     <td class = "c1"> 2's </td> 
 
      <td class = "c2"><div class = "scr" id = "s2"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 3's </td> 
 
      <td class = "c2"><div class = "scr" id = "s3"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 4's </td> 
 
      <td class = "c2"><div class = "scr" id = "s4"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 5's </td> 
 
      <td class = "c2"><div class = "scr" id = "s5"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 6's </td> 
 
      <td class = "c2"><div class = "scr" id = "s6"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 3 of a kind </td> 
 
      <td class = "c2"><div class = "scr" id = "k3"> </div></td> 
 
      <td class = "c3" rowspan = "8"><button id = "ng" onclick="newGame()"; >New Game </button> </td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 4 of a kind </td> 
 
      <td class = "c2"><div class = "scr" id = "k4"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Full House </td> 
 
      <td class = "c2"><div class = "scr" id = "fh"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Sm Straight </td> 
 
      <td class = "c2"><div class = "scr" id = "ss"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Lg Straight </td> 
 
      <td class = "c2"><div class = "scr" id = "ls"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Yahtzee </td> 
 
      <td class = "c2"><div class = "scr" id = "yah"> </div></td>  
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Chance </td> 
 
      <td class = "c2"><div class = "scr" id = "cha"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Total</td> 
 
      <td class = "c2"><div class = "scr" id = "tot"> </div></td> 
 
     </tr> 
 

 
    </table> 
 

 
</body>

関連する問題