Jeopardy Flashcards

1
Q

What are tenets of functional programming?

A
  • Functional purity - no side-effects, output derived only from input
  • Simple functions
  • First class functions (functions as variables)
  • Higher order functions (functions that return functions or accept functions as arguments)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a closure, and how/why would you use one?

A
  • A closure is a function that maintains a reference to its outer scope.
  • These are useful in high order functions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the reference data types in JavaScript?

A
  • Object. Just object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between ‘null’ and ‘undefined’

A
  • Undefined has no value

- Null is explicitly set to an empty value

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

What are the primitive data types in JavaScript?

A
  • Boolean
  • Null
  • Undefined
  • Number
  • String
  • Symbol (new in ECMAScript 2015)
  • Everything that is not a primitive data type is an object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between a primitive data type and a reference data type?

A
  • Primitive data types have a fixed amount of memory that they take up
  • A reference data type is made up of references to other variables and does not take up a fixed amount of memory
  • Primitive types include booleans or integers
  • Reference types include arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain hoisting

A
  • Hoisting is JavaScript’s method of moving declarations to the top of the current scope before the code in the rest of the scope executes
  • Makes the function available everywhere in the scope
  • To avoid bugs, always declare all variables at the beginning of every scope.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What creates scope in JavaScript?

A
  • There’s global and function scope in ES5. In ES2015 there’s also block scope.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Name 2 or more ways to define a global variable in JavaScript

A
  • Assign in the top level scope

- Leave the ‘var’ off and assign it to the ‘window’

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

How does inheritance work in JavaScript?

A
  • Inheritance in JavaScript is prototypal.

- Objects are linked to other object instances via prototypes

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

Explain how prototypal inheritance works

A
  • Every JavaScript object has a prototype property (empty by default), and you attach properties and methods on this prototype property when you want to implement inheritance.
  • By adding methods and properties to a prototype, you make those methods and properties available later on.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between ‘==’ and ‘===’ in JavaScript?

A
  • ‘==’ (type coercison) checks for equality of value, while ‘===’ checks for equality of both value and data type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is async in JavaScript?

A
  • Async is short for “asynchronous”
  • Asynchronous code takes statements outside of the main program flow, allowing the code after the asynchronous call to be executed immediately without waiting.
  • This allows multiple, independent tasks to be executed without slowing down the UI.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the difference between synchronous and asynchronous functions.

A
  • Synchronous functions must be executed in order. The first queued function must be completed before the second one is.
  • Asynchronous functions are executed immediately, without waiting for other functions to execute or resolve.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is event loop?

A
  • The event loop listens for queued up messages and adds instructions to the call stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the ‘this’ refer to in Javascript

A
  • The first value passed to call() or apply()
  • The value that was binded to the function
  • The calling object
  • The global scope
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a data type?

A
  • A classification of data which tells the compiler or interpreter how the programmer intends to use the data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why does (1 /3).toFixed(2) + 3 equal 0.333?

A
  • toFixed() coerces to a string, which makes + act as a concatenation operator, so it appends 3 to the resulting string
19
Q

How big can a number be in JS?

A
  • The range of safe numbers in JS is -(2^53 - 1) to (2^53 -1). This is roughly 9 quadrillion values
20
Q

Why doesn’t .1 + .2 = .3 in JS?

A
  • JS stores data as double-precision floating point numbers, which can’t accurately represent the infinite amount of decimal numbers that exist
21
Q

What is scope?

A
  • Scope is a container where references to variables and functions are shared
22
Q

What is the difference b/w var, let and const?

A
  • Var is scoped to the function, let and const are scoped to the block.
  • Cosnt cannot be reassigned
23
Q

Why is it dangerous to pollute or use the global scope?

A
  • Libraries and frameworks might have modified the global scope, which makes it unreliable.
24
Q

You declare a variable in the middle of a scope. Where is that variable accessible?

A
  • Anywhere after the declaration in the same scope, as well as any nested scopes created after the declaration.
25
Q

What does it mean to shadow a variable?

A
  • Redirecting a variable that’s available in an outer scope, which removes access to it
26
Q

