Functions Flashcards

1
Q

What is a function?

A

It’s a set of instructions,
you want to run over and over again.

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

How does a function begin?

Let’s look at the structure of a function.

A

using the function keyword

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

What is the function name in this example?

A

[goToCoffeeShop]

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

What is the function parameter?

A

(drink)

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

What role does the curly braces serve?

A

start and end of function

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

What is an argument?

A

An argument is a value (primitive or object)
passed as input to a function.

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

How do you call a function?

A

use the function name followed by parenthesis and semi-colon

goToCoffeeShop();

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

Why use functions?

A

To avoid unnecessary repetition and effort

To avoid having to re-enter code in multiple places

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

What does this function do?

A

It generates a random number between 1 and 6.

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

In this example, where are the functions in this snippet?

A

Find by ()

floor is a function

random is a function

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

Will this code run?

A

No

The function hasn’t been called

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

What does the alert(); function do?

A

It creates a simple pop up window

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

How do you call a function?

A

functionName();

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

Why will this JavaScript run successfully?

A

The function is being called

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

What’s the advantage of using functions?

A

It produces multiple results,
without repeating code.

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

How is the function being called in this example?

A

alertRandom();

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

How do you call a function multiple times in a row?

A

repeat the function call

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

In this example, how many times is the function being called?

A

4 times

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

When the function is called 4 times, how is the number appearing on the screen?

A

It’s basically creating 4 popups, one after the other.
Each popup has a randomly generated number.

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

What is a return?

A

A return is a function that gives you the value of a variable.

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

Is return more flexible?

A

Yes

You can manipulate output as alert, console,
or store a value in a variable.

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

In this example, why does alert removed and return added?

A

In the original function, the alert was hardwired into it; this limits its functionality to only one way to output the code.

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

Why was the function renamed?

A

It no longer made sense to use alert in
the function name, as it was going to
be capable of more than that.

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

How do you call a function outcome from an alert?

A

