Week 3 Flashcards

1
Q

what is a template literal

A

a new way to create a string literal that expands on the syntax of the String primitive type allowing for interpolated expressions to be inserted easily into strings.

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

what is the main advantage of using template literals?

A

the ability to interpolate variables or expressions into strings

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

what do we wrap the data that we want to interpolate in?

A

${}

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

what happens to the data wrapped in the dollar sign curly braces when the code is run?

A

the variables or expressions wrapped within the ${} will be evaluated and then replaced with the value of that variable or expression

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

what character do we use to create a template literal?

A

a backtick aka grave (`)

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

what can template literals evalute?

A

anything that can be stored in a variable including functions

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

what is a call stack?

A

a structure that JAvaScript uses in the JS runtime to keep track of the evaluation of function calls; it uses the stack data structure

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

what is a stack? how are we using this right now?

A

a general pattern of organizing a collection of items
currently the items being organized are the function calls that occur during the execution of our program

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

describe our current usage of a stack as a vertical pile

A
  1. pushing a new item to the stack - new items must be placed on top of the pile
  2. popping the top item from the stack - at any point, the only item that can be removed is the top of the pile
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does the term stack frames describe?

A

the items that are being pushed and popped; items placed on the call stack

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

describe the ways the JS leverages stack mechanics during runtime

A
  1. when a function is called, a new frame is pushed onto the stack
  2. when a function returns, the frame on the top of the stack is popped off the stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

when will a frame entirely leave the stack

A

when the function is popped due to a function return which can either be an explicit return with the return keyword or an implicit return after the last line of the function’s definition is executed

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

what does the function on top of the call stack represent?

A

the function being executed currently

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

when can the program on the call stack exit?

A

when the stack is empty

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

why is JS a single-threaded language?

A

the use of a single call stack leads to a single thread of execution; the JS runtime can only preform one command at a time and the one command currently being executed is what ever is at the top of the stack

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

what is the critical behavior to be aware of in the JS runtime?

A

an event can only be handled once the call stack is empty

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

what does it mean for a function to be recursive?

A

the function is called from within itself

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

what is the difference between a functtion that recurs and a function that is recursive

A

recur means for a function to be called more than once, while recursive means that the function is called from within itself

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

what are the two cases in a recurisve function and what does this term mena?

A

the two cases are the expected output for a particular input in a resursive function; the two cases are the base case and the recursive case

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

what is the base case in recursion?

A

when the data passed into our function is processed without any additional recursion; the base case is excuted, the function runs once and ends; the situation in which the function stops recursing

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

what is the base case also know as? and why

A

the terminating case because it results in the function stopping

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

what is the recursive case?

A

the situation where the function recurses, this represents the data state that causes the function to call itself; what causes our function to keep recursing

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

what is a default parameter?

A

I’m not sure yet

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

how is a default parameter declared?

A

it is declared in the function signature like a regular parameter, except it is given a default value using =

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

when can you start executing a recursive function’s stack frames

A

when you reach the base case

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

if there a limit on the number of times that a recursion is done? if yes, what is that limit

A

JS does have a call stack size limit which depends on how much memory is allocated to the JS program on your system

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

what is stack overflow

A

an error that is thrown from your code when you call stack reaches the call stack limit; the program is halts, the stack is wiped out entirely and you have no result

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

what is direct recursion?

A

functions directly calling themselves

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

what is indirect recursion

A

recursive loops across multiple functions

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

how is iterative code different from recursive code?

A

its less resource-intensive
requires less planning
easier to read and understnad

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

when should you choose recursion over iteration?

A

when the problem can clearly be subdivided into smaller problems by solving the smallest or simplest case and then working towards a full solution; if its a problem you can solve on paper for 1-5 things, but can’t imagine doing for 1000, use recursion

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

when should you use iteration over recursion

A

it is a problem that is no harder for 1000 things than 1-5, but will just take longer

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

what are the three steps that define a recursive function?

A

The function calls itself
The function has an end state (base case)
The function moves closer to the base case with each call (recursive step)

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

what is an iife?

A

an immediately-invoked function; it is an anonymous function that is defined and then invoked as soon as it is defined

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

what are the steps to writ an IIFE?

A
  1. wrap the anonympus function in the grouping operator ()
  2. invoke the function after the grouping operator with another set of parenthesis
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

how can we hang on to the result of an IIFE?

A

assigning the return value to a variable

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

do we have access to variables declared within an IIFE after the function has been called?

A

no

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

how does hoisting work, when a function is declared using function declaration syntax?

A

the name of the function and its contents are hoisted

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

explain how JS approaches hoisting when the function is declared with function declaration syntax

A

when JS runs this code, it takes the function declaration and the function itself in its memory to the top of the code we’re writing

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

can you calla afunction that is declared using function declaration syntax, before it is actually declared?

A

yes

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

how does hoisting work when a function is declared using function expression syntax and using the keywords const and let

A

hoisting doesn’t work here, we will get a reference error telling us that we cannot access the value of the variable before its initialization; the value of the variabel has not been assigned yet

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

how does hoisting work when a function is declared using function expression syntax and using the keyword var

A

hoisting doesn’t work here, the name of the variable is hoisted to the top of the scope, but it is assigned a value of undefined until the variable is assigned, we will get an error message because we are attempting to invoke undefined

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

how do function and variable hoisting work, when the variables are within in the same scope, have the same name and are declared with const or let

A

we get an error message telling us that the name has already been declared as we can only use a name once within the same scope

44
Q

explain the relationship between variable assignment, function declaration, and variable declaration for variables declared with var, the same names, and within the same scope?

A

variable assignment trumps function declaration, but if the variable is not assigned to a value then function declaration trumps variable declaration

45
Q

what is the default value of variables declared with let, const, and var?

A

let is undefined
var is undefined
const variables need to be assigned a value

46
Q

what are the two main differences between the primitive data type and the reference data type?

A

primitive data types are immutable;
primitive data types cannot have methods because they are not objects

47
Q

What is runtime?

A

the execution of a progrma

48
Q

what is the thread of execution?

A

the sequence of well-ordered commands

49
Q

what does is it mean for an execution to be single threaded?

A

only one command can be processed at a time

50
Q

what type of execution is JS? single or multi-threaded?

A

single

51
Q

what does it mean for an execution to be multi-threaded?

A

multiple commands can be processed at the same time

52
Q

what are some disadvantages to single-threaded execution?

A

takes longer to execute a program because it can only tackle one command at a time

53
Q

what are some advantages of a single threaded execution?

A

it’s easier and simpler for a computer to execute

54
Q

what are some advantages of multithreaded execution?

A

its faster to execute a program

55
Q

what are some disadvantages of multithreaded execution?

A

it’s more complex for the computer to execute
it could potentially slow down the time that it takes to execute a function because of limited computer resources
things can go wrong if coordination between the threads isn’t accurate or becomes complex

56
Q

do the individual threads on a multithreaded execution do more than one command at a time?

A

no

57
Q

what is the problem that all single-threaded languages face? and how do we fix this?

A

if a command is being executed and something comes along (like a user pressing a key) that we want to pay attention to immediately, we can’t, because we can’t run more than one command at a time; the only solution for this is for them to wait (an unresponsive program)

58
Q

what are components of the event loop?

A

the call stack
the message queue

59
Q

what does the message queue keep track of?

A

tasks that cannot be executed at this moment, but will be execute once the current task is finished

60
Q

what is js execution pattern referred to as an what does this mean?

A

its called run to completion because the execution of an ongoing task will never be interrupted by another task

61
Q

what structure does the message queue take?

A

a queue

62
Q

what pattern does the message queue follow

A

new items are added to the back of the queue (enqueueing an item)
items can only leave through the front of the queue (dequeuing an item)

63
Q

what are messages in the message queue? and what do they correspond to?

A

items that are stored on the queue; the correspond to events that have occurred but have not yet been processed

64
Q

where are synchronous tasks performed?

A

on the call stack

65
Q

where are asynchronous tasks stored while the current task is being processed on the stack?

A

on the queue

66
Q

in what order do items on the queue get proccesed?

A

first come first serve

67
Q

how do the call stack and the message queue interact?

A

while the current tasks is being performed on the stack, the asychronous tasks wait on the queue, when the current tasks is completed, the next task on the queue is moved to the staxk for execution and so on and so worth

68
Q

what has to happen for the message queue to be utilized

A

a task has to wait to be fulfilled

69
Q

what is synchronous code?

A

the code has an inherent order among the commands and this order execution is guaranteed

70
Q

what is synchronous code?

A

the code has no guaranteed total order in which the commands are executed

71
Q

is the time period in setTimeout exact? why or why not?

A

no, the time period specified in setTimeout is the minimum time that will elapse before executing the callback, it could take longer.

72
Q

what does the setTimeout method do?

A

sets a timer which executes a function or a specified piece of code once the timer expires

73
Q

what does setTimeOut accept?

A

a callback or just a piece of code and an amount of time in milliseconds

74
Q

what is a non-blocking function? provide an example

A

an asynchronous function that doesn’t prevent the code that follows their invocation from running

75
Q

is setting a time required for the setTimeout function

A

no; default time is zero

76
Q

what does the setInterval function do, what does it take in as parameters and how can we clear it?

A

executes a callback repeatedly at the specified delay;
takes in cb and time in ms
clearInterval

77
Q

what must you input to close the readline interface? and when would you do this?

A

rl.close()
after the question is answered

78
Q

what happens to our readline if we don’t close the interface?

A

it will hang and not exit

79
Q

is the question synchronous or asynchronous

A

asynchronous

80
Q

how do we get a command to come directly after a callback is invoked asyncronously?

A

pass that command inside of the callback

81
Q

what does callback chaining allow us to do?

A

perform a series of asynchronous functions one after the other,

82
Q

what is the recommended way to refactor callback chaining that requires a lot of nested structures

A

use named functions instead of passing in anonymous functions

83
Q

when should you use name functions over anonymous when callback chaining?

A

when creating a callback chain longer than two

84
Q

what is callback hell?

A

an overly nested callback chain

85
Q

where should you place debugger keywords in your code and what does this do?

A

you should place them every time the scope changes; this tells the debugger where to look

86
Q

what does the continue button on the debugger do?

A

will go from debugger to debugger statement

87
Q

what does the step into button on the debugger do?

A

go line by line into a function call, will step into the function regardless of a debugger statement or not; if there is a function call, we will step into that function

88
Q

what does the step out button do on the debugger?

A

if we have stepped into a function, this button will take us out of that function

89
Q

what does the step over button on the debugger do?

A

it will step over any function that does not have a debugger; if there is a function call and no debugger statement and we use the step over button, it will step over the function

90
Q

what are some use cases for recursion?

A

its an alternative to iteration
used for handling a large set of data
breaking problems into smaller problems, sub problems

91
Q

can you have more than one base case?

A

yes

92
Q

how does stack overflow occur in recursion?

A

when you don’t tell your function when to stop recursing

93
Q

what happens to the default parameter if that parameter is passed in a value during the function

A

it is overwritten

94
Q

how many times can you run an iife

A

one

95
Q

how can IIFes be useful?

A

if you don’t want to use up the global name space
protect function names variables
if you want to execute async code
only want to run a function one time

96
Q

explain for anonymous functions interact with the function expression syntax?

A

as soon as you give a variable a name it is no longer anonymous

97
Q

write an IIFE that takes in no parameters, uses the fat arrow syntax and doesn’t use the function keyword

A

(() => console.log(‘message here’))();

98
Q

write an IIFE that takes in a parameters, uses the fat arrow syntax and doesn’t use the function keyword

A

((name) => console.log(hello ${name}))(‘brandon’);

99
Q

which type of code blocks the call stack? synchronous or asynchronous

A

synchronous

100
Q

why is asynchronous code useful?

A

it allows the program to wait for user input
bad connection
handle multiple operations that run independently and do not block or wait for each other to finish
users are unpredictable
allows for a more dynamic website
the environment that we run our application in is full of uncertainty

101
Q

what does the event loop do?

A

check to see if the call stack is empty and if it is, we can move a task from the messaging queue to the call stack

102
Q

is the callstack FIFO or LIFO?

A

LIFO

103
Q

is the messaging queue FIFO or LIFO

A

FIFO

104
Q

does the call stack handle asynchronous or synchronous code

A

synchronous

105
Q

does the messaging queue handle synchronous or asynchronous code

A

asynchronous

106
Q

can recursion be performed with asynchronous code? why or why not?

A

no because, you can’t populate the call stack with asynchronous code