Beginning JavaScript Flashcards

1
Q

Where should your scripts / script tags always go?

A

At the bottom of your body tag, because for you to access elements on the page, they first must have loaded onto the page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What, in folder symbol language, is “this folder”

A

./

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the downside of multiple script tags in the same file?

A

Every time the browser hits a script tag it will go off and download and parse that file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What should you do instead of having multiple script tags within the same HTML document?

A

Either bundle your files into one JS file or employ code-splitting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is code splitting?

A

When you split JS files into multiple smaller files and then have them load on demand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the keyboard shortcut within the browser to inspect an element?

A

Cmd + Shift + C, then you can hold it while hovering over the element you need.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the keyboard shortcut to open the console directly in the browser?

A

Option + Cmd + J

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why don’t we have to type ‘use strict’ every time?

A

Because it is enforced by default in JS modules, which is how almost all modern JS will be written.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference in scope between var, let, and const?

A

let and const are block scoped while var is function scoped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Wes’s opinion of when to use var, let, or const?

A

By default always use const, once it needs to change switch it to let…never use var.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the 7 types in JavaScript?

A

BS BONUS - BigInt, Symbol, Boolean, Object, Null, Undefined, String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Everything in javascript is an….

A

Object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the only non-primitive Type?

A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which type always gives us a guaranteed unique identifier?

A

Symbols.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you disable eslint on a particular file?

A

Just make a comment at the top:

/* eslint-disable */

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you disable eslint for just one line?

A

Make a comment above the line

/* eslint-disable-line */

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can use a single quote within single quotes?

A

Escape it with a backslash /
This tells the interpreter to take what follows as text, not as javascript.

‘he/’s so cool’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does it mean to say the plus sign is ‘loaded’?

A

It can both add and concatenate and if you use different types, you could be in for a world of hurt and misunderstanding, bugs and misery.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How should you store money?

A

Just in cents, so that you have a whole number that is easy to do math with rather than a floating point integer that is prone to errors, then just divide by 100 when you need to display it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the difference between undefined and null?

A

Undefined is when a variable has been created but never defined, null is when something has explicitly been set to nothing. It’s the difference between Cher being born with only one name (undefined) and Teller of Penn & Teller legally setting his name to just Teller (marking his last name as null explicitly)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the difference between using a double and triple equal sign?

A

“It’s almost always wrong to use the double equal sign to test equality.

Triple equal tests that the value and the type are the same, whereas a double equal sign tests only that the value is the same, not the type.

So, ““10”” == 10 is true, but ““10”” === 10 is false because they are different types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the tab keyword to get lorem ipsum text with the emmet extension?

A

lorem500 (or however many words you would like)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What built in JS function will let you move the navbar to any x, y coordinate on the page?

A

scrollTo - it either accepts an x and y coordinate as the params, or else accepts an option object which would have the position defined by top, left, bottom.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What argument can you pass if you need a function parameter to fall back to its default (but, for example, it’s in the middle of the argument list, so you can’t just skip it)

A

If you pass undefined, then the function will fall back to its default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What does it mean to say a JS Function is a 1st class citizen?

A

JS functions are values in themselves - this means that they can be stored in variables and passed into other functions. This allows functions to be handled and moved around just like any other data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is an anonymous function?

A

An anonymous function is just a function declared without a name - they are valid for callbacks or in an IIFE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is an IIFE?

A

An Immediately Invoked Function Expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is a function expression?

A

A function expression is when you store a function as a value in a variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Why do some say you shouldn’t use function expressions?

A

Because using them gives you bad errors - this is a little outdated, but it used to be tricky to track down what caused an error within a function expression, modern debug tools, however, infer the name of the function from the variable name it’s assigned to, and uses that in the error logging.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Why is it ok to use function expressions now when it used to be a faux pas?

A

Because modern browsers can infer the name of the function expression from the variable it’s assigned to and use this in error logging.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the difference between a regular function declaration and a function expression (as far as how they work, not syntax)?

A

The main difference is just in how they operate during hoisting. A function expression must be initialized before being run or you’ll get an error (no hoisting) - with regular functions defined using the function keyword, they are hoisted to the top, so it’s always available everywhere.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What are the advantages of arrow functions?

A
  1. A concise syntax that’s shorter.
  2. They don’t have their own scope in reference to the ‘this’ keyword, which is a little more intuitive.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Why must arrow functions be stuck into a variable?

A

Because, they are a special type of anonymous function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How can you use implicit returns within an arrow function?

A

Place the function all on one line, then make sure to take out the curly brackets (and the return keyword)

const implicit = () => ‘awesome’

implicit()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

When do you not need parentheses around a parameter sent into an arrow function?

A

If you only are passing one parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What can you do if you need to implicitly return an object from an arrow function?

A

Pop a set of parentheses around the object to be returned and that will contain it and keep the compiler from thinking that the object’s opening curly is the block of the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is the most important factor when determining whether to use an arrow function or a normal function?

A

Readability - if you come back in 6 months, which type will make the most sense and be understood better in this case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What is an IIFE? (Not the acronym - but what is an immediately invoked function expression?)

A

It’s when you wrap an anonymous function inside a parentheses - and parentheses always run first in JS, so the IIFE will run and return a function value then you can immediately run that value by putting another parentheses on the end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is this an example of?

(function() {

console.log(‘running the anon function’);

return ‘You are Cool’;

})();

A

An IIFE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is the benefit of an IIFE?

A

Not really any anymore, but it used to be popular before there were modules and block scoped variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What is a method?

A

A function that lives inside of an object. NOT the same thing as a function, as many functions are not methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

“What is the normal function shorthand way to write this method?

const robb = {

name: ‘Robb Schuneman’

sayHi: function() {

console.log(‘Hey Robb’)

return ““Hey Robb””;

}

}

A

