Course Technology, Thomson Learning
>Online Companion Home  >HTML Tutorial 8

Tutorial 8: Programming with JavaScript

Creating a Programmable Web Page for North Pole Novelties

Additional Topics


JavaScript Resources on the Web

If you want more information on using JavaScript, here are some resources on the Web:

To the top


Interrupting Loops

If you are creating a loop, you can use the break and continue commands to alter the loop's operation.

The break command halts the loop, even if the conditions to end the loop have not been met. For example, if you want to create a loop that prompts students to answer a question, and give them three chances to answer that question, you can use the following JavaScript program:

for (i=1;i<=3;i++) {
  answer=prompt("In what year did 
  the Civil War begin?","");
      if (answer=="1861") {
     alert();
   break;
  }<
}

In this example, if the answer variable is equal to "1861" (the correct answer) the loop stops with the break command. If the student answers the question incorrectly, the loop continues for 3 iterations or until the question is answered correctly. Note that you can learn more about the alert() and prompt() methods in Tutorial 8. To see this JavaScript program in action, click the button below:

The continue command is similar to the break command, except that it is used to jump to the end of the command block without completing the remaining commands in the block. From that point, the loop continues to iterate. For example, the following JavaScript program prompts a student to answer a series of simple math questions. If the student answers correctly, the program jumps to the end of the command block, skipping a command to display the correct answer.

for (i=1;i<=5;i++) {
  answer=eval(prompt("What is "+i+" times "+i+"?",""));
   if (answer==i*i) {
   continue;
   }
    Alert("No, the answer is: "+i*i);
 }

To see this JavaScript program in action, click the button below:

To the top