Thinkful Unit 2 Comprehension Checks

Lesson 1

Assignment 1

What is the DOM (Document Object Model) and how does it relate to JavaScript/jQuery?

The Document Object Model, normally abbreviated to DOM, creates a hierarchy corresponding to the structure of each web document. The DOM is the API through which JavaScript interacts with content within a website.

Why would you choose jQuery over JavaScript to add interactivity to your web page?

jQuery is designed to make many JavaScript development tasks much easier.

Is there a difference between the following: jQuery('h2'); vs. $('h2');?

No.

Why is it oftentimes important to wrap your jQuery functions in the following?$(document).ready(function () {do something;});

To make sure the page is completely loaded before running scripts.

How do you get jQuery into your web page?

Download the jQuery library from jQuery.com or include jQuery from a CDN, like Google.

How would you select an element vs. a class vs. an id in jQuery?

$( "div" );$( "#myDiv" );$( ".myClass" );

Describe the difference between and provide an example of each of the following jQuery selectors:

descendant selector
jQuery descender selector (X Y) is used to select all elements matched by ‘Y’ that are child, grandchild, great-grandchild, great-great-grandchild..(any levels deep) of a ‘X’ element.
Ex: $('form input'); ‘ selects all elements matched by <input> that are descendants of an element matched by <form>.

child selector
jQuery child selector (X > Y) is used to select all elements matched by ‘Y’ that are child of a ‘X’ element.
Ex: $('form > input'); ‘ selects all elements matched by <input> that are child of an element matched by <form>, Only child element will match, grandchild, great-grandchild will not match.

multiple selector
Selects the combined results of all the specified selectors.
Ex: $("div,span,p.myClass"); – selects all divs, spans, and paragraphs with the class “myClass”.

What does it mean to ‘traverse the DOM’ and how do you do it?

jQuery lets us “traverse” ‘ or move through ‘ the HTML elements that make up our page. First, we make an initial selection, and then move through the DOM relative to that selection.

What does the % operator do? For instance, what would result would you get from 101 % 100 ?

Modulo, like remainder. So 101 % 100 would be 1.

When would you need to ‘escape’ a character in a string with a backslash?

Quotes inside quotes, newlines, backslashes, tabs

How do you concatenate two or more strings?

+ operator

How do the logical operators && , || , and ! work in JavaScript?

The && operator represents logical and. It is a binary operator, and its result is only true if both of the values given to it are true.
|| is the logical or, it is true if either of the values given to it is true.
Not is written as an exclamation mark, !, it is a unary operator that flips the value given to it, !true is false, and !false is true.

What is the order of precedence for operators (keeping in mind that you can always use parentheses when in doubt)?

Division and multiplication always come before subtraction and addition. When multiple operators with the same precedence appear next to each other (1 – 1 + 1) they are applied left-to-right.

What is an expression in the context of programming? How is this different from a statement? (HINT: Think about side effects).

A piece of code that produces a value is called an expression. An expression can be content to just produce a value, but a statement only amounts to something if it somehow changes the world.

What are reserved words?

javascript keywords that cannot be used as variable names.

How do you implement a while loop? How do you implement a do loop? How do you implement a for loop?

While loops: First, a ‘counter’ variable is created. This variable tracks the progress of the loop. The while itself contains a check, usually to see whether the counter has reached some boundary yet. Then, at the end of the loop body, the counter is updated.
Do loops:
For loops: All the statements that are related to the ‘state’ of the loop are now on one line. The parentheses after the for should contain two semicolons. The part before the first semicolon initialises the loop, usually by defining a variable. The second part is the expression that checks whether the loop must still continue. The final part updates the state of the loop.

Why is indentation important (though not necessary) in JavaScript?

The role of the indentation inside blocks is to make the structure of the code clearer to a reader.

What happens when the break keyword is reached in a loop?

It immediately jumps out of the current loop, continuing after it.

What is the syntax for a function in JavaScript?

function add(a, b) {
return a + b;
}
show(add(2, 2));

Why might you define a function within another function?

Because the computer looks up all function definitions, and stores the associated functions, before it starts executing the rest of the program.

Give an example of a recursive function. Why is the elegance of the recursive function not always preferable in JavaScript?

In JavaScript, running through a simple loop is a lot cheaper than calling a function multiple times.

What is an anonymous function, and why might you use one?

A function that doesn’t have a name.

Lesson 4

Assignment 1

What is a property in JavaScript? How do you add a non-existent property to an object?

A property is an association of another value with a value. If you have a JavaScript value with the type object, you can add and remove properties at will. If a property that does not yet exist is set with the = operator, it is added to the object.

var empty = {};
empty.notReally = 1000;
show(empty.notReally);

What is an array? What do the following array methods do?

A set of numbers or objects that will follow a specific patternmyArray.push() – adds an item to an arraymyArray.pop() – removes an item from an arraymyArray.join() – builds a single big string from an array of strings

How would you get the value of the item at a given index in an array?

var valueAtIndex1 = myValues[1];

What does the new keyword do in JavaScript?

– It creates a new object. The type of this object, is simply object.
– It sets this new object’s internal, inaccessible, [[prototype]] property to be the constructor function’s external, accessible, prototype object.
– It executes the constructor function, using the newly created object whenever this is mentioned.