Example: Iteration using for

Before clicking on the Run button, try to figure out what will be printed to the console.

In particular, notice the following:

  • To form newString, the value of counter is increased by 5 at each iteration.
  • To form reverseString, the value of counter is decreased by 1 at each iteration. Notice the starting and ending values.
  • To form shortString, a break statement is used when an e is read, and otherwise the current symbol is added to the string.
  • To form skipString, a continue statement is used when a blank is read, and otherwise the current symbol is added to the string.

Try changing the break statement to a continue statement and the continue statement to a break statement. Guess what the results will be and then run the script to see if you are correct.

Note:

Hints:

  • undefined
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
var myString = "This is the string we have.";
var newString = "";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX