2012-02-18 11 views
1

この割り当てのエラーを確認するには、私は助けが必要です。このスクリプトを実行すると私のインターネットブラウザがクラッシュします!私はこのスクリプトを完全に自分で書いたので、間違っているかもしれません!これは、配列を使用して月を出力するためにループを使用する学校の割り当てです。JavaScript:プログラムがクラッシュするのはなぜですか?

PS:私がブレーク/コンティニューコードを作成するまで、プログラムはうまくいった!これに

document.write("I skipped March with a continue statement”.”); 

:あなたが適切にあなたの引用を閉じていない

document.write("I skipped March with a continue statement”."); 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 


<head> 

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

    <title>Assignment 6: Steph Hussar</title> 

    <script type="text/javascript"> 

     // Variable Declaration 

     var monthArray = new Array(); 
     monthArray[0]="January"; 
     monthArray[1]="February"; 
     monthArray[2]="March"; 
     monthArray[3]="April"; 
     monthArray[4]="May"; 
     monthArray[5]="June"; 
     monthArray[6]="July"; 
     monthArray[7]="August"; 
     monthArray[8]="September"; 
     monthArray[9]="October"; 
     monthArray[10]="November"; 
     monthArray[11]="December"; 


     // Using the for loop 

     document.write("<h4>Using the for loop</h4>"); 

     for(var count =0 ; count < 12 ; count++) 
     { 
      document.write(monthArray[count] + "<br />"); 
     } 

     // Using the while loop 

     document.write("<h4>Using the while loop</h4>"); 

     var count = 0; 
     while (count < 12) 
     { 
      document.write(monthArray[count] + "<br />"); 
      count++; 
     } 


     // Using for in loop 

     document.write("<h4>Using for in Loop</h4>"); 

     for(index in monthArray) 
     { 
      document.write(monthArray[index] +"<br />"); 
     } 



     // Using the Break 

     document.write("<h4>Using the break when the month of March is found</h4>") 

      for(count = 0 ; count < 12 ; count++) 
      { 
      if (count == 3) 
       { 
       break; 
      }else{ 
      document.write(monthArray[count] + "<br />"); 

      } 
      } 
      document.write("I broke out of the loop!"); 

     // Using the Continue 

     document.write("<h4>Using the continue when the month of March is found</h4>")  


      for(count = 0 ; count < 12 ; count++) 
      { 
      if (count == 2) 
       { 
       continue; 
      }else{ 
      document.write(monthArray[count] + "<br />"); 

      } 
      } 
      document.write("I skipped March with a continue statement!"); 


    </script> 

</head> 

<body> 

</body> 

    </html> 
+0

セミコロンが各javascript文の最後にあることを確認してください。特にdocument.write文が重要です。 –

+0

宿題なので、答えは教えてくれません。しかし、ヒントとして、個々のループをコメントアウトしてから、クラッシュの原因を見つけだしてから、ループのどの部分が原因で発生するのかを特定してください。 –

+0

@JohnPick - 実際には、セミコロンはjavascriptでは必須ではありません。 – mrtsherman

答えて

2

あなたは、この行を変更する必要があります。

2

私にはうまく見えますが、私はあなたの "私はループから勃発しました"で中括弧引用符を削除しようとしています。文字列。ブラウザによっては、その場合には、文字列の終端としてカーリー引用符が表示されることがあり、「構文エラーです

2

構文エラーがここにあります:。。

document.write("I skipped March with a continue statement”.”); 

奇妙なエンドに注意してください。引用...文字列が適切に引用されていない

2

あなたは非常に近い実際にある - ちょうどもし文が固定される必要があり、私が考える - 次のように:。ここで

// Using the Break   
    document.write("<h4>Using the break when the month of March is found</h4>") 
    for(count = 0 ; count < 12 ; count++)    
    {    
    if (count == 2) 
    {  
     document.write("I broke out of the loop"); 
     break; 
     } 
     else 
    {    
     document.write(monthArray[count] + "<br />"); 
    } 
    } 
    // Using the Continue   
    document.write("<h4>Using the continue when the month of March is found</h4>") 
    for(count = 0 ; count < 12 ; count++)    
    {    
    if (count == 2) 
    { 
    document.write("Skip March and continue<br />"); 
    continue; 
    } 
    else 
    {  
    document.write(monthArray[count] + "<br />"); 
    }  
    } 
0

それが正常に動作しているようです:私はいくつかの行方不明カンマで追加

http://jsfiddle.net/VvUW5/2/

は、壊れた文字列を固定し、そしていくつか欠けているvar年代を追加しました。

今のところすべてやっているのですか、それとも別の問題がありますか?

関連する問題