Workshop
The workshop will test how much you have grasped of the topics discussed in this chapter.
Q&A
I want to repeat a piece of code an exact number of times. Which JavaScript statement should I use?
The for statement is ideal to achieve that. You could achieve the same thing using a while loop, for example, but it is less convenient to use it.
I want to be able to check the length of several strings of characters in my code. How can I do that?
You could create a short function which takes a single argumentthe string you want to check. You may want to use array techniques within the function. Those will be described in Chapter 5.
I want to check if more than one thing is true. Can I use a switch statement to do that?
You could use a switch statement, but not on its own. You could nest another function which uses a switch statement within a case statement. Often it will be at least as easy to use an if statement with a number of else if statements. Whichever technique you choose you need to be clear about the process of logic and which statements will be evaluated and which statements won't.
Quiz
List the looping statements that are present in JavaScript.
Is it possible to include multiple statements in my own functions?
Quiz Answers
The looping statements in JavaScript are the while statement, the do while statement, and the for statement.
Yes. You can include multiple statements in a function by enclosing them in curly braces, like this:
function myFunction() { statement statement ... statement}
Exercise
Modify the multiplication table generator so that it will respond appropriately if the user clicks Cancel or enters a non-numeric value.