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 ofcounter
is increased by 5 at each iteration. - To form
reverseString
, the value ofcounter
is decreased by 1 at each iteration. Notice the starting and ending values. - To form
shortString
, a break statement is used when ane
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