Week 3 Flashcards

(106 cards)

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
when can you start executing a recursive function's stack frames
when you reach the base case
26
if there a limit on the number of times that a recursion is done? if yes, what is that limit
JS does have a call stack size limit which depends on how much memory is allocated to the JS program on your system
27
what is stack overflow
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
28
what is direct recursion?
functions directly calling themselves
29
what is indirect recursion
recursive loops across multiple functions
30
how is iterative code different from recursive code?
its less resource-intensive requires less planning easier to read and understnad
31
when should you choose recursion over iteration?
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
32
when should you use iteration over recursion
it is a problem that is no harder for 1000 things than 1-5, but will just take longer
33
what are the three steps that define a recursive function?
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)
34
what is an iife?
an immediately-invoked function; it is an anonymous function that is defined and then invoked as soon as it is defined
35
what are the steps to writ an IIFE?
1. wrap the anonympus function in the grouping operator () 2. invoke the function after the grouping operator with another set of parenthesis
36
how can we hang on to the result of an IIFE?
assigning the return value to a variable
37
do we have access to variables declared within an IIFE after the function has been called?
no
38
how does hoisting work, when a function is declared using function declaration syntax?
the name of the function and its contents are hoisted
39
explain how JS approaches hoisting when the function is declared with function declaration syntax
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
40
can you calla afunction that is declared using function declaration syntax, before it is actually declared?
yes
41
how does hoisting work when a function is declared using function expression syntax and using the keywords const and let
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
42
how does hoisting work when a function is declared using function expression syntax and using the keyword var
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
43
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
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
explain the relationship between variable assignment, function declaration, and variable declaration for variables declared with var, the same names, and within the same scope?
variable assignment trumps function declaration, but if the variable is not assigned to a value then function declaration trumps variable declaration
45
what is the default value of variables declared with let, const, and var?
let is undefined var is undefined const variables need to be assigned a value
46
what are the two main differences between the primitive data type and the reference data type?
primitive data types are immutable; primitive data types cannot have methods because they are not objects
47
What is runtime?
the execution of a progrma
48
what is the thread of execution?
the sequence of well-ordered commands
49
what does is it mean for an execution to be single threaded?
only one command can be processed at a time
50
what type of execution is JS? single or multi-threaded?
single
51
what does it mean for an execution to be multi-threaded?
multiple commands can be processed at the same time
52
what are some disadvantages to single-threaded execution?
takes longer to execute a program because it can only tackle one command at a time
53
what are some advantages of a single threaded execution?
it's easier and simpler for a computer to execute
54
what are some advantages of multithreaded execution?
its faster to execute a program
55
what are some disadvantages of multithreaded execution?
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
do the individual threads on a multithreaded execution do more than one command at a time?
no
57
what is the problem that all single-threaded languages face? and how do we fix this?
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
what are components of the event loop?
the call stack the message queue
59
what does the message queue keep track of?
tasks that cannot be executed at this moment, but will be execute once the current task is finished
60
what is js execution pattern referred to as an what does this mean?
its called run to completion because the execution of an ongoing task will never be interrupted by another task
61
what structure does the message queue take?
a queue
62
what pattern does the message queue follow
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
what are messages in the message queue? and what do they correspond to?
items that are stored on the queue; the correspond to events that have occurred but have not yet been processed
64
where are synchronous tasks performed?
on the call stack
65
where are asynchronous tasks stored while the current task is being processed on the stack?
on the queue
66
in what order do items on the queue get proccesed?
first come first serve
67
how do the call stack and the message queue interact?
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
what has to happen for the message queue to be utilized
a task has to wait to be fulfilled
69
what is synchronous code?
the code has an inherent order among the commands and this order execution is guaranteed
70
what is synchronous code?
the code has no guaranteed total order in which the commands are executed
71
is the time period in setTimeout exact? why or why not?
no, the time period specified in setTimeout is the minimum time that will elapse before executing the callback, it could take longer.
72
what does the setTimeout method do?
sets a timer which executes a function or a specified piece of code once the timer expires
73
what does setTimeOut accept?
a callback or just a piece of code and an amount of time in milliseconds
74
what is a non-blocking function? provide an example
an asynchronous function that doesn't prevent the code that follows their invocation from running
75
is setting a time required for the setTimeout function
no; default time is zero
76
what does the setInterval function do, what does it take in as parameters and how can we clear it?
executes a callback repeatedly at the specified delay; takes in cb and time in ms clearInterval
77
what must you input to close the readline interface? and when would you do this?
rl.close() after the question is answered
78
what happens to our readline if we don't close the interface?
it will hang and not exit
79
is the question synchronous or asynchronous
asynchronous
80
how do we get a command to come directly after a callback is invoked asyncronously?
pass that command inside of the callback
81
what does callback chaining allow us to do?
perform a series of asynchronous functions one after the other,
82
what is the recommended way to refactor callback chaining that requires a lot of nested structures
use named functions instead of passing in anonymous functions
83
when should you use name functions over anonymous when callback chaining?
when creating a callback chain longer than two
84
what is callback hell?
an overly nested callback chain
85
where should you place debugger keywords in your code and what does this do?
you should place them every time the scope changes; this tells the debugger where to look
86
what does the continue button on the debugger do?
will go from debugger to debugger statement
87
what does the step into button on the debugger do?
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
what does the step out button do on the debugger?
if we have stepped into a function, this button will take us out of that function
89
what does the step over button on the debugger do?
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
what are some use cases for recursion?
its an alternative to iteration used for handling a large set of data breaking problems into smaller problems, sub problems
91
can you have more than one base case?
yes
92
how does stack overflow occur in recursion?
when you don't tell your function when to stop recursing
93
what happens to the default parameter if that parameter is passed in a value during the function
it is overwritten
94
how many times can you run an iife
one
95
how can IIFes be useful?
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
explain for anonymous functions interact with the function expression syntax?
as soon as you give a variable a name it is no longer anonymous
97
write an IIFE that takes in no parameters, uses the fat arrow syntax and doesn't use the function keyword
(() => console.log('message here'))();
98
write an IIFE that takes in a parameters, uses the fat arrow syntax and doesn't use the function keyword
((name) => console.log(`hello ${name}`))('brandon');
99
which type of code blocks the call stack? synchronous or asynchronous
synchronous
100
why is asynchronous code useful?
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
what does the event loop do?
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
is the callstack FIFO or LIFO?
LIFO
103
is the messaging queue FIFO or LIFO
FIFO
104
does the call stack handle asynchronous or synchronous code
synchronous
105
does the messaging queue handle synchronous or asynchronous code
asynchronous
106
can recursion be performed with asynchronous code? why or why not?
no because, you can't populate the call stack with asynchronous code