# JavaScript by Example: Under Certain Conditions

• Print
This chapter is from the book

## Exercises

1. Create a while loop that displays numbers as: 10 9 8 7 6 5 4 3 2 1. Put the numbers in HTML table cells.

2. Ask the user what the current hour is. If the hour is between 6 and 9 a.m., tell the user, "Breakfast is served." If the hour is between 11 a.m. and 1 p.m., tell the user, "Time for lunch." If the hour is between 5 and 8 p.m., tell the user, "It's dinner time." For any other hours, tell the user, "Sorry, you'll have to wait, or go get a snack."

3. Create a conversion table using the following formula:

```C = (F – 32) / 1.8;
```

Start with a Fahrenheit temperature of 20 degrees and end with a temperature of 120 degrees; use an increment value of 5. The table will have two columns, one for Fahrenheit temperature values and one for those same temperatures converted to Celsius.

4. Ask the user for the name of the company that developed the JavaScript language. Alert the user when he or she is wrong, and then keep asking the user until he or she gets the correct answer. When the user gets it right, confirm it.

5. Use a switch statement to rewrite the following JavaScript code. Prompt the user for the number of a month rather than setting it to 8.

```<script type=text/javascript>
month = 8;

if (month == 1) {
}
else if (month == 2) {
}
else if (month == 3) {
}
else if (month == 4) {
}
else if (month == 5) {
}
else if (month == 6) {
}
else if (month == 7) {
}
else if (month == 8) {
}
else if (month == 9) {
}
else if (month == 10) {
}
else if (month == 11) {
}
else if (month == 12) {
}
else{
}
</script>
```
6. Consider the following example:

```var start_time = (day == weekend) ? 12 : 9;
```

Rewrite the conditional statement using an if/else construct.