LS 101 Flashcards

LS101 Methods, Terminology, and Concepts

1
Q

What is the difference between Git and Github?

A

Git is a distributed version control system designed to be used locally without a central repository.
Github is a cloud based remote repository which acts as a centralized repo for git to enable collaboration.

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

When should you nest Git repositories?

A

You should never nest Git repositories

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

What are the built-in (primitive )JavaScript data types?

A

String, Number, Boolean, Undefined, Null

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

Does Javascript have Integer and Float specific Numeric data types?

A

No. Every numeric value has the primitive type of Number and is represented using a floating point number system.

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

Are all primitive values immutable?

A

Yes. String, Number, Boolean, Undefined, and Null are all immutable.

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

Is NaN a primitive value?

A

Yes. NaN is technically a number so it is a primitive.

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

Does this contain a statement, an expression, or both?

A

Both.A variable declaration with an optional assignment is an expression.Anything valid to the right of = is an expression, so 3 is an expression.

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

The code to the right of the assignment operator = is an ??????????.

A

an expression

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

Is the reassignment of myVariable from a String to a Number valid since they are different data types?Is the original value (‘Hello, World’) changed by the reassignment?

A

No, JavaScript variables are not type specific.No, that is a primitive so the reassignment either creates a new value or assigns the variable to refer to an object.

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

When using == to compare a number and a string, what happens as far as coercion?

A

The string is always coerced to a number regardless of which side the number/string is on and then the comparison is made using === with the coerced value.

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

What is the return value in JavaScript of coercing a non numeric string (“blue”) to a number?

A

NaN

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

What does typeof NaN return?

A

number, NaN is an instance of the primitive type number

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

Return Values:

A

The return values are different since theletstatement always returnsundefined, but assignment expressions that aren’t part of a statement return the new value for the variable.

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

What is always the return value of a let/const statement?

A

undefined

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

What is the return value of this snippet:

A

undefined. an assignment using the let keyword will always return undefined.

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

What is the return value of this snippet:

A

false, an assignment not using the let/const/var keyword always returns the value on the right side of the =.

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

How do you create a single line comment in JavaScript?

A

use // to make the entire line a comment

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

What is best practice for max line length in JS?

A

80 characters

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

What formatting is used for variable names and function names?

A

camelCase

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

What is different about the naming convention for a constructor function?

A

You use PascalCase instead of camelCase.

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

What is different about the naming convention for a constant?

A

You use SCREAMING_SNAKE_CASE to name a constant

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

What are the only type of characters that should be used when naming variables, constants, and functions in JavaScript?

A

Alphanumeric characters (a-z, A-Z without accents) and numeric characters.When naming a variable the first character should ALWAYS be alphabetic.Constants may use underscores$ is allowed in names but only in specific cases, they are legal but should not be used in general.

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

let 5Miles = 5280 * 5;is this a valid variable declaration?

A

The declaration will work but you shouldn’t ever start a variable/function name with a numeric character.

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

Does this snippet adhere to code the JavaScript style guide:

A

Yes, there must be a space between the key word and condition check and it is best practice to put a space between the conditional and the opening curly bracket.

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

Should you always use a space between an operator and operand in Javascript?

A

Yes, while it will work correctly without the spaces it makes it harder to read with them.

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

When should you not end a line with “;”

A

Use semicolons to terminate each logical line of code unless the line ends with{,}, or:.

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

What is a short circuit operator?

A

In an && or || statement, JavaScrip stops evaluating sub expressions as soon as it can be 100% certain of the result of the final value.If the first expression evaluated in an && returns a falsy value, it doesn’t matter what the other side returns so it immediately evaluates to false.The opposite occurs with || since a truthy value on either side results in it evaluating to true.

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

What are the falsy values in JavaScript?

A

falseundefinednull”” (empty string)0NaN

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

How does the conditional in this code snippet use short circuit operator behavior to ensure it runs?

A

If name is null/undefined, there would be an error when the conditional executed if we didn’t leverage the behavior of && to prevent name.length from being evaluated if it doesn’t exist.In a comparison using &&, if the first value evaluates to false the second expression will never be evaluated, so even if name is null/undefined the code won’t stop executing because the comparison will immediately evaluate to false and the code in the else block will run.

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

Is it true that JavaScript never evaluates the right side of a short circuit operator when the expression short circuits?

A

Yes, the right side of an && / || comparison will never be evaluated if the result is settled by the first evaluation. (false on the left for &&, true on the left for ||).

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

What’s wrong with this snippet?

A

Even though it may work as intended, putting an assignment operator in a conditional is highly discouraged.

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

What is implicit type coercion?

A

Implicit type coercion occurs when you perform an operation on two different data types and JavaScript coerces one of them to match the other. It is not coercion explicitly performed by the developer.

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

What dictates how values get coerced?

A

The operator used in the expression, the most common operators are ==and+.

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

When does the non-strict equality operator (==) work identically to the strict equality operator?

A

When the operands have the same type.

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

What happens during a comparison of two operands with two different types using the non-strict equality operator?

A

One of the operands is implicitly converted.

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

When comparing operands of two different types with the strict-equality operator will the result ever be true?

A

No. You will always get a false result when comparing operands of 2 different types.

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

As far as implicit conversion, what is happening in this snippet?

A

When comparing a string to a number, the string is always coerced into a number. Then the comparison is done using the converted values.1 === 1> true

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

What happens when you compare a boolean with any other type using ==?

A

The boolean is converted to it’s numeric equivalent, 1 is true and 0 is false. Then the comparison is performed using the numerical value.

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

What is happening in this snippet as far as implicit coercion?

A

A boolean compared to any other value type using == is converted to a number (true is 1 false is 0).Now the comparison is ‘1’ == 1, when you compare a number to a string, the string is converted to it’s numerical equivalent or NaN.Now the operands have the same type and the comparison is 1 === 1.

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

What does this return?

A

true, the non-strict equality operator considers these equivalent.

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

What are we actually checking for in the comparison after the if in this snippet?

A

We’re checking if val is undefined or null since we are using the == to perform the comparison. This is not a best practice though and shouldn’t be used.

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

How does == compare to === when comparing objects instead of primitive values?

A

It works exactly the same in that it returns true only if the two objects are the exact same object.

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

What does this return?

A

True, When you compare a plain object {} to a string using ==, it converts the object into the string’[object Object]’ and then performs the comparison.

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

What does this return?

A

True, when comparing an empty array to a string the empty array is converted to an empty string (‘’).

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

When should == be used?

A

Most developers recommend to never use it. But the argument has been made that if you are comparing operands of different types in your code then something is wrong with the code.During Launch School we do not use it.

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

How does the unary plus (+) operator work?

