2017-10-28 4 views
0

Ich bin neu in HTML und Java-Skript. Für meinen Code bekomme ich immer wieder, dass meine Funktionen nicht definiert sind und ich kann nicht herausfinden warum. Ich habe versucht, das Skript an den Kopf-Tag und nach dem Anruf zu verschieben. Ich habe versucht, den Namen der Funktion zu ändern, um sicherzustellen, dass sie nicht schon benutzt wurde. Ich denke, dass ich etwas kleines und dummes vermisse. Jede Hilfe wirdJavascript - undefined Funktionsfehler

<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

hinzuzufügen vergessen Sieht aus wie Sie eine Syntax e haben rror in Ihrer 'aaa' Funktion:' var di = document.getElementById ("d" + i) '. Es fehlt ein Gleichheitszeichen. – Nick

+0

Und ich bevorzuge das Array Literal '[0,0,0,0]' anstelle des Konstruktors. –

+0

Einer der wichtigsten Teile des Lernens (und dann später Codierens) von Javascript ist die Verwendung der Browserkonsole. ('console.log' und' console.error') In Ihrem Code helfen Ihnen diese Zeilen beim Debuggen. Auch wenn Sie diese nicht verwenden, wird der Browser immer noch Fehler und Warnungen ausgeben, wenn er auf Probleme stößt, zusammen mit der (häufig nicht so oft) genauen Zeilennummer, auf der der Fehler aufgetreten ist. Sie können die Konsole über die Entwicklertools in den meisten modernen Browsern öffnen (die Tastenkombination "Strg + Umschalt + i"). Lerne die Konsole zu lieben, willkommen bei SO. – Dellirium

Antwort

0

der Fehler war, dass man = nach di in Zeile

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

Ich verbrachte so viel Zeit damit, es zu betrachten und ich kann nicht glauben (aber wusste es) war etwas so einfaches. Danke –

0

Der folgende Code Ihrer Syntaxfehler korrigiert geschätzt, und jetzt werden die Funktionen aufgerufen werden (können Sie die „hallo“ alert sehen wie gewünscht).

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>