Summary
In this hour, you learned the basics of adding jQuery and JavaScript to web pages. The basic data types that are used in JavaScript and, consequently, jQuery, were described. You learned some of the basic syntax of applying conditional logic to JavaScript applications. You also learned how to compartmentalize your JavaScript applications into functions that can be reused in other locations. Finally, you learned some ways to handle JavaScript errors in your script before the browser receives an exception.
Q&A
Q. When should you use a regular expression in string operations?
A. That depends on your understanding of regular expressions. Those who use regular expressions frequently and understand the syntax well would almost always rather use a regular expression because they are so versatile. If you are not very familiar with regular expressions, it takes time to figure out the syntax, and so you will want to use them only when you need to. The bottom line is that if you need to manipulate strings frequently, it is absolutely worth it to learn regular expressions.
Q. Can I load more than one version of jQuery at a time?
A. Sure, but there really isn’t a valid reason to do that. The one that gets loaded last will overwrite the functionality of the previous one. Any functions from the first one that were not overwritten may be completely unpredictable because of the mismatch in libraries. The best bet is to develop and test against a specific version and update to a newer version only when there is added functionality that you want to add to your web page.
Workshop
The workshop consists of a set of questions and answers designed to solidify your understanding of the material covered in this hour. Try to answer the questions before looking at the answers.
Quiz
- What is the difference between == and === in JavaScript?
- What is the difference between the break and continue keywords?
- When should you use a finally block?
- What is the resulting value when you add a string “1” to a number 1, ("1"+1)?
Quiz Answers
- == compares only the relative value; === compares the value and the type.
- break will stop executing the loop entirely, whereas continue will only stop executing the current iteration and then move on to the next.
- When you have code that needs to be executed even if a problem occurs in the try block.
- The string “11” because the number is converted to a string and then concatenated.
Exercises
Open hour0504.html and modify it to create a table instead of a list. You will need to add code to the writeIt() function that writes the <table> open tag before iterating through the planets and then the closing tag after iterating through the planets. Then modify the makeListItem() function to return a string in the form of
<tr><td>planent</td><td>moon</td></tr>
Modify hour0503.html to include some additional times with different messages and images. For example, between 8 and 9 you could add the message “go to work” with a car icon, between 5 and 6 you could add the message “time to go home” with a home icon. You will need to add some additional cases to the switch statement and set the timeOfDay value accordingly.