A

It converts all string representations of numbers, booleans, and null to numbers. They can be integers, floats, hexadecimal, exponents, and Infinity.If it cannot return a number it will return NaN.

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

In this snippet what are the terms for.A. x and yB. + and =C. z

A

A. those are operandsB. those are operatorsC. that is the result

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

What is a unary operator?

A

A unary operator is one that takes a single operand/argument and performs an operation.

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

What is the general rule when using the binary + operator with a string and another type?

A

The non-string value will be converted to a string.

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

How does the binary + interact with operands that are numbers, booleans, undefined, null?

A

They’re converted to their equivalent numbers (0 / 1) and added together.

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

How does the binary + interact with the operands when one is an object? What kind of coercion is this?

A

They’re both converted to strings and concatenated together using implicit type coercion.

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

What are the relation operators? What two types are they used on?

A

> , =, <=They are defined for numbers and strings. There is no strict-equality version of these operators.

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

How do the relational operators interact with their operands when they are different types?

A

If one is a number, the other operand will be converted to a number as well.If they are both strings, they are compared lexicographically (alphabetically).

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

Should you alwaysuse explicit type coercion?

A

Yes.

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

When should you not use strict equality operators? (=== and !==)?

A

Never.

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

Is it acceptable to use the unary + to convert strings into numbers?

A

Yes!

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

Should you avoid using String() and toString() in template literals ${...}?

A

Yes, don’t do that.

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

What is explicit type coercion?

A

Explicit type coercion occurs when a programmer intentionally uses one of the built-in functions and operators to coerce one type of value to another.

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

What does the Number() function do?

A

It attempts to convert the argument to a number. If it can’t convert the string to a number it returns the value NaN (which has the type of number).

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

How do you check the data type of a value?

A

use typeof, it doesn’t use any parentheses or camelCase which is weird.

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

What does this snippet return?

A

It returns 0. Number() returns 0 for both empty strings and strings containingonlywhitespace.

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

How does Number() interact with other types and objects?

A

Inconsistently, make sure you are accounting for the behavior in your program.

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

What do parseInt() and parseFloat() do? Do they work with multiple data types?

A

They convert strings to Integers / Floats. They exclusively work with strings.

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

How does parseInt() work?

A

It takes a string and attempts to parse an integer from it. The string must start with a numeric character for it to parse anything. If it receives a floating point (decimal) number it will return the integer to the left of the decimal.It can convert strings when there are non-numeric characters as well, as long as the string starts with a digit, optionally preceded by - or + :You can also pass parseInt a second argument called the radix. It specifies the base of the number contained in the string.10101 is 21 in binary, you’re setting the radix to 2 which means you’re using a base 2 number system (binary) to convert the string.

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

How does parseFloat() work?

A

It coerces a string to a number but it works with floating point numbers. It parses the numeric part of the string and stops once it hits a character that can’t be part of a number. It can return an integer.Like parseInt() the number in the string can optionally be preceded by a + or -.

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

How does the unary + operator work?

A

It works exactly the same as Number() but is more concise. Use Number() for clarity but it is acceptable to use the +.

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

How does toString() work? What data types does it not work on? What data type can you not call it directly on and why?

A

It returns a string representation of a value.You cannot use it on undefined or null.You cannot call it directly on a number type because the . that is required to call the method makes the interpreter think it is dealing with a floating point number.To work around this you can assign the number to a variable, surround the number with (), or use .. when calling the method.

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

When called on a boolean value, what does toString() return?

A

‘true’ / ‘false’

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

What happens when you call toString() on an array?

A

it returns a string version of the array with each element separated by commas.Any elements that are undefined or null within the array will be represented as empty values.

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

What value is returned when you call .toString() on an object?

A

‘[object Object]’

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

Does String() work the same as .toString()? How do they differ?

A

It works the same for the most part, you can however pass undefined/null to String() without causing the program to halt.

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

Why shouldn’t you use String() or toString() inside a template literal? {String(name)}

A

because the when you use a template literal it automatically returns a string so converting the string to a string is redundant.

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

When do you not use camelCase for names?

A

Only when you are naming a constant or a constructor function. Otherwise always use camelCase.

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

What is an idiomatic variable name?

A

A name that follows the commonly accepted conventions and standards.

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

What are non-idiomatic names for variables?

A

Names that, while syntactically correct, do not follow the conventions and standards typically followed by engineers. Examples:

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

In programming, what is a magic number and why should you avoid them?

A

It’s a number in your program that doesn’t have any information about why it’s there. It could be using a 5 for the amount of cards to deal or any number you use without context or info.You should use a constant whenever possible for these numbers.const CARDS_TO_DEAL = 5;

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

What is an example of using the expression assigned to a constant to be more explicit about what the number represents?

A

vs.

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

What do you use to indent code?

A

Always two spaces, never tabs.

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

When should you use semicolons?

A

Semicolon should terminate every line that is not a brace delimited block.

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

in if, for, and while statements why do you always put a space between the keyword and the open parenthesis that follows it?

A

To prevent confusion between built in statements and function calls.You should also always put a space between the closing parenthesis and opening brace.

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

When should you use one let to declare multiple variables?

A

Never, it is syntactically correct but the accepted standard for declaring variables is using one let per variable.

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

Can you ever mutate a constant? Should you?

A

Yes you can mutate a constant if you assign a pass-by-reference value to a constant.You shouldn’t ever mutate a constant though, that defeats the purpose of creating a constant.

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

What is the best practice for the length of a function?

A

Keep to around 10 lines, if it’s more than 15 lines or more, split it into multiple functions.

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

What is returned from a function if no return value is specified?

A

undefined

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

Is it best practice to log something significant (necessary for the program) and return a value from the same function?

A

No, the return value should be a separate function.

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

Should a function perform side effects and return a value?

A

No, the function should either have a side effect or return a value with no side effects but it should not do both.

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

What’s a major consideration you should make when naming a function?

A

whether the function has side effects or not, make sure the name gives insight into what the function is changing if it has a side effect.

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

why is returnTotal not a great function name?

A

If the function has no side effect, it must return something. We would just name it total as the fact that it is created to return a total is implied.

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

Which of these is a poor function name:

A

iterateThroughCards(), it is at a different level of abstraction than the other functions and deals with the actual nuts and bolts of the function (iterate).The name should just deal with what the function does in the context of the game, shuffle(), checkForWinner(), dealTwoCard() would all be fine.You shouldn’t include implementation details (programming jargon) in the function name.

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

why would we not use the above snippet with the snippet below?

A

We expect updateTotal() to mutate a value/values but not return anything. You need to split a function into multiple functions if it both mutates and returns a value.

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