const robb2 = {

name: ‘Robb Schuneman’

yellHi() {

console.log(‘HEY ROBB’)

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

“What is the method arrow shorthand way to rewrite this function?

const robb = {

name: ‘Robb Schuneman’

sayHi: function() {

console.log(‘Hey Robb’)

return ““Hey Robb””;

}

}

A

const robb2 = {

name: ‘Robb Schuneman’

whisperHi: () => {

console.log(‘HEY ROBB’)

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What is ‘this’ equal to in normal functions?

A

The object that the function was called against.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What does ‘this’ refer to in an arrow function?

A

An arrow function inherits the parents scope of what ‘this’ refers to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What is a callback function?

A

A function that will happen when something else is done - prominent examples are a click or a timer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What type of function is this an example of?

const button = document.querySelector(‘.clickMe’); button.addEventListener(‘click’, robb.yellHi)

A

A callback function (listening for a button click)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What is this an example of?

button.addEventListener(‘click’, function() { console.log(‘nice’);

});

A

A callback function that is defining its own anonymous function (rather than calling a predefined function).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

What does console.count(‘message’) do?

A

It keeps a running tab of how many times that console.count has been hit, so if you stick it in a function that you suspect is being called multiple times when it shouldn’t, you can easily see how many times exactly that it has been called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What does console.table do?

A

If you have a group, say an object or an array of objects, console.table puts them into an easily read attribute table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What is the advantage of passing in a variable to console.count

A

It’s counting the exact instance of the string that’s passed in as a param. So if you use object literal notation to do something like calling ${person.name} then it will tell you how many times the log is called for each argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

What does console.group do?

A

It nicely organizes anything that is put under it - this is a way to group together logs or variables or whatever.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

How do you start and end a console.group?

A

console.group to start and console.groupEnd to finish.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

What does console.groupCollapsed do?

A

it starts the group (still ending with console.groupEnd), but it is collapsed by default to provide less clutter to your console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is the console command (not keyboard shortcut) to return whatever element you currently have selected?

A

$0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

In addition to $0, how do you call the second to last element, or beyond that?

A

$1 is next to last, $2 second to last, and so forth.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

What aspect of a webpage will keep the $ command from working within the debugger console?

A

If the site is running JQuery, it overrides the meaning of the $ sign so that you can’t use it in debugging.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

What does the $(‘element’) console selector do?

A

It returns the first matching element or class object selected - so $(‘p’) will return the first paragraph on the page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What does the $$ selector select?

A

This will return all matching elements - so $$(‘p’) will return all paragraphs on the page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

What command will pause JS from running at the specified point in the code and give you a lot of tools you can use to figure out what’s wrong?

A

debugger;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

What all can you see when using the ‘debugger’ command within your code?

A

The program will pause running and allow you to see the local variables and what they’re exactly set to, as well as the call stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

How do you progress when using the debugger command to insert linebreaks?

A

Use the play button, it will forward to the next time it hits the debugger call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

What does the Step Over command do in the debugger console?

A

It will allow you to go step by step through your code, watching how variables change and what gets called when.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

What is a second way to set a breakpoint, in addition to putting the ‘debugger’ command into your code?

A

If you click the sources tab, then click on a line of code, it will insert a breakpoint.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

Where can you see which network calls are being fired when?

A

If you go to the Network tab, you can see all the files being used. Additionally, if you click on the timing tab, it will let you see where the time was spent (processing, waiting for data to return)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

What does it mean to break on an attribute within the console debugger, and how do you do it?

A

If you right click on an element, you can tell the debugger to break on attribute modification - this will pause in the debugger whenever the attribute change happens. This will let you see which function is calling for a change, as well as to see which attributes are getting passed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

How can you put a breakpoint on a specific event (like a click, or a network call)

A

If you go to sources, you’ll see an event listener breakpoint - you can tell it to break on mouseClick for example, and then it will give you a breakpoint debugger when they click - or whatever listener you’ve set,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

What is this an example of?

var anon = function() { 
 alert('I am anonymous'); 

} anon();

A

An anonymous function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

What question does scope answer?

A

Where are my functions and variables available to me?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

What is a global variable?

A

A variable that is available anywhere within your application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

What can a global variable be accessed by?

A

Any other Javascript running on the page - all functions, modules, if statements, and so on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

What is the global scope in terms of the browser?

A

The Window object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

How are common pre-defined methods available to us throughout Javascript?

A

Because they are defined on the Window object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

What is the difference between var variables and let and const when talking about variables declared on the top level?

A

Var variables are attached to the window object and are globally scoped, but const and let do not attach to the Window object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

Global variables are awesome!

A

GTFO - they are a recipe for a disastrous headache.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

Where are variables available if they are created within a function?

A

Only within that function (unless returned to whatever called it)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

What is Scope Lookup?

A

If a variable isn’t found in the scope where it is called, it will then look for it at a higher level.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

What happens if two variables of different scope are declared with the same name?

A

The one at the most immediate level of scope will prevail. (If a variable is declared both in a function and globally, and then called within the function - it will use the function definition of the variable.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

Why is it not a good idea to name variables at different scopes the same thing (shadow variables)?

A

Because you’ve limited yourself to not being able to access the variable from the umbrella scope within the more specific scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
80
Q

How can you access a variable outside of a block where it is modified?

A

Just declare it prior to the block as a let and then modify it - it will keep the scope of where it is declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
81
Q

What is one of the best things about block scoping on let and const?

A

Previously, with the function-scoped var, variables would sometimes leak out of their intended scope - for instance, if you finished a for loop with a var variable declared to count the iterations, the variable would still be there just hanging around after the loop has finished.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
82
Q

What is Lexical Scoping (or Static Scoping)

A

It is a feature in some languages, including JS, which means that Scope Lookup begins from the scope in which the function is defined, not by the scope in which the function is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
83
Q

We shouldn’t create global variables - how does modern JS make it hard to do so?

A

Most of modern JS is done within modules, where it is almost impossible to declare a global variable unless you explicitly call the window and attach the variable to it. But don’t do that - that’s just some hellish bugs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
84
Q

What are functions scoped to?

A

Their parent function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
85
Q

What is hoisting?

A

It’s a feature of JS that allows you to access functions and variables before they’ve been created within the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
86
Q

Which two things in JS are hoisted?

A
  1. Function Declarations
  2. Variable Declarations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
87
Q

What might be a preferred method instead of relying on hoisting?

A

Just define your functions and variables at the top of your file, or else put your functions into a separate module (utilFunctions, mathFunctions, etc) and then import them only as needed into the file where you need to run them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
88
Q

What might be one use for hoisting?

A

“Some people prefer to first make clear in their code ““what does this file do””, and then below, have the function declarations and variables showing how the sausage is made - but that’s just a style preference.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
89
Q

What’s the gotcha on hoisting variable declarations?

A

JS will hoist the declaration - but not a value if it’s set, so if you call a variable before the value that it’s been set to, it will return undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
90
Q

What is a closure?

A

A closure is the ability to access a parent level’s scope from the child level’s scope even after the parent has been terminated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
91
Q

How does a closure work?

A

JS is able to reach out to the parent scope from the child scope even if the parent is done running because it still maintains the variable or function in memory so that it can be accessed at a later time - it isn’t cleaned up or garbage collected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
92
Q

Why would a closure be helpful?

A

For one, often they can be used to create private variables that are inaccesible except through a child function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
93
Q

What is the Document Object Model (DOM)?

A

It is what the browser turns the HTML and CSS you write into.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
94
Q

What is the best way to think of the Window object?

A

Think of it as everything about the current browser window - which would include the tabs, browser bar, and other external things.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
95
Q

What is the best way to think of the document?

A

The document is responsible for everything from the opening html to the end - so not the tabs or the browser bar like the window. This entire HTML document is then accessible through the document keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
96
Q

What is the navigator keyword?

A

The navigator is a higher-level thing than the window or document, which give you info about not just the browser, but the device that the code is being run on itself - it can tell you about webcams or battery level or audio access, etc.

Device-specific things live on the navigator object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
97
Q

What happens if you load your Javascript before the HTML is created, like putting the script tag in the Head section as CSS is?

A

You won’t be able to access the HTML - you should instead put it right before the closing body tag.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
98
Q

If you really needed to load your JS from the header section for some reason, how could you do it?

A

You’d need to wrap all of your code in some sort of init function, and then launch that init function from an EventListener listening for the DOMContentLoaded event.

function init() {

const p =

document. querySelector(‘p’); console.log(p); }
document. addEventListener(‘DOMContentLoaded’, init);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
99
Q

What are the two main ways to select elements on the page?

A

.querySelector and querySelectorAll

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
100
Q

Can .querySelector and .querySelectorAll only be called against the document object?

A

No, they can be called against any element. Doing so will limit the scope of the selector to only children of that element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
101
Q

Give an example of how to select the first paragraph on a page.

A

const p = document.querySelector(‘p’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
102
Q

What does querySelectorAll return?

A

A NodeList of all matching elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
103
Q

What does querySelector return?

A

The first matching element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
104
Q

What are NodeLists?

A

They are lists of things, like array, but they don’t have all the functions built in that arrays do (map, filter, reduce, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
105
Q

Give an example of a parent -> child selector within JS?

A

const imgs = document.querySelectorAll(‘.item img’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
106
Q

What are additional ways to select elements on a page, beyond querySelector and querySelectorAll?

A

Though not really used anymore, there are still functions like getElementById, getElementByClassName and the like. Mostly you need to know about them in case you come across them in code, but you should be able to do all you need with querySelector and querySelectorAll.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
107
Q

What does console.dir do?

A

It will give you the object properties for an element, rather than just the display of the element - so you can see al the methods available, such as the parentNode, textContent and the like.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
108
Q

How do you set a property of an HTML element once you’ve gotten it with a querySelector?

A

“Simply by setting it equal to something new via dot notation.

heading.textContent = ““Robb is cool””

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
109
Q

What is the property for the text within an element?

A

.textContent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
110
Q

What are the differences between .innerText and .textContent?

A

.textContent is newer and gets the contant of all elements, including script and style properties within the element - .innerText only shows human-readable element.

However, innerText is style-aware, and won’t return elements hidden by CSS, whereas textContent just returns everything in the element, hidden or not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
111
Q

What does the element.innerHTML property give you?

A

The full HTML within the element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
112
Q

What does the element.outerHTML property give you?

A

It returns the full HTML within the element - and also the element it is contained within, as well as keeping any white space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
113
Q

How should you tack some content onto preexisting content?

A

Instead of seeting the content equal to itself and whatever additions, use the insertAdjacentText property, like so:

pizzaList.insertAdjacentText(‘afterend’, ‘pizza’)

The first argument says where to add the text, the second says what to add.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
114
Q

Why should you use element.insertAdjacentText instead of setting the textContent equal to what’s already there plus whatever is new?

A

Replacing the textContent is pretty slow in cases where there is lots of text and html getting put back in - and it causes the browser to re-render the entire element instead of just inserting the text.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
115
Q

What is the difference between an HTML Node and an Element?

A

Everything is a Node, whereas an Element describes only things wrapped in some sort of HTML tag.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
116
Q

What’s the best website to find a list of all the element properties and methods?

A

developer.mozilla.org/en-US/docs/Web/API

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
117
Q

How do you add a class to an element using JS?

A

element.classList.add(‘newClass’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
118
Q

How do you remove a class from an element?

A

element.classList.remove(‘oldClass’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
119
Q

What does element.classList.toggle(‘aClass’) do ?

A

It will add the class to the element if it isn’t present, or remove it if it is present.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
120
Q

Where should you generally put transitions? On the element? Or on a class?

A

Probably on the element being transitioned, not the class, because otherwise it will only transition one way - when the class is added, not when it’s removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
121
Q

What does element.classList.contains(‘someClass’) do?

A

It will return true or false dependent on whether the classList contains that class - which would be useful if you’re basing some conditional on that.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
122
Q

What two measurements can you pass in to CSS transform: rotate()?

A

You can either measure the rotation in degrees (360deg) or # of turns (1turn).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
123
Q

What are some common transform properties?

A

transform: rotate(360deg) or
transform: scale(1.5) or

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
124
Q

What arguments get passed to transform: scale() ?

A

You can either pass one argument to scale it both ways, or you can separately pass the X and Y you’d like to scale like:

transform: scale(2, 0.5);

It’s based off of relation to 1 - so 2 would be twice the size, 0.5 would be half the size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
125
Q

Give me an example of a common, basic argument set for the boxShadow CSS property?

A

boxShadow: 0 0 10px black;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
126
Q

What border radius will give you a circle?

A

border-radius: 50%;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
127
Q

What is probably the most common way to interact between CSS and JS?

A

Just adding and removing classes at different points in time, which lets us step in with our CSS and add or remove all the different transitions. This is especially common with modals or navs that need to slide in or whatever.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
128
Q

What is the className element attribute?

A

It is a class attribute which gives us a list of classes - but it’s older than classList, and classList additionally gives us these very useful functions (add, remove, toggle, contains)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
129
Q

What is an HTML attribute?

A

Anything provided to an element as additional info - classes, source, alt, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
130
Q

How do you set an HTML attribute?

A

The same as other things - once you’ve gotten your element, just set the dot property equal to something new:

element.alt = ‘Cute Pup’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
131
Q

“Do you need to title an alt attribute something like ““photo of (whatever)””?”

A

“NAH, because screen readers will already let the people using them know that this is an image. Just name it ““cute pup”” not ““A photo of a cute pup”””

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
132
Q

True or False - dot attributes like element.width or element.title work as both setters and getters?

A

True, you just do element.width = 300 or myWidth = element.width

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
133
Q

All dot attributes are setters and getters?

A

Not all - some, like .naturalWidth, can’t be changed, which makes sense if you think about it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
134
Q

What can you do if you have an element that you’re waiting to load before you can do something with it in JS?

A

You can put it in an event listener with the ‘load’ trigger - either from the window attribute, or for the element in question.

window. addEventLIstener(‘load’, function() {
console. log(pic.naturalWidth);

});

This waits for it to be loaded first, then executes the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
135
Q

True of False - eventListeners can only be fired on the Window element.

A

Nah. You can use them to watch for actions on specific elements as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
136
Q

Why do you have to wait for things to load even if the script tag is at the bottom of the body tag?

A

Because, that is true it will wait for all HTML to load - but certain things are asynchronous, like API calls or things loaded from a server, and it will not wait for those.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
137
Q

Why do we have getAttribute, setAttribute and hasAttribute methods when we can just use dot notation on an attribute?

A

They’re older for one, but also they work for getting non-standard attributes. But don’t set custom attributes, that’s pain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
138
Q

Should you set non-standard attributes on HTML elements?

A

No. Don’t go willy-nilly making up your own attributes, because if the HTML spec introduces it in the future, your legacy code will clash with the standard and make for huge bugs. Some popular libraries did this, and that’s why JS has to have stupid names for certain methods (.contains instead of .includes)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
139
Q

How should you set your own custom attributes?

A

Use data attributes - which is to say, use data-whatever like so:

This lets you attach data that isn’t a standard attribute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
140
Q

How do you access data attributes?

A

You call element.dataset, which returns an object with each of the properties defined on it - so you can do element.dataset.customWhatever

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
141
Q

What is it an example of if you have an image, and set a custom attribute of data-largeUrl that is the source to a larger picture you can use in the link?

A

That’s data attributes, homey.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
142
Q

What is a good website to figure out keycodes?

A

keycode.info

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
143
Q

What happens if you call .play() on an audio element already playing?

A

Nothing. It won’t play again until it has run its full course.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
144
Q

How do you rewind an audio element to the beginning?

A

audio.currentTime = 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
145
Q

Why shouldn’t you have a timeout in your JS to run when a transition ends, in addition to a transition manually set to the same amount of time?

A

Because that creates multiple places you have to remember to update if you change the time. Also someone on your team might unknowingly change one spot and not the other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
146
Q

What is a better solution than setting two corresponding timers (one as a transition, one as a timer before running code)?

A

Use the transitionEnd event that fires when the element is done transitioning, rather than trying to time it just right.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
147
Q

You can attach a listener to a JS array? T/F?

A

false. You can’t do that, you instead need to loop over each of them and attach an eventListener to each.
keys. forEach(key => key.addEventListener(‘transitionend’, removeTransition));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
148
Q

What is this (almost) always equal to?

A

The thing that it got called against.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
149
Q

What is the main way to create HTML elements in Javascript?

A

document.createElement(), and then pass it a tag name ( (‘p’) or (‘div’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
150
Q

What do square brackets around a param on MDN mean?

A

That it’s an optional param.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
151
Q

When you use document.createElement() it automatically puts that element on the page. T/F?

A

False - it just creates it within memory, then you still will need to tell it where to go.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
152
Q

What is the shortcut to give a class or title to an element via document.createElement?

A

TRICK NO TREAT - there is no shortcut for that, you just have to do it after creation by dot notation on the variable that you stored your newly created element within.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
153
Q

How do we add some created HTML to the DOM?

A

We grab the element we want to add it after and use .appendChild() or insertAdjacentElement()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
154
Q

How do you access the body of the document?

A

Easy - with document.body - they give us a nice attribute accessible via dot notation directly on the document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
155
Q

Why shouldn’t you update the created element and re-append it to the document.body a bunch of times?

A

Every time you use appendChild() you’re modifying the DOM, which causes the browser to re-paint what is on the actual page. So, you should use appendChild only after setting all the content and classes and whatever other attributes you need to.

And if you have multiple elements going in the same spot, append them to each other before appending them to the DOM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
156
Q

How can you add a JS created element to another element?

A

element.append() - but it doesn’t work on IE, or element.appendChild()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
157
Q

What built-in function can you use to append somewhere other than as a child of an existing element?

A

You can use .insertAdjacentElement() - which works the same as insertAdjacentText() - you pass it one of several options on where you want the element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
158
Q

What built-in function will copy the contents of a node to a new variable?

A

newElement = elementToBeCopied.cloneNode()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
159
Q

When you use .cloneNode() but don’t pass any arguments, what should you expect?

A

It will just be the top level node without any of its children - not even the textContent of that node. To get those along with it, you pass it an argument of ‘true’ which will tell it to do a deep clone and copy the child elements as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
160
Q

What’s a major gotcha when using backtick strings to dump HTML into the DOM?

A

If they’re coming from user input, you are opening yourself to XSS - cross site scripting, unless you sanitize it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
161
Q

What do we use to set or change the actual HTML of the item we have selected?

A

We would use .innerHTML (as opposed to just .textContent)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
162
Q

Other than security risks - what is another downside to setting innerHTML to backtick HTML?

A

What you get are not elements - they’re just strings so you can’t call functions against them or add event listeners or set attributes unless you first dump it into the DOM and then pull it back out.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
163
Q

What is a range?

A

Just a collection of elements or HTML that we can then work with.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
164
Q

How can you turn a back-tick string into actual HTML so that you can use your element built-in functions?

A

call .range.createContextualFragment()

document.createRange().createContextualFragment(myHTMLString);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
165
Q

What is XSS?

A

Cross-site Scripting - when a third party runs their own JS on your site, which should never happen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
166
Q

Explain the difference between a node and an element in terms of this element:

I am SO Cool

A

If you grabbed this p, and then called myP.children (which returns the child elements) - youwould only get the em tag - if you called myP.childNodes you would get the text node before the em, the em, and the text node afterward.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
167
Q

List the properties we have available for traversing and working with elements?

A

For elements:

.children

.firstElementChild (first element under)

.lastElementChild (last element under)

.previousElementSibling (the element before this)

.nextElementSibling (element after)

.parentElement (up one level)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
168
Q

What’s a better way to move between nodes in JS then traversing up, up, over, down or something?

A

Just use querySelector to grab the one you want, or if you don’t know exactly, use .closest()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
169
Q

what does .closest(element or class) do?

A

It grabs the closest element to whatever it is called against.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
170
Q

Why is it a bad idea to traverse the dom going up up over down down and so on?

A

Because you can’t assume the document structure will stay the same - if one div gets inserted between, or something else changes the structure of your code slightly, suddenly your day is ruined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
171
Q

What are the properties available for dealing with nodes?

A

.firstChild

.lastChild

.previousSibling

.nextSibling

.parentNode

.childNodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
172
Q

What is maybe the one most important take away for the node and element properties?

A

The exact same properties are available for both and are confusingly named - so you might get them mixed up and then wonder why your results aren’t what you thought. Just make sure you’re calling the ones you want.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
173
Q

What is the built in method to remove an element or node that is available on every single method or node?

A

.remove()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
174
Q

What is the gotcha with using .remove() to remove elements/nodes?

A

If you remove it - it’s taken out of the DOM, but is still present in memory - so you still have access to it, and you can do some stuff to it and then put it back in the same spot or somewhere else.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
175
Q

If you want to insert an HTML string into the DOM, how can you accomplish this?

A

You’d have to use insertAdjacentHTML instead of insertadjacentElement, as this accepts any valid HTML. Or else do the createRange().createContextualFragment(divToInsert) trick.

You can’t just use insertAdjacentElement because an HTML string is not an element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
176
Q

What two arguments does .addEventListener take? (usually - could be three though)

A
  1. ) What type of event you want to listen for - ‘click’, ‘keyUp’, etc.
  2. ) What you want to do when that type of even is detected, in the form of a callback function.
177
Q

What are the 3 steps to listening for events?

A
  1. ) Go get something
  2. ) Listen for something on what you got
  3. ) Do something when it detects what it’s listening for.
178
Q

What is a good convention for how to name event listener functions?

A

handleWhatever (e.g. handleClick, handleDrag, handleKeyUp)

179
Q

Why shouldn’t you put () after a callback function (unless it’s returning another function)?

A

The browser will run the function for us, we won’t initiate it. If you put the parentheses afterward, you’re telling it to run on page load instead of on the event.

180
Q

What are the advantages for defining your event handlers outside of the event listener and then passing them in instead of using an anonymous function as the second argument?

A
  1. ) It allows for DRYer code - if you use an anonymous function you can’t access it, so you’ll have to define the same thing over if you want to listen on multiple things - with a function it’s very easy for multiple listeners to trigger the same action.
  2. ) If you later want to remove a listener from an element, you have to have access to the function. There’s no way to access an anonymous function. This is required by removeEventLIstener - you have to pass it the event to stop listening for, as well as the function to stop responding with in order for it to unbind the listener.
181
Q

What is binding?

A

Just taking a function and listening for a specific click against an element with it. This binds the function to the element.

182
Q

What is the first object always passed when the browser runs a callback function?

A

The event object.

183
Q

What does the isTrusted attribute on an event tell you?

A

It returns a boolean of whether the click was actually from a mouse (or whatever you’re listening for), or was programatically made.

184
Q

What does the event.pressure attribute tell you?

A

How much pressure was clicked or pressed, such as on the newer iPads and macs where you can do things differently dependent on Force Touch.

185
Q

What is the difference between event.target and event.currentTarget?

A

There’s no difference unless you have elements nested inside of the element you’re listening on - in that case, the event.target is the node that actually got clicked (even if it’s just a TextNode), while the event.currentTarget is the element that fired the event.

186
Q

Between event.target and event.currentTarget, which one do you usually want?

A

event.currentTarget, because you generally are wanting to deal with the element that triggered the event. You would use event.target if you need to do different things based off of exactly where the interaction occurred.

187
Q

What is propagation?

A

Propagation is how, if you click an area that has multiple listeners, it will propagate up the scope (bubble up) and fire all the event listeners that are within scope.

188
Q

What will event.stopPropagation() do?

A

It will stop the event (say, a click) from firing the parent scope listeners as well, and only fire up to the element which calls the method.

189
Q

What is capture?

A

Kind of the opposite effect of bubbling - capture is the path downward through the scope from parent to child, where as bubbling is going up from the most immediate child through its parents.

190
Q

How do you stop capture from going to the lowest element?

A

You can pass a third argument into addEventLIstener() which is an options object - if you set capture: true there, it will operate top-down instead of bubbling down-up

window. addEventListener(‘click’, function(event) {
console. log(‘you clicked the window’);

}, {capture: ‘true’ }

Then, the order in which events are fired will flip, and you can use stopPropagation() to prevent the event listeners at a lower level from firing.

191
Q

What does event.type() tell you?

A

It tells you what sort of event this was - say, a click, or a drag, or whatever.

192
Q

What does event.bubbles tell you?

A

It returns a boolean that tells you whether this element will bubble the eventListener upwards or if stopPropagation() has been called.

193
Q

what is the ‘mousemove’ event?

A

It detects every time the mouse moves over the element that called it.

194
Q

What is the ‘mouseenter’ event listener?

A

It detects any time the mouse moves onto the element it was called against.

195
Q

If you have a callback function, what will ‘this’ refer to?

A

The element the callback was called on. An easy way to think about this is just that it will be whatever is to the left of the dot in the event listener call.

196
Q

If you use an arrow function in a callback function, what will the ‘this’ keyword refer to?

A

In that case it will be scoped to the parent, it won’t be the element it was called against.

197
Q

What should you use instead of ‘this’ in event listener callback functions?

A

It’s probably best to always go ahead and just use e.currentTarget or e.target instead of ‘this’.

198
Q

How do you stop the default action of an HTML element?

A

You can call event.preventDefault() on an eventListener for that element, and then do whatever it is you’re stopping the default in order to do.

199
Q

What does confirm(‘some message here’) do?

A

“Similar to alert(), except it gives you a yes or no option, which you can then assign to a variable:

const shouldChangePage = confirm(““This site sucks, sure you want to go?””)

shouldChangePage will hold the true/false value of whichever they click.

200
Q

What does prompt() give you?

A

Similar to alert() or confirm(), but it gives you a text box, whose entry you can assign to a variable.

201
Q

How do you get the url of a link if you have an event listener on it?

A

event.currentTarget.href

202
Q

What is the best way to grab onto a form with JS?

A

Give the form a name, rather than a class - then grab it with an attribute selector.

203
Q

How do you grab an element by its attribute (like a name) rather than by class or element type?

A

“const signupForm = document.querySelector(‘[name=”“signup””]’);”

204
Q

How can you keep a form from submitting unless an input is filled out?

A

Just add ‘required’ to the input and it will throw an error if submit happens with no input on it.

205
Q

How can you access form elements?

A

“Within the form, it creates properties for each of the fields IF they’re given a name - so if you have an input with name=”“email””, you can then access it from a form.email attribute, accessible as event.currentTarget.email (or event.currentTarget.email.value for the raw value)

This is all the more reason to use valid semantic HTML by giving inputs a name.

206
Q

How can you test whether a string includes a particular subset value?

A

myString.includes(‘whatiamtestingfor’) will check if it is included in a given string - but it’s also case sensitive.

207
Q

What are the ‘focus’ and ‘blur’ events?

A

Focus is when a field gains focus, blur is when a field loses focus.

208
Q

What, as far as accessibility, is the difference between buttons and links?

A

Buttons are to be used for actions that happen inside of an application, links are used to change the page. Don’t mix those up or you will mess up all sorts of accessibility features in specialized readers.

209
Q

What is the main thing to keep in mind as far as accessibility and click events?

A

Things that are not keyboard accessible should not have clicks registered on them, because not everyone has a mouse.

210
Q

How can you solve the problem of an image that is clicked to enlarge, for example, being inaccessible without a mouse?

A

“You can give an element a role of button to solve this (role=”“button””) as well as a tabindex=”“0”” to make sure it can be tabbed to. But a better option would be to just wrap whatever the element is inside of a button.”

211
Q

What do you need to do if you have an image that is clickable but can’t be triggered without a mouse?

A

You can put a keyUp listener on the image:

function handlePhotoClick(event) {

if(event.type === ‘click’ || event.key === ‘Enter’) {

console.log(‘you clicked the photo!’)

}

}

212
Q

When setting up a canvas, how should you set the width and height?

A

Within the html, set the width and height to twice what you need, and then halve that in the size of the canvas using CSS - that will keep the picture from looking pixelated.

213
Q

How do you work with the canvas?

A

First you grab it, and then grab the context, passing it either 2d or 3d, and then you will have a set of APIs or methods that are used for drawing. Everything you can imagine from MS Paint is available.

const canvas = document.querySelector(‘#etch-a-sketch’);

const ctx = canvas.getContext(‘2d’);

ctx. lineJoin = ‘round’;
ctx. lineCap = ‘round’;
ctx. lineWidth = 10;
ctx. beginPath();
ctx. moveTo(x, y);
ctx. lineTo(x, y);
ctx. stroke();

214
Q

How do you set a rounded edge on the canvas pen?

A

“ctx.lineJoin = ““round”” and ctx.lineCap = ““round”””

215
Q

How do you change the width of the canvas drawing tool?

A

ctx.lineWidth = 10 - the default is 1px, and you don’t have to specify the px.

216
Q

How do you put a drawing cursor on the page?

A

You call ctx.beginPath()

217
Q

How do you move the drawing cursor, in an event listener responding to a click, or what have you?

A

ctx.moveto(x, y), where x and y are whatever value you’ve determined - typically, to move up you decrement y, to move down you increment y, and then to move right you increment to x, and then to move left you decrement x.

218
Q

What do you call on the canvas after setting your moveTo and lineTo?

A

You need to call ctx.stroke() in order to actually commit the drawing to the canvas.

219
Q

How do you listen for keyDown on your whole site?

A

Just add the listener on to the window.

220
Q

What is the default action of an arrow key?

A

To scroll the page (up or down, left or right, respectively), so you’ll have to listen for the up and down arrow key and preventDefault() on them if you don’t want that to happen.

221
Q

If your functions have six or seven parameters, what should you do instead of passing them all in and why?

A

You should make an options object and make each of the parameters an attribute on the options object. That way, it’s much easier to deal with some of them being optional, and they don’t have to all be passed in in a certain order.

222
Q

When you pass an object into a function, what’s an easy way to have top level variables instead of having to call options.thing every time?

A

Destructure the attributes you need in the parameters instead.

Not this:

function draw(options) {

console.log(options.key);

}

But this:

function draw({ key }) {

console.log(key);

}

223
Q

What’s another way to write x = x - 10?

A

x -= 10

224
Q

What is the convention for naming true constant variables?

A

name them in ALL_CAPS with underscores between words.

225
Q

What is the best site for seeing HSL values?

A

www.mothereffinghsl.com

226
Q

What is HSL?

A

Just a way to declare colors, similar to hex codes or RGB, except the ‘h’ or hue value goes from 0-360, and then once you pass 360 the browser is smart enough to modulo 360 and get the proper result to loop back around to 1 - so it’s easy for changing colors.

227
Q

How do you set the color of a canvas pen?

A

ctx.strokeStyle(‘hslValueOrHexCodeOrWhatever’)

228
Q

What do you need to remember if you set the arguments of ctx.strokeStyle dynamically?

A

It has to be called every time you want to change - it’s a one-time setting, it’s not like it’s listening for changes on the values passed in, it just affects the next stroke, not continuing to affect the ones that follow.

229
Q

What listener can you fire if you need to remove a class when an animation is finished?

A

‘animationend’

230
Q

What do you need to remember about removing event listeners?

A

If you set one up, it will remain on the object, and then if you’ve set it up within a function call or something, that will actually create a new event listener each time the function is called.

If you only want it to listen once per instance of being called and then remove itself instead of continuing to be called, you have to pass in the argument options to your addEventListener with { once: true } - then it will unbind itself.

function clearCanvas() {

canvas. classList.add(‘shake’);
ctx. clearRect(0, 0, width, height);
canvas. addEventListener(

‘animationend’,

function() {

canvas.classList.remove(‘shake’);

},

{ once: true }

);

}

231
Q

How do you clear the canvas?

A

ctx.clearRect(startX, startY, howFarToGoX, howFarToGoY) - where you pass it where to start (0, 0) if you want to clear the whole thing, and then how far to go with erasing (width of the canvas and height of the canvas if you want to wipe the whole thing)

232
Q

What does the CSS property ‘pointer-events’ do?

A

If set to ‘none’ it will tell the browser to ignore any events that come to that element, and it won’t capture them - so you’ll be able to click objects below the element, for instance.

233
Q

How could you make a hidden div (like a modal)

A

Just set it up in HTML how you want it, and then set the opacity to 0 and the pointer-events to none.

234
Q

How does .closest() look for matches?

A

It will look up the DOM tree of elements, where as querySelector() looks down the DOM tree.

235
Q

How can you get the source of an image in javascript?

A

const imgSrc = card.querySelector(‘img’).src;

236
Q

How can you replace a certain part of a string?

A

You can call .replace on the string, then pass it what text you want to replace if it exists in the string, along with what you want to replace it with.

237
Q

How can you simulate a slow cell network on your website?

A

In the developer tools, under the network tab, there are checkboxes to simulate various networks, including a slow 3G connection.

238
Q

What can you use instead of a ‘scroll’ event in order to tell when an element is on the page?

A

An IntersectionObserver.

239
Q

How do you make text scroll rather than extend the height of an element or get cut off?

A

You set the CSS property of overflow to ‘scroll’

240
Q

How can you make your code more robust, for cases where you want to use it on multiple pages where some needed elements may not exist yet, without it breaking?

A

Wrap your code in a function, then test for the needed elements and if they aren’t there, just return out of the function you’ve made, rather than have a breaking error.

241
Q

What does the .scrollTop property of an element tell you?

A

How far the element is scrolled from the top of the element.

242
Q

What property will tell you the maximum scroll of an element?

A

.scrollHeight

243
Q

What is an IntersectionObserver used for?

A

It can tell you whether an element is on the page or not. It can even tell you if the element is just partially on or off the screen.

244
Q

What is the syntax to call an IntersectionObserver?

A

const ob = new IntersectionObserver(obCallback, { root: terms, threshold: 1 });

ob.observe(terms.lastElementChild);

function obCallback(payload) {

if (payload[0].intersectionRatio === 1) {

button.disabled = false;

// stop observing the button

ob.unobserve(terms.lastElementChild);

}

}

245
Q

When does the callback function passed to an IntersectionObserver fire?

A

Every time the element is on the page, as well as everytime it leaves the page.

246
Q

After declaring your IntersectionObserver, what’s required to make it work?

A

It’s just a watcher, so you still have to tell it which elements to watch for it to work. If your observer is in a variable called ob, just call ob.observe(thingToBeObserved)

247
Q

How could you tell how long something has been on the screen?

A

An IntersectionObserver can give you the time that something has been on this page - which could be very useful in making games.

248
Q

If you have an IntersectionObserver named ob, what will ob.intersectionRatio tell you?

A

How far onto the page the element is - with 1 meaning ‘completely on’ and decimals telling us what percentage if not completely on.

249
Q

What is the second argument passed into an IntersectionObserver declaration?

A

An options object, where, for instance, you can set the root of what we are scrolling with (by default, it’s the body), as well as define the threshold - which is at what point of being on the screen it should notify the function (1 being totally on the page, .5 being half on, etc.)

const ob = new IntersectionObserver(obCallback, { root: terms, threshold: 1 });

250
Q

If you have an IntersectionObserver that won’t fire because the bottom element is never fully on the page, due to padding or whatever, what can you do?

A

You could just add an

or some type of element to bump it off of the very bottom, which will get it to go 100% on to the page.

251
Q

How do you stop an IntersectionObserver?

A

call observer.unobserve(theElementYouWereObserving);

252
Q

Similar to the IntersectionObserver, what will a ResizeObserver do?

A

It will watch and tell you (or at least your callback function) when an element has been resized to whatever percent you listen for.

253
Q

What is the aria-label attribute?

A

It tells the screen reader what this element is about.

254
Q

What is the aria-selected attribute?

A

It just tells which element should be set to active - as with tab bars and panels.

255
Q

What does ARIA stand for?

A

Accessible Rich Internet Applications

256
Q

What is ARIA?

A

A set of attributes that define ways to make web content and web apps more accesible to people with disabilities.

257
Q

What’s the easiest way to hide an element?

A

Just add the hidden attribute to it - this also makes it accessible.

258
Q

What’s different about accessing aria properties?

A

For most attributes you can just access them directly via dot notation in order to set or get, but with aria properties, you have to use the setAttribute() method on the element to set it and getAttribute() to get it.

259
Q

When should you use an accessibility attribute over a class for styling and JS?

A

Whenever you can (whenever one exists) - it saves you from having to double up and set aria attributes in addition to classes.

260
Q

What is the syntax for the array.find method?

A


261
Q

How do you convert a NodeList into an array?

A

Wrap it in an Array.from() method.

262
Q

What is the order of operations in JS?

A

Same as math - BEDMAS - Brackets (Parentheses), Exponents, Division, Multiplication, Addition, Subtraction

263
Q

How do you do exponents in JS?

A

You use the ** operator - so 2 ** 2 is 2 squared.

264
Q

What does a regex statement always start and end with?

A

/

265
Q

What does \s mean within a regex?

A

A space

266
Q

What does g mean after the closing backslash mean within regex?

A

Global - To find all things that match, not just the first thing.

267
Q

How should you do an if statement, instead of like this?

A

“Leave off the ending else block, like this:

This evaluates to the same thing because of the return keyword - which returns out of the functions and stops the rest of the function code from running.

268
Q

Why do we still have == in the language if we should almost always use === ?

A

“To check if a variable is either null OR undefined easily - since with == they evaluate to be the same - but this is very rarely needed or used, because we have the concept of ““truthy’ and ““falsey”” instead.”

269
Q

In terms of truthy/falsy - what’s the difference between ‘’ and ‘ ‘ ?

A

An empty string is falsey, while a string with any value, even a space, is true.

270
Q

Is the value 1 truthy or falsey?

A

Truthy

271
Q

Is the value -10 truthy or falsey?

A

Truthy

272
Q

Is the value ‘hello’ truthy or falsey?

A

truthy

273
Q

Is the value ‘ ‘ truthy or falsey?

A

Truthy

274
Q

Is the value ‘0’ truthy or falsey?

A

Truthy

275
Q

Is an empty array ( [] ) truthy or falsy?

A

Truthy.

276
Q

Is an empty object ( {} ) truthy or falsy?

A

Truthy

277
Q

Is the value 0 truthy or falsey?

A

Falsey

278
Q

Is the value Undefined truthy or falsey?

A

Falsey

279
Q

Is a variable set to null truthy or falsey?

A

Falsey

280
Q

Is the value NaN truthy or falsey?

A

Falsey

281
Q

Is an empty string truthy or falsey?

A

Falsey

282
Q

Is [].length truthy or falsey when it is an empty array?

A

Falsey - because its length is 0.

283
Q

Is {}.length truthy or falsey (that is, an empty object’s length)

A

Falsey, because it evaluates to zero.

284
Q

What is coercion?

A

When we force something of one type to become another type - usually things are coerced into booleans.

285
Q

How can you check if a string exists?

A

If you put a bang in front of it, that will return a boolean of whether or not it has a value in the string - true if it doesn’t have a value, false if it does. You can use a double bang (!!) to get the more logical true if it does have a value, false if it doesn’t.

286
Q

How can you check if a variable is set to an empty string?

A

!emptyStringName will return true if it is an empty string, whereas !!emptyStringName will return false if it is an empty string.

287
Q

Why are bang (!) and bang bang (!!) used with strings?

A

In if statements we use them to take a truth or falsey value and coerce it into a real boolean true and false value.

288
Q

What is the && trick?

A

“Well, the cool thing about chaining &&s in JS is that if any condition fails along the way (or otherwise returns false), the chain will stop running and immediately return without running any of the rest of the commands. This is referred to as short circuiting.

This can be manipulated so that instead of writing:

You can write

If isAdmin is false, showAdminBar will never run. This is extremely common in React, when deciding whether to display a component or not.

Some people hate this, but it seems reasonable to most and is very common.

289
Q

What is a blockless if statement?

A

“If an if statement is on one line, you can leave off the curly brackets:

Again, this is purely stylistic in choice - it makes sense to just keep the curly brackets to be more readable, and otherwise, if you move the result to a second line, the statement will be broken and not work - it must all be on one line.

290
Q

What two arguments does setTimeout() take?

A

“A callback function and the number of milliseconds to run that callback after.

291
Q

Is it true that if you use a timer, everything outside of it will stop running and wait for the timer to complete before moving on to the next line?

A

Nah. You’re thinking of Async/Await - as soon as it has stored a reference to the timeout, it will continue with the code and just listen for the number of milliseconds to be up, and then will call the callback function. It is asynchronous.

292
Q

What is the difference between setTimeout() and setInterval()

A

setTImeout will run once after the given number of milliseconds, setInterval will repeatedly run every few milliseconds

293
Q

What is the syntax for setInterval()?

A


294
Q

setInterval doesn’t run right away - T/F?

A

True - it will wait the specified amount of time before running for the first time.

295
Q

How can you arrange for setInterval to run immediately?

A

“You could roll your own setImmediateInterval() function -

296
Q

What’s the key thing to remember if you will need to clear an interval or timeout?

A

You msut save the reference to it into a variable - there’s no other way to stop an interval or timeout. Once that’s done, you can call clearTimeout(referenceToRunningTimer) or clearInterval(referenceToRunningInterval)

297
Q

What if you want an interval to stop, but only after running for a specified amount of time?

A

“You can set a clearInterval call within a setTimeout:

This will run the interval for 3 seconds, and then clear the interval.

298
Q

When should you use an object over an array?

A

When the order doesn’t matter - there’s no guarantee object properties will appear in the order you set them in.

299
Q

Object values must be what data type?

A

Any type - even other objects or arrays or variables set elsewhere or functions

300
Q

What can you do if the object property and the value variable you want to set it to are the same?

A

You can just list the variable - so if both are named age, you just put ‘age,’ in the object properties list instead of doing ‘age: age’

301
Q

Do object keys have to be in the format of

name: ‘wes’ ?

A

No, you can also have keys with dashes and spaces in them if needed, just list them in a string. This is different from variable names which cannot have either of those things.

302
Q

What’s the reason you hadn’t thought of before for leaving a trailing comma?

A

Otherwise, if someone else comes in and adds a property, and adds a comma to your line, then the GIT Blame is messed up and shows that line as theirs, so you can’t figure out who messed up as easily.

303
Q

const means that the value of an object cannot be changed, true/false?

A

No - it means the binding of the variable can’t change - so if you define an object with const, any of its properties can change, but you can’t set up a new object to overwrite it with the same name.

Like, as a human grows up, many of their characteristics and particulars will change, but they are still themselves, and can’t be another person.

304
Q

What if you do want a variable or object to be immutable?

A

Instead of using const, you’d use the ‘freeze’ keyword.

Object.freeze(newObject)

Then, none of its properties can be overwritten.

305
Q

How do you call an object when you are setting which key to call programmatically?

A

If you want to programmatically call a property, that is, when the exact key may change depending on how the variable is set - then you’ll need to use square bracket notation to pass it the variable - through dot notation it will look for the variable in the object’s properties, which ain’t what you want.

306
Q

How can you access object properties with a - in them or a space?

A

Use square bracket notation and stick them within a string. This will likely only happen if your values are coming from a different language, or someone is very messed up in their Javascript understanding, but there it is.

307
Q

What will the eventual deep check ability in JS look like?

A


308
Q

“Rewrite this into the new deep check syntax:

A


309
Q

What do we do if we have an element where we aren’t sure if it will exist or not at any given time?

A

“We have to first check for its existence before we do anything with its further properties or we’ll get an error:

310
Q

How do you remove a property from an object?

A

You just call delete:

delete wes.job

311
Q

What will it return if you console.log the result of a delete statement (or the statement itself)

A

It will return a true value if the value has been deleted and a false if it has not.

312
Q

What is the difference between a method and a function?

A

A method is just a function which lives inside of an object.

313
Q

How are booleans, numbers and strings copied?

A

They are copied by value, so you can change the copy without changing the original.

314
Q

If you set an object to a different object, and then change one of the properties, what happens?

A

The properties of both objects change, because the reference has changed. This is also true for arrays, maps, and sets.

315
Q

How CAN you make a valuecopy of an array/object/set/map, not a copy by reference?

A

“Use the spread operation (…object) and assign it to a new variable.

316
Q

What is the old way of copying an object by value instead of reference?

A

But this is very rarely used anymore - just something to know in case you see it in legacy code.

317
Q

What is the gotcha with using spread to copy an object by value?

A

The copy by value with a spread only goes 1-level deep - so if there are sub-objects, they will be copied by reference. Sometimes that’s what you want, but if you want to copy the sub objects by value (so they can be changed without changing the original) you have to use Lodash currently.

318
Q

How can you do a deep copy beyond one level of an object/array/set/map?

A

You can use the cloneDeep function from LoDash.

319
Q

How do you use the cloneDeep option from LoDash?

A

“First, load LoDash from unpkg -

Then you can use _.cloneDeep:

320
Q

What is LoDash really good for?

A

When you need some complicated, harder methods than is available in standard JS - especially in regards to arrays.

321
Q

Give an example of using spread to merge objects into a new one, please?

A


322
Q

How many objects can you spread into a new one?

A

As many as you want.

323
Q

What if you spread in objects with multiple properties with the same name but a different value?

A

The last one spread in will win, so the order you spread them in in matters.

324
Q

What happens if you pass an object into a function?

A

It will modify the external object as well as the internal if you make any changes - as opposed to a string, where only the value of the internal string would change.

325
Q

What is the syntax for a Map?

A


326
Q

What two arguments does MyMap.set() take?

A

“The key of the map, and the value for that key:

327
Q

Why can’t you use dot notation to add a property to a map?

A

Using dot notation will make the entered value a property on the map, instead of a value within the map. You must use .set()

328
Q

What method can you call on a map to see if it contains a value?

A

.has()

329
Q

What method can you call to delete a value from a map?

A

.delete()

330
Q

What is a big benefit of using a map, beyond properties being ordered?

A

A key can be any type - not just a string. You can use a number as a key. This is different from an object, where, yes, the value can be any type - but the key itself must be a string or a key.

331
Q

What is a dictionary in JS?

A

A way to store additional metadata about something.

332
Q

What is the use of Maps in congruence with Objects?

A

“You can use maps as a dictionary, to store additional metadata.

Maps let you use a reference to an object as a key in your Map, like so:

333
Q

Do you remember the example using a Map to set the prize for certain scores?

A


334
Q

What is the equivalent of Array.length for Maps?

A

myMap.size

335
Q

With a Map is the order of properties guaranteed?

A

Yes, unlike with an object.

336
Q

What two methods let us loop over a Map?

A

forOf and forEach

337
Q

What is the syntax of a Map forOf loop?

A


338
Q

What’s that cool example of using destructuring in a For Of loop on a map to get variable names that make sense rather than having to do array[0], array[1] and the like?

A


339
Q

When do you use a Map over an Object?

A

Mainly, if you need to maintain the order of your items. Also, they’re useful as dictionaries with meta info about an object.

340
Q

How do you do a literal for maps? (e.g. like const newObject = {dummy: ‘thing’}

A

You can’t - with Maps you have to use the const newMap = new Map(); syntax

341
Q

How are Objects and Maps different in regards to methods?

A

You can’t put functions inside of Maps like you can Objects - Maps are simply for storing data.

342
Q

What’s the catch on Maps with JSON?

A

JSON doesn’t recognize maps right now - you have to convert the Map to an Object before being able to stringify it - something like Object.fromEntries(myMap) - but that won’t be perfect - if you’re using the cool benefits like using an object as a key, those get all messed up and don’t really work when JSON’d.

343
Q

What types can array items be?

A

Any type at all.

344
Q

How do you call an Array’s keys?

A

“It has no keys, silly - TRICKS! That’s the main difference between an object and an array - the only ““key-like”” thing with an array is its index.”

345
Q

How do you check if something is an Array?

A

You can call Array.isArray(thingToCheck) - this returns true/false.

You can’t use typeOf because an Array isn’t its own type - just a special kind of object.

346
Q

What is the difference between mutable and immutable Array methods?

A

Mutable methods change the original array, whereas immutable methods don’t, they just return a new array.

347
Q

Is Array.reverse() a mutable or immutable method?

A

Mutable. It will change the orginal array, even if you’re using it in order to set a new variable.

348
Q

What does Array.slice() do?

A

It lets you take a portion of the array and make it into a new array. It is an immutable method - it won’t change the original data.

349
Q

How can you use an Array mutable method but not mutate the original array?

A

“Take a copy of the original array using spread notation, prior to calling the mutable method on it:

350
Q

How do you add an element to the beginning of an array?

A

“Use Array.unshift(‘thingToAdd’):

351
Q

What is the difference between Array.slice() and Array.splice() ?

A

.slice is immutable and .splice is mutable - that is, .splice will actually carve the chunk you describe out of the original array.

352
Q

How do the arguments given to Array.slice() and Array.splice() differ?

A

.slice() takes a starting index and an ending index, whereas .splice() takes a starting index and then how many indices to go forward.

353
Q

How do you add to the middle of an array?

A

“It’s best to use spread to do this:

You spread in a slice up until where you want to enter, enter the new value, then spread in a slice from where you left off until the end.

354
Q

What happens if you don’t pass Array.slice() a second argument?

A

It will start from the index given as the first argument and just go until the end of the array.

355
Q

How do you remove an element from an Array?

A

“You just slice twice - once up until the thing you want to leave out, and then continue from the next one:

356
Q

Which indices will be sliced by the following command:

newBikes.slice(0, 3)

A

newBikes[0], newBikes[1], and newBikes[2]

(slice is non-inclusive of the second argument given)

357
Q

What does Array.findIndex() do?

A

“It loops over every single item in the array, then returns the index of the element that matches the condition:

358
Q

“Can you refactor the following code?

A

“You don’t need an if statement because the === already returns a true or false. THen we can use an implicit return instead of an explicit return:

359
Q

What does Array.flat() do?

A

It will flatten any nested arrays into the main one.

360
Q

What is the difference between static and instance methods?

A

Instance methods are called against individual instances of the element, whereas static methods are called against the class itself.

361
Q

How do you use Array.of()?

A

“You pass it in whatever you’d like to make an array from, and they will become elements in the new array. This is rarely used as you can almost always just use the array literal notation ( myArray = [] )

362
Q

“Thinking about spread notation - what would something like ““myArray = […‘robb’] “” do?”

A

It would set the new array to each iterated letter - so [‘r’, ‘o’, ‘b’, ‘b’]

363
Q

What does the first argument to Array.from() need to be?

A

Something that is iterable - that is, something with a length property. This can be an object that has been given a length property.

364
Q

“What is the following code doing?

A

It’s creating a range with a dynamic number of elements,

365
Q

What is the difference between Object.entries(), Object.keys() and Object.values()?

A

“Think of an object like this:

Object.entries(meats) will return a 2d array, where the inner arrays are each of the key and value pairs.

Object.keys(meats) will return an array of just the keys.

Object.values(meats) will return an array of each of the quantities - which would be useful if we then wanted to total them up with .reduce()

366
Q

Let’s say you have an Object, and you want to take the entries for each key/value pair in that object and put them into variables within a method - how would you do that?

A

“Use destructuring to make this easier on yourself:

or this would be even more concise:

367
Q

What does myArray.join() do?

A

“It turns the array into a string joined together with a comma. This method takes an argument, which is just what you want to join on - e.g. myArray.join(‘, ‘) or myArray.join(‘ or “”) - by default, elements are joined with a comma.”

368
Q

What does myString.split() do?

A

“It will split a string on whatever character we want (usually a comma) and break it up into separate elements within an array.

369
Q

What happens if you don’t pass an argument to myString.split() ?

A

It will, by default, split every letter of the string, since you passed no argument of which it should separate on.

370
Q

What happens if something you’re trying to split has emojis?

A

It don’t work, jerk. Emojis are typically 2-3 emojis layered on top of each other, so they break the way split() normally does stuff.

371
Q

What does myArray.pop() do?

A

It returns the last item in the array, and removes it from the array.

372
Q

“How can you do the following without mutating the array?

A


373
Q

What does myArray.push() do?

A

It puts a variable onto the end of the array.

374
Q

What does myArray.push() return if you set it to a variable?

A

The new length of the array, just in case you need it.

375
Q

If myArray.push() and myArray.pop() put things on and take things off the end of an array - how do you do the same for the beginning of an array?

A

myArray.shift() and myArray.unshift()

376
Q

What three optional arguments can anArray.find() pass to whatever callback function you set?

A
  1. ) The individual element you are looping over.
  2. ) The index of that element.
  3. ) The entire array itself.
377
Q

Give an example of using anArray.find() with a callback function.

A


378
Q

What’s a solution if you have a large number of functions that you are re-using?

A

“It can be nice, structurally, to put them all inside a util object, and then call them with util.findBurger or whatever.

379
Q

What are functions that return other functions called?

A

High Order Functions.

380
Q

What can you do when you have two functions that are doing almost exactly the same thing?

A

“Instead, create a function that creates those functions for us:

Here’s how to call it:

This is so flexible - pass in whatever word you want, and it returns a referemce to a function to feed to find and grab it.

381
Q

How does array.filter() work?

A

“It takes an argument and uses that to return a new array - the argument is usually an if/else or some other true/false arrangement that determines to carry the element to the new array if true, and not to carry it if it’s false.

382
Q

“Refactor this:

A


383
Q

“Make a function generator that can handle multiple input minimum ratings from the following:

A


384
Q

What is so awesome about a function generator, as pertains to both filter or find?

A

What’s cool is that, if you have a generator, you can pass it to both, and one will give you the first match, the other will give you a new array of all matching occurences, and that’s flippin cool. Both from one function!

385
Q

“Given this data - remove the 1 star review:

A


386
Q

What does the array.some() method do?

A

It tests whether at least one element in the array passes the test you define, and then it returrns a boolean.

387
Q

What does anArray.some() do?

A

It tests whether at least one element in the array passes the test, and returns a boolean.

388
Q

What does anArray.every() do?

A

It checks that every member of the array meets whatever criteria you pass as an argument, and returns a boolean of whether or not they done did.

389
Q

What is the difference between anArray.some() and anArray.every()?

A

Kind of what you’d expect - .some() just checks that at least one meets the criteria whereas .every() checks that everything in the array meets the criteria (the ‘criteria’ being whatever you set in your function within the .some() or .every() call.

390
Q

What happens if you call anArray.sort() on an array full of numbers.

A

You probably get confused, because by default .sort() treats things as strings, so it would do something like 1, 100, 2 and so forth.

391
Q

How do you sort numbers qua numbers with the .sort() method?

A

“Pass it a funky function, brotha. It takes that callback function goodness, and gives you arguments for the firstItem and secondItem, then you write a test it repeatedly runs until there are no changes.

Which is all to say:

392
Q

How does .sort() interpret what comes back from a callback function within it?

A

If it returns less than zero, it will put the first item first.
If it returns 0, it leaves the two items alone.

If it returns greater than zero, it puts the second item first.

393
Q

“Refactor this noise:

A


394
Q

“Take this and break it into a function:

A


395
Q

What is the way most loops work?

A

a method lives on an array (forEach, or map or reduce or filter, etc.), and we pass it a callback function, or else define an anonymous function - it then runs once for every item in the array, giving us access to each individual item.

396
Q

What is the most basic array method?

A

The .forEach method.

397
Q

How is .forEach different from the other array methods?

A

It doesn’t return anything - it simply takes in the elements of an array and does some work on them. If you assign it to a variable, the variable returns undefined.

398
Q

“Given this function, and an array called ““faces”” - map over each and add arms to each face.

A

const toys = faces.map(addArms);

399
Q

“Take this array, map over each name and add the last name of ‘Schuneman’ to each name.

A

” ”

400
Q

What is forEach mainly used for, that is different from other array methods?

A

forEach is great for side effects - reaching outside of the current function to do something else (like put data on the page, or attach an event listener, for example)

Other loops just take in data, do something with it, then return the modified, massaged or transformed data.

401
Q

What is a Pure Function?

A

Functions that take in data, return the same length of data, and always work the same way given the data that is supplied without reaching outside themselves

402
Q

If I call .map on an array with a length of 8, what can I be guaranteed about the result, regardless of what transformations are called within .map?

A

The length of the array on the other side will be 8. .map always returns the same length of array as what is passed to it.

403
Q

How do you return fewer or more items than you pass in with array.map?

A

No can do.

404
Q

What’s one handy way to achieve multiple transformations on an array?

A

You could chain multiple maps together in order to achieve multiple transformations. You could also do multiple transformations in a single .map callback function, but chaining them will keep your functions single-purposed.

405
Q

What does the string method .repeat() do?

A

It repeats the string however many times you pass it as the argument.

406
Q

What does Array(3) do?

A

It would create an array with 3 indices, all of them empty.

407
Q

What does anArray.fill do?

A

It fills the array’s empty indices with whatever you pass it as an argument, just repeated across all empty indices. (I’m not actually sure if it’s only empty indices, or if it overwrites pre-filled indices.)

408
Q

Why is .map used so often in dealing with API returns?

A

an API usually returns a bunch of data, but it isn’t in the format you need - you use map to massage the data and return it formatted how you need it for your purposes.

409
Q

What should you never do from a .map?

A

Don’t go off and update the DOM - that’s what a .forEach is for.

410
Q

What does ‘new Date’ return if you don’t pass it anything?

A

The current date - if you pass it a string of a date, it will convert it into a Date object.

411
Q

If you need to compare two dates and figure out how much time is between them, and for some reason you don’t want to use date-fns or moment - What would you need to do?

A

You need to convert them to timestamps. You do this by calling .getTime on a JS Date object, which returns to us a random-looking number that is actually the number of milliseconds since January 1, 1970.

412
Q

What is a shortcut to get the current TimeStamp?

A

You can call Date.now(), rather than creating a new Date object and calling getTime() on it.

413
Q

What’s a good site to convert timestamps to human dates and vice verse?

A

“Epoch.now.sh”

414
Q

“Convert this function into a function generator (so it can handle more than one hard-coded id)

A

” ”

415
Q

Given an array of people objects named cleanPeople, each with an age attribute that is a number - filter to only include those over40.

A

” ”

416
Q

“Ok, so you have a findById higher order function. Good job. Can you make it more flexible where you could search by whatever prop you want?

A

” ”

417
Q

Explain the basic difference between Map, Filter, and Reduce

A

Map takes in an item and returns a transformed version of that item.

Filter takes in items and returns a filtered subset of those items.

Reduce takes in an array of data and returns a tally of some part of those items. It could be a single number total, or an object representing instances of something.

418
Q

What’s wrong with totaling everything up this way?

A

You’re having to reach outside of the function to do it, which is a side effect.

419
Q

“Explain this image:

A

Map transforms each item in the array, filter edits out the parts that don’t work, while reduce puts everything together into something new.

420
Q

What two arguments does reduce pass to the callback function defined within it?

A

An accumulator, and a currentValue - the accumulator is the running tally, while currentValue is each iteration’s individual value.

421
Q

What is the second argument passed into a .reduce() function?

A

The value at which you’d like to start the accumulator - after this the accumulator always goes off the return of the last iteration, but the first time in you can define the starting value. Often, for totaling something up, this will be 0.

422
Q

What is the accumulator in a .reduce() loop?

A

“What is passed in as the return value from each iteration of the loop. If you don’t return anything, the accumulator is undefined after the first time. If you return something static like ““hello”” - each iteration will just return ““hello””. To accumulate, add the currentValue to the tally, and return that.”

423
Q

What is a gotcha when using reduce to total up attributes on an object?

A

“If the attribute key is undefined, and you try to add to it, undefined + 1 = NaN, and that will mess up the rest. We have to catch the case where it’s undefined and set an initial value.

Something like:

424
Q

“Take the following and code how to find the number of occurences of each type of clothing:

A

” ”

425
Q

“given the following, add up the inventory price in one line.

A

” ”

426
Q

How would you take all the text on a webpage and figure out which letter/number is used most often?

A

” ”

427
Q

What three things does a for loop take?

A

An initial expression, an exit condition, and an increment expression.

428
Q

When is the plain for loop useful?

A

“Running a code block N number of times.

429
Q

What was the former trouble with the for loop and the var keyword to declare variables?

A

at the end, you’d always have a global variable sitting around with the last value put into it. let fixed this since it is block scoped.

430
Q

What is the now much easier way to do a for loop?

A

We use a forEach, map, or reduce to execute similar functions.

431
Q

What is one major use case where we might still use the old for loop?

A

The canvas element has a getImageData method that pulls the RGBA values for every pixel - since there are 4 values, it’s useful to be able to jump over and increment by 4 when looping over them - that would be harder with map or forEach.

432
Q

What’s a cool little side thing about for of as opposed to forEach?

A

It can handle emojis - forEach gets thrown off by the fact emojis are multiple images blended together, but for of handles them like you’d think it should - listing out each image that is part of the composite.

433
Q

What is a major advantage of the for of loop over forEach?

A

If you are having to sequence a large amount of data that takes time to process, you can do async and await within for of, which you can’t inside of a forEach.

434
Q

What’s a downside of the for of loop compared to forEach?

A

It doesn’t give us the index of our item, it also doesn’t allow us to filter or anything like that.

435
Q

How is for in better than Object.entries?

A

It’s not..but it’s there - you might see it somewhere. It just also loops over keys.

436
Q

What’s one advantage of using for in?

A

If you have a prototyped object, for in will grab the prototyped keys as well as the explicit ones - Object.entries only grabs the explicit keys.

437
Q

How does the while loop work?

A

It takes a condition and runs infinitely until that exit condition is met.

438
Q

What’s the difference between a while loop and a do while?

A

The do while loop will execute the first go round prior to checking the condition, whereas while will check prior to executing.