What is the difference between declaring a function vs declaring a function expression?

A
  • A declared function is hoisted, a function expression is not
27
Q

What is the DOM?

A
  • The document object model, the API the browser exposes for web pages
28
Q

How do you create a div element?

A
  • const div = document.createElement(‘div’)
29
Q

What are 5 ways you can access a DOM element?

A
  • document.querySelector
  • document.querySelectorAll
  • document.getElementById
  • document.getElementsByClass
  • document.getElementsByTagName
30
Q

How do you access the ‘form’ element on a form submission event?

A
  • event.target
31
Q

How do you set the background-color of a given DOM element to red?

A
  • element.style.backgroundColor = red
32
Q

How do you access the ‘src’ attribute of an ‘img’ element?

A
  • WIth ‘img.src’ or img['src']
33
Q

What’s the difference between setting the innerHTML of an element and using appendChild()?

A
  • innerHTML completely replaces the content and takes a string; appendChild() adds to the element’s children and takes a DOM element
34
Q

What is event bubbling?

A
  • An event that happens on an element also happens on all parent elements sequentially unless event.stopPropagation is called
35
Q

What are the 3 phases of a DOM event firing?

A
  • Capture (going from the body down to the element that fired)
  • Target (when the innermost element fires)
  • Bubbling (when the event comes back through the DOM)
36
Q

Compare and contrast a NodeList and an Array

A
  • Both are collections, but they have different prototypes and therefore different methods available.
37
Q

How would you check if a number is an integer?

A
  • Check and see if there is a remainder left when dividing by 1

> function isInt(num) {
> return num % 1 === 0
}

console. log(isInt(4)) // true
console. log(isInt(12.2)) // false

38
Q

What will the following code output?

> (function() {
>> var a = b = 5:
> }) ();

console.log(b)

A
  • Since b is not declared anywhere in the function w/ var, it is set to equal 5 in the global scope.

The code is interpreted as such:

var a = b
b = 5
39
Q

Write a fn that will allow you to do this: multiply(5)(6)

A
  • This is where you would create a closure, thereby keeping the value of the outer function even after the inner function is returned.
> function multiply(a) {
>> return function(b) {
>>> return a * b;
>> }
> }

multiply(5)(6) // 30

40
Q

When would you use a bind function?

A
  • When you have a particular fn that you want to call with a specific ‘this’ value. You can then use bind to pass a specific object to a fn that uses a ‘this’ reference

> function fullName() {
> return ‘hello, this is ‘ + this.first + ‘ ‘ + this.last;

// create a person object and pass its values to fullName fn

> var person = (first: ‘Foo’, last: ‘Bar’);

console.log(fullName.bind(person)()) // Hello this is Foo Bar

41
Q

What does ‘use strict’ do?

A
  • ‘use strict’ is a literal entered at the top of a JS program or fn, and helps you write safer JS code by throwing an err if a global var is created by mistake.

A program like this will throw an err w/ use strict:

> function doSomething(val) {
> ‘use strict’;
> x = val + 10;
}

This will throw an err because ‘x’ was not defined and is being set to some value in the global scope.

42
Q

How would you add your own method to the Array object so the following code would work?

> var arr = [1, 2, 3, 4, 5]
> var avg = arr.average()

console.log(avg)

A
  • Since this calls for adding a method to the global Array object, you should modify the Array prototype
> Array.prototype.average = function() {
>> //calculate sum
>> var sum = this.reduce(function(prev, cur) {return prev + cur;});
>> // return sum divided by # of elements
>> return sum / this.length
> } 

console.log(avg); // 3

43
Q

What is a callback function?

A
  • A fn that is passed to another fn as an argument and is executed after some operation has been completed.
> function modifyArray(arr, callback) {
>> // do something to arr here
>> arr.push(100);
>> // then execute the callback fn that was passed
>> callback();
> }

> > var arr = [1, 2, 3, 4, 5]

> modify Array(arr, function() {
> console.log(‘arr has been modified’, arr)
}

44
Q

How would you create a private variable?

A
  • Create it as a local variable within a function, so that even if the function is executed the variable cannot be accessed outside of the function.