What are convoluted functions with complex logic a sign of?

A

You don’t understand the problem well enough to break it down into well-compartmentalized pieces. As your understanding grows, your code should be easier to read and the intent should be easy to discern.

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

What’s wrong with this code?

A

it will throw an exception of ReferenceError: answer is not defined. We’re referencing answer before it exists.

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

What is the difference between a global variable and a local variable?

A

A global variable is available throughout a program whereas a local variable confined to a function.

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

What two things determine the scope of a variable?

A

The keyword you use to declare the variable and the location where you declare it.

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

What is a local variable?

A

Any variable declared inside a function or block is local. Everything else is global.

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

will the following code reassign greetingMessage?

A

Yes. You can reassign global variables from a local scope. If you had used let within the function it would have referenced the local greetingMessage and the global greetingMessage would have been unchanged.

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

Should your programs contain lots of global variables?

A

No, you should constrain the scope of your variables as much as possible. A smaller scope reduces the likelihood of of an outer scope misusing the varaible and makes bugs easier to track down.

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

What is a local variable? What determines its scope?

A

A variable declared within a function, they cannot be accessed outside the function they are declared in.A variables scope is determined by where it is declared.

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

Is greetingMessage a variable? What kind?

A

greetingMessage is a local variable initialized when an argument is passed into the function.Parameters have local scope within a function.

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

Do local variables persist through a programs life-cycle?

A

No, they go away when the function they are constrained to stops running. Every time a function is invoked a new scope is created, if the code within that scope declares a new variable, the variable belongs only to that scope. After the function finishes running the scope and any local variables is discarded.

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

Scope-wise, what is the difference between JavaScript in the browser to JavaScript in Node.js?

A

In Node.js a global variable is only available in the file/module you declare it in. To make global variables available across files/modules, you’ll have to explicitly import them in modules.

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

What is a global variable?

A

A variable that is available anywhere in a program, either globally or inside a function or block.

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

What are the two forms of local scope?

A

function scope and block scope

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

What is function scope?

A

Functions define a new scope for local variables. Think of a scope declared by a function as an inner scope. You can nest functions to create nested scopes. The scope of a variable is determined by where it is declared.

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

True / False : Outer scope variables can be accessed by the inner scope?

A

True! Outer scope variables can be accessed AND mutated/reassigned by inner scope variables.

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

True / False: Inner scope variable can be accessed by outer scope variables?

A

False, the outer scope has no access to inner scope variables.

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

Does a local variable exist before the function it is local to is invoked?

A

No, unless the function is called/invoked, the local variable will never be created. However, the scope is defined when the function declaration is created regardless of whether the function is called or not.

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

What level of scoping do functions at the same level of scoping have? Can they conflict with each other? What will calling funcB result in?Ex:

A

They are said to have peer scopes, they do not conflict with each other, funcB has no access to a so it will return a ReferenceError: a is not defined.

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

Can function reference variables created within other functions?

A

Only if they are nested within the function in which the variable is declared.

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

Do nested functions create their own variable scope?

A

Yes, they create another scope within the function. They can access variables outside of their scope as well.

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

What is this an example of (look at the variable names)

A

Variable shadowing. When a variable in an inner scope shares a name with a variable in an outer scope, it prevents access to the variable in the outer scope. It isn’t limited to callback functions.

112
Q

What is this an example of and how will it affect the a in the outer scope?

A

This is variable shadowing. It won’t have any affect on the a in the outer scope because within the scope of doit the outer a doesn’t exist. The a that is an argument creates a new local variable within the doit scope that is totally independent of the outer a.

113
Q

What is variable shadowing?

A

Declaring variables in an outer scope and an inner scope with the exact same name. It is always a bad practice and prevents any access to the outer-scoped variable from the inner scope.

114
Q

What is a code block? Is it the same as a function body?

A

A code block is code grouped by {}, they are often created by if/else, while, and for constructs. They are different from a function body, but the scoping rules are identical to functions.

115
Q

Every value in JavaScript is either a —- or an —–.

A

Primitive or an Object

116
Q

What are the two categories of data types?

A

Primitives and Objects.

117
Q

What are the primitive values in JavaScript?

A

string, number, boolean, undefined, null

118
Q

What are four of the most common types of objects?

A

Simple ObjectsArraysFunctionsDate ObjectTheir are other kinds of objects but these are the most common.

119
Q

What is the main difference between arrays and objects?

A

You use integer indexes to access array values instead of keys.

120
Q

What are objects composed of?

A

Primitive values and other objects.

121
Q

Can you change parts of an object while keeping the object the same object?

A

Yes, you can modify objects while maintaining their identity, they’re mutable.

122
Q

Are primitive values immutable? Or can they be changed like objects (mutable)?

A

Primitives are immutable. They are indivisible, atomic. If a variable contains a primitive value, all you can do that value is use it or reassign it giving it an entirely new value.Every operation on a primitive results in a new value.

123
Q

What value does number reference now? What is this an example of?

A

still 20, the immutability of primitives.

124
Q

what value does object.c return now? what is this an example of?

A

obj.c is now 4, this shows the mutable nature of objects.

125
Q

If two variables reference the same object, will a change to the object using one variable be reflected when using the other variable to reference it?

A

Yes, since objects are pass by reference, any change to the object will be reflected in any reference to it since they are all referencing the exact same object in memory.

126
Q

What does “pass by value” mean?

A

When you specify avariableas an argument for a function call. The function can’t do anything that sets the original variable to a different value.

127
Q

When can you treat JavaScript as pass by value?What does this mean?

A

When you are passing a primitive to a function.This means that no operation performed on a primitive can permanently alter the value. No matter what the function does with that value, you won’t be able to see the effects by inspecting the variable that you passed to the function.

128
Q

When you pass an object into a function does JavaScript display pass by value, or pass by reference behavior?

A

It depends on what you do with the object in the function. If you call a destructive function on it then the object is mutated.

129
Q

Is reassignment a destructive (mutative) operation?

A

No. But you can mutate the object on the expression side by using a destructive function.The above mutates whatever array you pass in because array.push is desructive.

130
Q

Where do variables that have primitive values store their value?

A

In the variable itself. When you assign a new value to a variable, JavaScript changes the variable’s content to the new value. The value has no reference to another variable/value.

131
Q

What value is d now pointing to? Why?

A

d is still pointing to the array [1,2]. reassignment is non-mutative, it just changes the value c is referencing.if you had used a destructive method on c while it referenced [1,2] it would be reflected in d, but since you reassign what c is referencing it just changes what it’s pointing to.

132
Q

JavaScript stores primitive values in ——– but it uses —– for non-primitive values like arrays and objects.

A

variables, pointers/references

133
Q

