JavaScript:为什么我的程序会崩溃?

时间:2012-02-18 02:21:42

标签: javascript

我需要帮助检查此作业的任何错误。我运行这个脚本时,我的互联网浏览器崩溃了!我完全自己编写了这个脚本,所以可能不正确!这是学校的一项任务,我们将使用循环输出一年中的几个月(在数组中)。

PS:程序运行良好,直到我创建了中断/继续代码!

 <!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>

5 个答案:

答案 0 :(得分:2)

您必须更改此行:

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

对此:

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

您没有正确结束报价。

答案 1 :(得分:2)

对我来说很好看,但我会尝试删除“我突然失控”中的引号。“字符串。根据浏览器的不同,它可能会将卷曲引号视为字符串的结尾,在这种情况下。“是语法错误。

答案 2 :(得分:2)

这里有语法错误:

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

请注意奇怪的结束语...字符串未正确引用。

答案 3 :(得分:2)

你真的非常接近 - 我认为只需要修复If语句 - 就像这样:

 // 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 />"); 
    }    
  } 

答案 4 :(得分:0)

这似乎工作正常:

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

我添加了一些丢失的逗号,修复了一个损坏的字符串,并添加了一些丢失的var

它正在做它现在应该做的一切,还是有另一个问题?