Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

Practice exercises CHAPTER 3 121


■■The Array object has many methods.
■■The DOM represents a hierarchy of objects, forming a model of your HTML document.
■■An event takes place at a point in time.
■■Events are based on the publisher or subscriber pattern.
■■To subscribe to an event, you can use the addEventListener function. To cancel event
propagation, use the stopPropagation function on the Event object. To unsubscribe
from an event, use the removeEventListener function. To stop default behavior, call the
preventDefault method on the Event object.
■■To add an event subscription on an HTML element, add the “on” prefix to the event
name to get the name of the attribute you will add to the start tag of the element.

Lesson review


Answer the following questions to test your knowledge of the information in this lesson. You
can find the answers to these questions and explanations of why each answer choice is correct
or incorrect in the “Answers” section at the end of this chapter.


  1. You have two arrays of strings, customers and employees, and you want to combine
    them to create a contacts array. Which method would be most suitable for this task?
    A. concat
    B. join
    C. push
    D. splice

  2. You want to obtain a list of all elements whose tag name is div, and you need to
    retrieve this list as quickly as possible. Which function is most appropriate for this task?
    A. getElementsByName
    B. querySelectorAll
    C. getElementsByTagName
    D. getElementsByClass


Practice exercises


If you encounter a problem completing any of these exercises, the completed projects can be
installed from the Practice Exercises folder that is provided with the companion content.

Exercise 1: Create a calculator webpage


In this exercise, you practice your JavaScript knowledge by creating a website with a single
webpage that contains a calculator. The webpage itself will be somewhat basic until CSS is
discussed in Chapter 4.
Free download pdf