What is aliasing?If you assign an alias using a reference to another alias:Does it have any effect on what the 2nd alias points to? Is newerArr still referencing [1,2]?

A

When two different variables point to the same value they are said to be aliases for the same value.No, it just reassigns the value newArr is pointing to and has no effect on newerArr.

134
Q

What is the difference between declaration, initialization, and assignment?

A

Declaring a variable creates the variable but assigns no value to it.Initialization can only occur during declaration of a variable, it is the act of assigning a value to the variable at the time of declaration.Assignment is simply storing of a value in variable, it can occur at any time after the variable has been declared.

135
Q

True / False: functions should have either a side effect, or return a value, but should not do both?

A

True, you should avoid having functions that have a side effect and return a value.

136
Q

What’s wrong with this code? Will it work as expected?

A

You are mutating the array as you iterate through it. Don’t do this. It causes unexpected behavior. In this case, since for each iterates through the array using the index and you are changing the length of the array with every callback, it doesn’t properly empty the array.

137
Q

When would you use an underscore for a callback paramater?

A

When you are not using the parameter in the body of the callback.

138
Q

Are these values truthy or falsy?[]{}[undefined]

A

truthytruthytruthy

139
Q

Do Number() and parseInt() work identically? Consider the results passing them these strings:’12 oz’'’3.22

A

No they’re quite different.1. Number would return NaN and parseInt would return 12.2. Number would return 0 and parseInt would return NaN3. Number would return 3.22 and parseInt would return 3.

140
Q

Objects are passed into functions as ——-.Primitives are passed into functions as ——.

A

References/Pointers (mutable)Values (atomic/immutable)

141
Q

What variables would be available in the global scope after the last line runs, but before the program ends? What happens to qux?

A

foo, bar, yam would all be in the global scope before the program exits. bar returns qux but it is discarded after the last curly bracket since it’s not assigned to anything in the global scope to keep is persistent.

142
Q

What determines variable shadowing?

A

If a declaration in an inner scope whether through prevents access to a variable in the outer scope because they have the same name, that is shadowing. It can be created explicitly in the function with a let statement or by giving the parameter the same name as an outer variable in a function declaration.

143
Q

Does foo in the bar function shadow the global foo?

A

Yes, but only because inner foo isn’t initialized until after the global foo is initialized. If bar was invoked before global foo was declared, it wouldn’t be shadowing because it didn’t exist when bar runs, everything is discarded after the function runs so it wouldn’t be shadowing because there was no inner foo when the outer foo was initialized.

144
Q

does the foo() run before the console.log?

A

It has to, the console.log is printing whatever the result of invoking foo is so it must run after foo finishes.

145
Q

What happens if you declare two functions in the same scope with the same name?

A

The first function will get overwritten by the second function.

146
Q

What is a function declaration?

A