alert( functionName() );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do you store the value of a function in a variable?
variable vName = functionName();
26
In this example, how was the function stored in a variable?
const dieRoll = getRandomNumber();
27
``` What's a practical example of using a function in the context of an eCommerce store? ```
You could create a function to calculate the total price of an item, including taxes and shipping costs in an online store.
28
Is it common to use returns in JavaScript?
Yes it is common to use returns It's common to use return values to use in mulitple ways.
29
If you don't specify a return value what happens?
It produces an error message: **undefined**
30
Is it possible to have multiple return statements?
Yes You can combine return statements to accomplish an objective.
31
What is a multiple return statement?
32
How is it helpful to have multiple return statements in the context of an online web form?
You can write a function that checks whether or not a form field is empty in a web form, then give the user a message on what they need to do next to complete the form.
33
Should the return statement(s) be at the beginning or end of the function?
Return statements should be at the end of the function.
34
Can returns retrieve multiple bits of information?
No Only one type, one value at a time
35
What are the 4 types of values a return can retrieve? CBS N
Content of a variable Booleans Strings Numbers
36
What's the first step?
Create a function & name it ## Footnote **function isFieldEmpty**
37
What's step 2?
Create a variable that equals to the info ID of the document. const field = document.querySelector('#info');
38
What's the third step logic?
Construct an if/else statement sequence that returns either true or false (booleans) depending upon whether or not variable value is correct.
39
What's the code for the 3rd step?
if else conditional statement consists of: if (!field.value) { return true; } else { return false; }
40
Can a conditional statement take two paths?
No It's either true or false, not both.
41
What is step 4?
Create a variable named **fieldtest** which **stores** the **value** of the function
42
What is Step 5 in this example?
Create a conditional statement that evaluates whether or not there is a value in the field. True or false. if (fieldTest) { alert('Please provide your information.'); }
43
What is an argument?
An argument is a **value** passed as **input** to a **function**.
44
What do parameters work like? They have alot in common with ....
Variables
45
What is a parameter?
It's a value passed to the function, so it can run a program.
46
Why would you use upper in place of a number?
It gives you flexibility to establish what upper is each time the function is called via an argument.
47
In the variable const randomNumber, why was upper added there?
6 was replaced with upper to change it from a fixed number to a parameter to let multiple values establish the upper limit.
48
What is the return doing?
This is the part of the function that's producing the return value mechanism. It's allowing the info to be 'sent back' any number of ways when the function is called upon.
49
Why are there multiple console.logs with randomNumbers?
It shows you how the **arguments** passed when the **function** is called is truly **returning** values in a **range**.
50
In this example, which is the parameter?
The parameter is within the function. | (upper)
51
Which is the argument, in this example?
The number specified when the function is called 6 100 1000 10
52
Can a function have multiple parameters?
Yes Multiple parameters can be established in a function
53
Do you need multiple arguments, when multiple parameters are established in a function?
Yes They should line up to avoid producing errors 1:1 ratio of parameters to arguments
54
Do parameters need quotation marks within a function?
No
55
What separates parameters when multiple parameters are defined in the function?
commas ,
56
How do you update the programming to include more than 1 parameter?
${item};
57
What's the term for the ${pastry}; in the programming?
Expression
58
When you establish 2 expressions ${drink} or ${pastry} in the programming, do you need to specify two arguments also?
Yes The function needs to have 2 arguments to run properly
59
When you call a function, what punctuation do you need to specify the arguments?
Each argument should be wrapped in single quotation marks goToCoffeeShop( 'Expresso', 'Donut');
60
With multiple arguments, should the comma be inside or outside of the string?
Outside the string goToCoffeeShop( 'Latte'**,**'Scone');
61
Can return specify more than 1 value?
No
62
Would this function return what we need it to return?
No It is missing string interpolation for the return value
63
How does string interpolation help the return perform what is needed?
Using string interpolation, we can get the return area to specify the parameters we need for the program.
64
How is string interpolation being used in this program?
Using expressions + string interpolation, the area and unit are defined within return
65
Why isn't every parameter listed as a return?
The calc is multiplying two values (width \* length); it's meant to condense into 1 value, rather than 2 measurements.
66
How do you call this function?
functionName(#, #, 'measurement'); getArea(10, 20, 'sq ft');
67
What would the console print out with getArea(10, 20, 'sq ft'); ?
200 sq ft
68
Should you create many parameters?
No It complicates things at several places, more to keep track of things
69
What's too many parameters/arguments?
Beyond 4 or 5, parameters it's better to use other methods as it gets tedious to match up parameters/arguments for the program.
70
Is it possible that as a website code grows, you'll accidentally or intentionally use the same variable name?
Yes, it is possible to reuse variable names
71
Do functions create the scope in which variables work?
Yes Variable names are contained within their function.
72
What's variable scope?
It's the programming constraint a variable operates within.
73
How are scopes helpful within JavaScript?
Scopes prevent conflicts and overriding of other programs.
74
How many times is the variable named person defined in this code?
2 times same const same variable name
75
What is a function scope?
Scopes defined within a scope is a function scope
76
Is function greeting a global scope or functional scope?
Functional scope
77
Is let person = 'Lee'; a functional scope or global scope?
Global scope, not created within a function
78
What's a best practice for global variables to prevent reassignment?
Use const for global variables to prevent accidental usage inside of functions
79
What is a function declaration?
It's a **function** keyword with function name **function** LearnBrainScape or **function** getRandomNumber(upper)
80
What is a function expression?
Function expressions let you assign a function to a variable
81
What is an anonymous function?
An anonymous function without a name after the keyword function.
82
With an anonymous function where does the name come from?
It comes from a variable name
83
Can you call a function declaration before it loads?
**Yes** You could use console.log(functionName() ); before it loads and it will return the value from the program
84
What is hoisting?
Lifting function declarations through the code, towards the top, near global scope
85
Does a function expression get hoisted?
No, function expressions are not hoisted -- It is only active when the code reaches that level.
86
If you call a **function expression** before it's defined you'll get an \_\_\_\_\_
**error**
87
What does this error message mean? ## Footnote **Uncaught referenceError: cannot access 'getRandsomNumber' before initialization**
It means the call came before the function expression. Function expressions have to be defined first, before they'll called upon.
88
Is this a function expression?
Yes, it is a function expression
89
With a function expression, is the function value stored in a variable
**Yes** the function value is stored in a variable
90
How do you know this is a function expression?
There is no name after the function keyword, just a parameter It has an equal sign assigning it a named variable
91
When should you use functions?
When there's repetition in the code Find yourself writing same code over and over Functions can help you group reusable codes
92
How do you create an arrow?
=\>
93
When converting a function into an arrow, what changes in the syntax?
function() is replaced with () =\>
94
Are arrow functions anonymous?
Arrow Functions Inherently anonymous
95
Why are arrow functions like function expressions?
Arrow functions behave like function expressions because they are not hoisted up in the code.
96
When are arrow functions called, inline or hoisted?
Arrow functions respond only when JS reaches the line of code it’s on; it is not hoisted.
97
Are function declarations hoisted?
Functional Declarations are hoisted, unlike arrow functions
98
Do you need parenthesis when you have 1 parameter specified in an arrow function?
No
99
What does this arrow function do?
It takes the specified value and multiples it
100
Do you need parenthesis with multiple parameters in an arrow function?
Yes
101
How can this one line statement be made shorter in syntax?
Remove curly braces
102
If you have a single line funciton with no parameters do you need parenthesis before the arrow token?
Yes
103
Why does a single-line arrow function with no parameteres require parenthesis before the arrow token?
With a single-line arrow function with NO parameters it requires parentheses before the arrow token to **avoid JS syntax errors.**
104
Looking at the code snippets, which is correct top or bottom? **Why?**
Bottom is correct Because with a single line, no parameters, in arrow function you need () between the = sign and arrow token
105
Why is it helpful to establish default values in function parameters?
If for any reason, you do not pass a specific argument to a function, the function falls back to the default value.
106
Does this code snippet expect an argument?
Yes It is expecting an argument relating to the (name)
107
If you enter the function name without specifying an argument, then what happens?
You get an error message, saying undefined.
108
What happens if you don't specify a function argument?
It can break your entire program.
109
Why would you want to specify a default function argument?
To prevent the program from not working altogether.
110
What is the default argument in this JS snippet?
student
111
Why does it work correctly?
The argument is being passed as a string to the function allowing it to provide the greeting with the specified name.
112
How do you specify a default argument?
You modify the parameter with an equal sign and string to give a default argument to the function.
113
Can a function have multiple parameters?
Yes, it is possible
114
How do you establish default parameters?
in the parameter you use the = sign to assign the value and string to define the default parameter
115
Even with established arguments, can you leave a blank when calling function?
No, you can't use blanks with default arguments the arguments are still expected
116
What word can you put in for an argument when you want the default response?
undefined
117
What happens when undefined is used to call the function?
Undefined works as a placeholder preventing an error This means it will use the default argument in the function
118
How do you begin a JS comment?
/\*\*
119
In a long comment, what symbol is used to mark the beginning of a new line of comments?
\* symbol starts each line after initial comment opens
120
What purpose does the @param tag serve?
@param tag provides name, type, and description of function parameter
121
Should you use one or many param tags per parameter?
Use one @param tag for each parameter you define
122
What's the purpose of the @return tag in a comment context?
@return documents the value the function returns with type and description
123
Why are comments helpful to document code?
Using a consistent and descriptive commenting approach makes your functions more predictable. Other developers who need to learn about your functions will have a faster and easier way to identify each part of a function.
124
Why is this comment well documented?
Each function component has a clear, concise, defined purpose in the explanation found within the comment.