It is a statement that MUSTbegin with the function keyword. It cannot be preceded by any other characters ( (, return, nothing).A function declaration can be nested within a function expression.

147
Q

What is a function side effect?

A

Anything that occurs within a function that modifies something not directly under the control of the function.These include modifying non-local variables, mutating an argument, and writing output to the console.A parameter reassignment would NOT be a side effect because it is contained within the function.

148
Q

How are the empty slots treated?

A

It will return undefined, but the index holds no value, not even undefined. They are just empty slots.You can explicitly set a value to undefined in an array and then it will not be an empty slot. If you map through an array, any empty slots are skipped over but any explicity undefined slots will be treated as an index containing an element.

149
Q

What is the Built in method that tells you if a string ends with a certain string?

A

string.endsWith(string);

150
Q

what’s the built-in method you can call on an object to see if it contains a specific key?

A

Object.prototype.hasOwnProperty(key);

151
Q

Object.assign()

A

Object.assign(target, …sources)adds the keys and values from one or more source objects to a target object.returns the target objectmutates the target object

152
Q

String.prototype.includes()

A

someString.includes(searchString [, position])searches for searchString in the String.returns true or falseoptional position arg allows you to specify the index to start the search.

153
Q

Array.prototype.push()

A

someArray.push(element1 [, …[, elementN]])1. Adds one or more elements to the end of an array.2. Returns the length of the array after the new elements are pushed to it.3. Mutates someArray

154
Q

Describe Array/String.prototype.slice(startIndex, [endIndex]);What happens if you pass no args?How does it handle negative arguments?What is a main difference?

A

Returns a copy of the specified portion of the string.element atstartIndex is included, element atendIndex is excluded.non-destructivecalling slice with no args returns a shallow copy of the array/stringthey are treated as string length + indexlength is 9, so it’s the same as calling slice on the string and passing in 5 and 2.String.slice() returns a string and Array.slice() returns an array.

155
Q

String.prototype.indexOf()

A

someString.indexOf(searchValue [, fromIndex])If a string is present in someString, return the index it starts at. If not return -1, compares the values using ===, cannot search backward through an array,returns starting index or -1 if not presentnon-destructive

156
Q

String.prototype.replace()

A

someString.replace(regexp | substr, newSubstr | function)Returns a new string with some or all matches of a pattern replaced by a replacement.non-destructivecan take a RegExp object or literalmatches replaced by newSubstr or the return value of a function

157
Q

What are three ways to empty an array?

A

Set it’s length to 0 using the length property: arr.length = 0;Use splice: arr.splice(0,3);Loop through it and using arr.pop():

158
Q

How does arr.splice() work? Can you call .splice() on a string?

A

arr.splice() changes the contents of an array by removing or replacing existing elements.start is required and it is the element to start the splice and is inclusive, deleteCount is how many elements to remove including the startelement. item1etc… are the items to insert.arr.splice() returns the deleted items and mutates the caller.You cannot call splice on a string. It doesn’t exist as a string method and it mutates the caller, a string is a primitive which cannot be mutated.

159
Q

What will this output:

A

You cannot combine arrays using the binary + in JavaScript. JavaScript will convert the arrays to strings and then concatenate them.

160
Q

Will a change to arr2 be reflected in arr1? Why?

A

Yes, obj2 returns a ‘shallow copy’ of obj1 which means it just an alias for the object. . obj2 and obj1 are references to the same object. Anything that mutates arr2 is reflected in arr1 and vice versa.

161
Q

How does String.repeat() work?

A

it repeats a string the number of times you pass in as an argument.

162
Q

How does String.split() work? What happens if you don’t pass any arguments? What does it return?

A

It turns a string into an array split on the argument you pass in, if you don’t pass any argument it just inserts the string into an array, if you want to split the string into letters you need to pass an empty string as the arg. It returns an array.

163
Q

What is the difference betweenand

A

.push() mutates the caller, .concat() does not.

164
Q

Why does this happen in JavaScript?

A

JavaScript uses floating point numbers for all numeric operations which can lack numeric precision.

165
Q

What does this output:If this doesn’t return true, how do you test if a value is NaN?

A

The print statement will return false, NaN does not equal NaN. You have to use Number.isNaN(nanArray[0]) to test it.

166
Q

What does munsterVals contain?Would modifying the values in the munsterVals array mutate the munsters object?

A

An array of 5 objects, Object.values() creates an array of the values within an object (not the keys).Yes, they are aliases for the objects within the munsters object so any mutation done to them would be reflected in munsters. They are “shallow copies” of the objects which is really just a reference to the original object.

167
Q

What is returned from bar(foo())? What are the param = “no” doing in this funciton?The param = “no” sets a default value for param is nothing is passed in. we’re passing in “yes” to bar so it’s not used and foo returns “yes” no matter what so param is unused.

A

The call to bar will always return no when foo is passed in since it return yes regardless of what is passed in. Bar then checks if “yes” === “no” and it never does so the ternary operation returns “no”.

168
Q

Will the print statements print the same thing? Why not?

A

You placed the return keyword on it’s own line in second() so it will return undefined. first will behave as expected.

169
Q

What is a shallow copy? Are changes to a shallow copy reflected in the object they were copied from?

A

A shallow copy is a copy of an object that is NOT a reference to that object.If the object contains references to pass by reference values such as objects, those references will be maintained and any mutation performed on a pass by reference value will be reflected in both the original and the shallow copy.Primitives are of course immutable so new primitive values are copied to the shallow copy. Mutating the copy will not mutate the original, but if you modify a pass by reference value, it will be reflected anywhere that object is referenced.

170
Q

What will be logged? Why?

A

name is still [‘Kyle’], you can’t reassign name in the outer scope due to variable shadowing. a name variable will be available in the inner scope but you cannot reassign it to modify what name in the outer scope references.

171
Q

What are the values or one, two, three now? Why?

A

They’re unchanged. Variable shadowing prevents the inner one, two, three from modifying the outer one, two, three. When you reassign a shadowed variable from an inner scope, it just modifies the local version of that variable and does nothing to the outer variable.

172
Q

What are the values of one, two, and three now? Why?

A

“two”, “three”, “one”When you pass an object as an argument the binding between the object and the outer and inner copy of the variable persist since they are referencing the same object. splice() mutates the caller, so it is reflected in the object passed in and the variable that references it.You can’t break the binding of the outer reference to the variable with an inner reference because of variable shadowing though. A local version of each argument is created and assigned the reference when you call the variable, but when you reassign what the variable references, the binding is broken and the outer and inner variables are no longer aliases for the same object.

173
Q

Primitives make —— when passed to a function whereas Objects make ———- to the object when passed to a function.

A

copies, references

174
Q

—— are containers for multiple values which can be other ——- or ———-.———- are not containers for multiple values.

A

Objects, objects, primitives, primitives

175
Q

What does “variables as pointers/references refer to”?

A

Variables are pointers/refer to either a value (primitive), or an address in memory (object). This is related to pass by value / pass by reference since what the variable is pointing to / referencing will be a primitive or object which determines if it is treated as pass by reference or pass by value.

176
Q

variables that point to ——— point to the value itself on the —–.variable that point to ——- point to an address in memory on the —-.

A

primitives, stackobjects, heap

177
Q
  1. How can you create a shallow copy of an object?2. What will happen if you try to modify an object within the shallow copy?3. Do objects within a shallow copy maintain the binding to their aliases?
A
  1. copiedObj will maintain it’s reference to any nested objects so mutating those objects will be reflected in both objectCopy and copiedObj.3. Yes.
178
Q

Describe String.prototype.substring(startIndex, [endIndex]), how is it different from .slice, which is preferred?

A

It returns a copy of a portion of a string, the startIndexis included, the endIndexis excluded.It behaves differently than slice in that if the start index is greater than the end index, it swaps the two whereas slice returns an empty string. substring also treats negative arguments as 0 while slice treats them as length - index.slice is preferred, its behavior is more predictable.

179
Q

What are the three most common collection in JavaScript?

A

Arrays, Objects, and Strings.

180
Q

will the mutation on line 3 be reflected in arr?

A

No, it’s a shallow copy. You can add and remove elements without mutating the array referenced by the other variable. Any objects (arrays/objects) in the original array will be mutated by references to the copy however.

181
Q

Arrays are lists of ——– ordered by —–. Arrays use ——- -based index to maintain the order of its elements.

A

elements, index, integer.

182
Q

Objects are common collection data structures that use -** pairs instead of **- indexes.What is another name for an Object key?

A

key-value, integer-basedproperty

183
Q

You want a collection of just the keys, or just the values from an array. What methods would you use?

A

Object.keys(objectName) or Object.values(objectName) both return an array of the keys/values contained within an object.

184
Q

What is the return value if you try to access an out-of-bounds indice on either a string or an array. What happens if you try to access an indice less than 0?

A

They both return undefined.

185
Q

What is the return value when you try to access a key that doesn’t exist on an object?How can you tell if the key doesn’t exist, or it has been assigned the undefined value?What is an alternative to using this method?

A

undefined will be returned.You can use Obj.hasOwnProperty(propName) to determine whether the property exists on the object. It returns a boolean.You can use Object.keys(objectName).includes(keyToSearchFor)

186
Q

Are JavaScript arrays object? What is the chief difference between an array and another other object? Can you add properties to arrays like with a regular object?

A

Yes, they’re objects. The chief difference is that an array uses unsigned integers as its primary keys. You can add properties just like a regular object, any non-unsigned-integer will be treated as a new property on the object and can be accessed as such.

187
Q

What will the first log print?What will the second log print?What will the third log print?

A

It will just show [‘kyle’, ‘apey’];It will print 2, non-integer indexes do not affect the length property of an array.It will print the array [‘0’, ‘1’, ‘-1’, ‘theThing’], these are all properties on the array and an array is still an object.

188
Q

What are non-positive and 0 integer keys on an array object referred to as?How do methods that iterate through an array treat these properties?

A
  1. non-element properties2. they ignore them.
189
Q

What does this log? Is there a different method to detect an array?

A

This will log object since an array is an object.You can use Array.isArray() to detect if an object is an array.

190
Q

What does Array.prototype.join() do? What happens if you pass in no args? What happens if you pass in one arg? What happens if you pass in an empty string?

A

It turn an array into a string. If you pass in no args it return the array in the form of a string with each element separated by a comma. Whatever you pass in as an argument will be placed between each element in the string, if you pass an empty string it will join all the elements into a single string with no spaces between them.

191
Q

What is numbers[0] now? What happens if you add 1 to an out of bounds indice?

A

numbers[0] is now 2, when you reassign the value an element holds this way it is destructive.you get NaN, undefined + 1 returns NaN.

192
Q

What are the two types of notation you can use to access/modify objects?When must you use bracket notation?

A

dot notation and bracket notation.You must use bracket notation to access any property that has a space in it’s name, or if you’re using a variable to access a property.

193
Q

How does this snippet illustrate one of the major differences between strings and the other collection types? How would you create this new string?

A

It shows that since strings are immutable, you cannot modify them by accessing a letter within them. Strings are indivisible since they are a primitive value. You cannot modify a string by accessing one of the letters using an index.To create the string the easiest way would be to use .slice().or

194
Q

Is a JavaScript string actually a collection?

A

No, you cannot store data in them. They are array-like in that you can use an integer based index to access characters within them and also loop through them using using the length property and a for or while loop.

195
Q

What methods that operate on strings mutate the string?

A

None of them. Strings are immutable so any string method that returns something creates a new string.

196
Q

How does String.concat() work?

A

It is the exact same as using +, you can pass multiple args to .concat and build a string. None of the string will be mutated, strings are immutable.

197
Q

How does String.includes() work?

A

It takes a string as an argument and returns a boolean based on whether or not the String contains the argument. You can pass an optional second argument that specifies which index to start searching at.

198
Q

String.trim()String.trimStar()String.trimEnd()

A

removes any whitespace from the leading or trailing end of a string. useful when getting input from users. also removes whitespace characters like \n and \t.trimStart() and trimEnd() behave exactly the same except they work on either the beginning or end of the string.

199
Q

toUpperCase()toLowerCase()

A

uppercase or lowercase the string passed in as an argument. often combines with slice and a concatenation to capitalize words.

200
Q

String.prototype.charAt(index)

A

Nearly identical to using a bracket to access a character on a string. String[5].The main difference between the two is if it is an out of bounds index you are using to access a character, charAt() will return an empty string “” and bracket notation will return undefined.

201
Q

String.prototype.charCodeAt(index)String.fromCharCode(charCode)

A

charCodeAt() returns the Unicode code point or character code of the character at the index. This is the number that represents a given character at a machine level.fromCharCode() is the opposite, it takes a character code and returns the character represented by the code.note that String.fromCharCode() is a static method /function whereas String.prototype.charCodeAt() is a prototype method. You call the former directly on the String constructor. It makes sense as you don’t call it on a string, you pass in an integer and it returns the character.

202
Q

String.prototype.endsWith(searchString[, length])String.prototype.startsWith(searchString[, length])

A

checks if a string begins or ends with searchString. length is optional and defaults to the actual string.length value. Returns a boolean.

203
Q

What is line 4 called? What is it’s purpose?

A

line 4 is a guard clause. It protects the body of the loop from dealing with values it doesn’t need to handle. In this case, it skips the odd numbers and continues on with the next iteration of the loop.

204
Q

Are you required to use a for/while loop to iterate over an object? What is another option?Show one way to iterate through the properties of an object using a for/while loop.

A

No, you can use a for in loop to loop through an object. The first arg will allow you to access each property and the second arg is the object to iterate over.You can use Object.keys to access an array of an objects keys and then iterate over it as usual with a while loop.

205
Q

what is the difference between a break statement and a continue statement?

A

break exits the loop completely, continue just stops that iteration of the loop and moves to the next one.

206
Q

What does PEDAC stand for, what is it’s primary goal?

A

P - [Understand the] ProblemE - Examples / Test casesD - Data Structure A -AlgorithmC - CodeThe primary goal of utilizing the PEDAC process is to identify the pitfalls that occur when you don’t code with intent.

207
Q

True of False, where most students run in to problems while problem-solving is not knowing the appropriate syntax to solve the problem?

A

FALSE ASSHOLEmost people run into problems is understanding the problemand determining the appropriate algorithm.

208
Q

What are 3 sub-step for the P in the PEDAC process? What does the P stand for? What do you do next?

A

Understand the Problem.The 3 steps are:Slowly read the problem descriptionCheck the test cases, if anyClarify any part of the problem that is unclear with the interviewer.Once you understand the problem and have loaded it into your brain, You need to write down what the inputs and outputs for the problem are, the explicit requirements you must meet, and the any implicit requirements you discover.

209
Q

What’s the easiest way to reverse a string? Why is the join method necessary?

A

The .join() call is necessary because str.split() returns an array, and we need a string for the output.

210
Q

When iterating through a collection, whether you are transforming or selecting, what is one thing you should ALWAYS pay attention to?

A

Whether you are mutating the collection or returning a new collection.

211
Q

What specific type of object fo forEach, filter, and map methods work on?

A

They only work on arrays! Even though objects are collections, and strings are collection-like object, these three methods are only available on an array.

212
Q

Though you can’t use array specific methods on objects (forEach, filter, map), what methods can you call on an object that would allow you to work with it’s values as arrays?

A

You could use Object.entries(), Object.values(), or Object.keys()

213
Q

Can you call forEach, filter, map on a string? Why or why not? How can you work around this? Are the methdos you would use to accomplish this mutative?

A

No, a string is ‘collection-like’ and you can’t use those array specific methods on them. You could convert the string into an array of characters using String.prototype.splice(“”) or an array of words using String.prototype.splice(“ “).These methods return an array, now you can use the array methods on them. If you needed to return a string you could then use Array.prototype.join(“”) to return the string as a single word.Neither splice(), nor join() is mutative.

214
Q

A ——– ———- passed to a method/function as an argument is called a ——–.

A

function expression, callback

215
Q

Which is a function declaration which is a function expression? What’s the difference between the two?

A

The first snippet contains a function declaration. The function declaration does not assign the function to a variable and has a name (it’s not an anonymous function). It can be invoked from ANYWHERE in the program regardless of on what line it is declared as long as it is in the proper scope.The second is a function expression, the function expression assign an anonymous function to a variable. This is an unnamed function that is invoked with the variable name you assign it to. You can only call a function created as a function expression after the line that assigns it to the variable.Due to variable hoisting, the variable that will point to the function is available to reference but it holds the value undefined until the line where the function is assigned to it.

216
Q

What is this an example of? What will the values be for the variables?

A

This is array-destructuring assignment. It is just a shorthand way of assigning the elements in an array to the variable names in the brackets. key points to the value at keyValue[0], value points to the value at keyValue[1] etc…It’s the same as:

217
Q

What do you use forEach for? What is it not used for? What does it return?

A

forEach is essentially a shorthand for or while loop, it iterates over every element in an array and performs whatever action on each element.Its sole purpose is iteration, you cannot use it if you need to return a value because it always return undefined regardless of what you try to return from it.

218
Q

What does Array.prototype.filter() do? How does it decide which elements to select?

A

filter selectsvalues that returns a truthy value when passed as an argument to the callback you specify and ignores any value that returns a falsy value.filter ALWAYS returns a new array even if it is identical the original array.the callback you pass to filter can accept and use 1,2 or 3 arguments for every element in the array. They are the element, index, and the array itself.filter() iterates through an array and returns a new array of elements based on the truthiness of the return value of the callback function you pass in.this snippet tests if a number is odd or even, if a truthy value (not true/false) is returned then the element will be added to the array returned when filter finishes running.This snippet uses an implicit return! Remember to return a value when using filter or it will return an empty array since every returned value will be undefined, which is falsy.

219
Q

What is the difference between truthy and falsy and true and false? What are the falsy values?

A

true and false are the two values that make up JavaScripts boolean type. Truthy and falsy values don’t belong to a specific type, they are just a classification that JavaScript recognizes as representing truth or falsity.false, ‘’’, 0, null, undefined, NaN

220
Q

What does Array.prototype.map() do? What does it return? What is the difference between filter and map?

A

The purpose of map is to create a new array of transformed values based on the callback you pass in to it. Map is used to perform a transformation on an element and insert it into a new array. The difference between map and filter is that filter is used only for selectionwhereas mapis used for transformation.map iterates through the array, performs an operation on each element, and then adds the return value of the callback function you pass in to the new array. The truthiness of the value doesn’t matter to map, just the return value of the function.

221
Q

What will the array returned by map contain? What about for this example?

A

[true, false, true] since that’s the return value of the callback.[undefined, undefined, undefined] you didn’t return a value, functions always return something, and that’s what gets added to the array. undefined in this case.

222
Q

Array.prototype.forEach() is used for ——— it returns ———Array.prototype.filter() is used for ——— it returns - — —–Array.prototype.map() is used for ————-it returns - — —–

A

iteration, undefinedselection, a new arraytransformation, a new array

223
Q

Describe Array.prototype.some()

A

.some returns a boolean based on whether or not any of the elements passed into the callback return atruthyvalue.

224
Q

Describe Array.prototype.every()

A

Returns true if the callback passed in to the method returns a truthy value for every element in the array. otherwise returns falsy.

225
Q

Describe Array.prototype.find()

A

.find() returns the first element that returns a truthy value when passed in to the callback function provided to it. If none of the callbacks returns a truthy value then undefined is returned.

226
Q

Describe Array.prototype.findIndex()

A

findIndex() is similar to find() except that it returns the index of the first element that returns a truthy value when passed in to the callback that you provide. It also return -1 if none of the callbacks return a truthy value instead of undefined.

227
Q

Describe Array.prototype.reverse()

A

reverses an array in place (mutates the array)use .slice() to avoid this behavior and leave the original array intact.

228
Q

Describe Array.prototype.includes()

A

includes returns a boolean based on whether or not the argument you pass in to it is present in the array. There is also a String.prototype.includes()that works very similarly.

229
Q

Why does the includes return false, the objects have identical properties/values.

A

includes uses === to compare objects and those are two different objects with the same contents.This would work since you are using a reference to the same object and obj === obj.

230
Q

What happens when you add a non-unsigned integer as an index to an array?

A

An array is an object, any non-unsigned integers you add will be properties on the object. They are not elements of the array the object references. They do not count toward the length property and any array methods will ignore them.

231
Q

Is an array with a .length value of 0 always empty?

A

No, any non-elements attached to the object as properties will be ignored by the .length property. These elements are not elements in the array but they are attached to the object so you need to decide what constitutes an empty array.

232
Q

What is a sparse array? How is this reflected in the length?

A

A sparse array has fewer elements in it than the length property returns. If you access one of these properties undefined will be returned, but the value has not actually been assigned as undefined and actually points to no value at all.

233
Q

When does JavaScript implicitly return a value from a function?

A

Only when it is an arrow function without braces surrounding the body of the function.returns

234
Q

What does this return? Why?

A
  1. .pop() returns ‘caterpillar’ and you’re calling .length on that string.
235
Q

What does this return?

A

[undefined, bear] since the callback doesn’t return anything if the length of the element is less than 4, when ant is passed in to the callback undefined is returned. Functions always return something, if no return value is specified then they implicitly return undefined.

236
Q

How does this snipper work? What is the 0

A

It converts the values present in the ages object to an array, and then calls reduce on that array. reduce accumulates the sum of all the values in the array in the ageSum arg. The 0 is what the initial value of ageSum is, currAge is the value of each element as it is passed into the callback.

237
Q

What does this code do? What does the … do?

A

it converts the values of an object into an array, we then use the spread operator (…) to convert the array into individual arguments to the Math.min() method. The method doesn’t accept arrays as an argument so the spread operator converts the array to a list of individual arguments.

238
Q

What do lines 5 and 6 do?

A

Line 5 uses short circuit operation to make an assignment. If the property has already been created on the result object, then the || will return the left side expression, if the property doesn’t exist yet then it creates it and assigns it the value of 0. If the value is already greater than 0 then it will be assigned the value it already holds and will increment it on the next line.Line 6 then increments the value.

239
Q

What will get logged?

A

undefined. You cannot access a character with a negative integer in a string or an array.

240
Q

What will the slice return? How does slice handle negative numbers?

A

It will return the substring ‘po’. If you pass negative numbers to slice, it will calculate the indexes passed in as (str.length - index) so this would be the same slice as passing in 1,3. You cannot pass in a negative and positive integer, they both have to be either negative or positive.

241
Q

What will get returned by this snippet?

A

undefined. JavaScript is very unforgiving when you try to retrieve indices that don’t exist. You’ll never get an error trying to access an index that doesn’t exist, just undefined.

242
Q

Which of these are valid for retrieving a value from an object?

A

Only the second one. if you had an object with a key of “This is” it would be retrieved. The brackets are required.The first snippet is invalid, you can’t pass a string for the key using dot notation. You must use bracket notation if you need to access a key with a space.The third is trying to access a variable named this is which is totally invalid.

243
Q

Will both of these return the same thing?

A

Yes, they’ll both return a an array of the words in the string split at the spaces. .split takes a string OR regex and / / is regex for a single space.

244
Q

are binary + and array.prototype.concat() the same? Does JavaScript perform implicit coercions on arrays?

A

Yes, if you try to combine arrays with the binary + it coerces both of them into strings. Yes, JavaScript attempts to coerce arrays when using +.

245
Q

What does [] === [] return?Is there any situation when an array will === an array?

A

it returns false, the only time an array === an array is when you are comparing aliases for the same object.

246
Q

Is an arrays length property always higher than the highest index used in an array? Can it be a negative number?

A

Yes. The highest index used will always be one less than the length.No, it is always a non-negative integer.

247
Q

Will this always be true? How can you call Object.keys() on an array?

A

No, if you have an array with empty spaces the Object.keys() method will not put them in the array. The Object.keys(arr) will have a length of 2 because the indexes that have empty spaces are not considered a key. arr.length will be 5, you set it to 4 then pushed one more element which means the length is 5.

248
Q

How long will the array be after the element is pushed onto it?

A

myArr.length will return 10.

249
Q

Will a forEach() called on an array containing seven elements ALWAYS run exactly seven times? How can you break out of forEach execution?

A

It won’t run seven times if an error stops execution, or the array is mutated during the execution of the forEach(). You should never mutate an array while iterating through it but it can happen.You cannot break out of a forEach() using break, return, or a null return value. Only an error can stop it from executing before it passes every element in the array it’s called on into it’s callback function.

250
Q

Why is sorting typically performed on arrays specifically?

A

Because sorting affects the order of the collection, and since arrays are accessed via their index, order in the array is important.Plain objects are accessed via their key, their is no set order of the keys and so if the order of the data you are working with is important, it shouldn’t be stored in a plain object.

251
Q

Do strings have built-in sorting methods? Does this prevent you entirely from using sort with strings?

A

No, their are no built in methods to sort a string but you could use array.prototype.split(“”) to return an array of letters and then use .sort() on the array.

252
Q

Why is there no point in sorting JavaScript objects?

A

They don’t maintain a set order for their key-value pairs. Most implementations order the KVPs of an object based on the sequence they were added but there is no guarantee they will be in that order. Object values are accessed via keys, there is no need to sort them.

253
Q

What is the default behavior of Array.prototype.sort() when called on an array of numbers with no argument?

A

It converts the numbers to strings and sorts them according to their Unicode values. It performs this character by character just like sorting alphabetically would be accomplished:.sort() is destructive, it mutates the original array.

254
Q

What is the default behavior of sort without arguments? What is the exception?

A

It converts the values to their string equivalents and performs the comparison alphabetically. The exception is if the value is undefined. All instances of undefined get moved to the end of the array..sort is destructive and mutates the original array.

255
Q

What does this code snippet do?

A

It returns a shallow copy of the array and sorts it alphabetically.

256
Q

How do relational operators (, ===) work with strings?

A

The strings characters are converted to their code pointin UTF-16 encoding and the integers are then compared.

257
Q

What is the rough order for UTF-16 codepoints?

A

Digits and Punctuation > Uppercase letters > A few weird punctuation characters > Lowercase letters > few more weird punctuation chars

258
Q

When you pass a callback to sort, how does it determine what order to put the two arguments it is sorting?

A

If the CB returns a number < 0, place a before bIf the CB returns 0, do nothingIF the CB returns a number > 0 place b before a

259
Q

What will this snippet return? Why?

A

This will return a reference to the original array sorted an ascending order. The return value of the callback dictates how a and b are ordered are ordered within the array, if the callback returns a negative value, a will be placed before b and vice versa.

260
Q

What does this snippet do, why?:

A

This returns a reference to the original array sorted in descending order. If the callback returns a positive number, b is placed before a.

261
Q

What is happening in this snippet?

A

We are using .reduce to reduce the arrays to a single value then using those values in the callback passed to .sort to return the array with the subarrays in ascending order.

262
Q

What does values does arr[0] now contain? Why? What affect would reassigning b to a new array have on arr?

A

arr[0] is now [1,5]. a and arr[0] both point to the same object and a[1] mutated the array.reassigning b to a new value would have no affect on arr[1], b would now reference a totally different object and arr[1] would still point to the original object.

263
Q

What is happening on line 2? What method is this mimicking? What kind of copy does this create?

A

Line 2 uses the ES6 spread syntax to create a shallow copy of arr. This is functionally the same as using .slice() to copy an array.

264
Q

Both .slice and the ES6 —— syntax create a shallow copy of an —–. Only the — level array is copied. When the array contains other ——-, like a nested array, then those objects are ——, not copied.

A

spreadarraytopobjectsshared

265
Q

When you mutate a ** ** in an array or other collection, it is the ** ** you are affecting rather than the collection.

A

shared object

266
Q

How is obj1 affected by the operation on line 4? obj2?

A

obj1 now has the properties of obj2 within it since it was passed as the first argument. obj2 is unaffected since it was the second argument.

267
Q

after the two snippets run, what does obj.c now return?

A

It is undefined, copyOfObj is a shallow copy of obj so they point to two different objects.c : ‘baz’ is only present on copyOfObj.

268
Q

What does this snippet do?

A

It creates a deep copy of the array. This only works on plain objects and arrays.JSON.stringify() serializes any object, including arrays that only have primitives, arrays, and plain objects as elements. The object is converted into a string that can be converted back to an identical object.JSON.parse() converts the string back into an object.

269
Q

What does Object.freeze() do? Will it work on nested objects? How do you check if an object is frozen?

A

It prevents mutation of the object.It does not work on nested objects.You use the static method Object.isfrozen(objToCheck)

270
Q

In most computer languages, the term?????or?????is used to describe values that can be passed to functions as arguments or returned by those functions.In JavaScript, ????? themselves are ?????

A

first-class values, first-class objectfunctions, first-class values/objects

271
Q

What is imperative programming? What is declarative programming?

A

Imperative programming requires you to be very explicit and tell the interpreter exactly what to do each step of the way.A declarative approach has more abstraction and allows you to specify what you want done but without as much instruction on exactly how to accomplish it.

272
Q

Function that take other functions as arguments are called ?????.The functions we pass to these functions as arguments are called ?????.

A

Higher order functionsCallbacks

273
Q

What will each iteration of the for in loop log in this snippet? How would you use that to access each object?

A

Each iteration would log the key for each value (plain object) within the munsters object.You would need to use munsters[munster]to access the object stored by each key. Munster only holds the key value to access each key in a for in loop, not the object itself.

274
Q

What does this log?

A

True. The “11” will be coerced to the integer 11 and 11 is greater than 10. If any of the comparisons performed by .some return true then true will be returned from the method call.

275
Q

In JavaScript, a **** is a function that belongs to an object.

A

Method

276
Q

When using relational operators:When comparing a string type to a number type, the * will be converted to a **When comparing a boolean to a number, the *** will be converted to a **When comparing a boolean to null, *** will be converted to **When comparing a boolean to undefined, **** will be converted to *****

A

string, numberboolean, numbernull, 0undefined, NaN