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
Q

How do you store the value of a function in a variable?

A

variable vName = functionName();

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

In this example, how was the function stored in a variable?

A

const dieRoll = getRandomNumber();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
What's a practical example of using a
function in the context of an eCommerce store?
A

You could create a function to calculate
the total price of an item, including taxes
and shipping costs in an online store.

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

Is it common to use returns in JavaScript?

A

Yes it is common to use returns

It’s common to use return values to use in mulitple ways.

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

If you don’t specify a return value what happens?

A

It produces an error message: undefined

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

Is it possible to have multiple return statements?

A

Yes

You can combine return statements to accomplish an objective.

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

What is a multiple return statement?

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

How is it helpful to have multiple return
statements in the context of an online web form?

A

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.

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

Should the return statement(s) be at the
beginning or end of the function?

A

Return statements should be at the
end of the function.

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

Can returns retrieve multiple bits of information?

A

No

Only one type, one value at a time

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

What are the 4 types of values a return can retrieve?

CBS N

A

Content of a variable

Booleans

Strings

Numbers

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

What’s the first step?

A

Create a function & name it

function isFieldEmpty

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

What’s step 2?

A

Create a variable that equals to the info ID of the document.

const field = document.querySelector(‘#info’);

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

What’s the third step logic?

A

Construct an if/else statement sequence that returns either true or false (booleans) depending upon whether or not variable value is correct.

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

What’s the code for the 3rd step?

A

if else conditional statement consists of:

if (!field.value) {
return true;
} else {
return false;
}

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

Can a conditional statement take two paths?

A

No

It’s either true or false, not both.

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

What is step 4?

A

Create a variable named fieldtest
which stores the value of the function

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

What is Step 5 in this example?

A

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.’);
}

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

What is an argument?

A

An argument is a value passed as input to a function.

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

What do parameters work like?

They have alot in common with ….

A

Variables

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

What is a parameter?

A

It’s a value passed to the function, so it can run a program.

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

Why would you use upper in place of a number?

A

It gives you flexibility to establish what upper is each time the function is called via an argument.

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

In the variable const randomNumber,
why was upper added there?

A

6 was replaced with upper to change it from a fixed number to a parameter to let multiple values establish the upper limit.

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

What is the return doing?

A

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.

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

Why are there multiple console.logs with randomNumbers?

A

It shows you how the arguments passed when the function is called is truly returning values in a range.

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

In this example, which is the parameter?

A

The parameter is within the function.

(upper)

51
Q

Which is the argument, in this example?

A

The number specified when the function is called

6

100

1000

10

52
Q

Can a function have multiple parameters?

A

Yes

Multiple parameters can be established in a function

53
Q

Do you need multiple arguments, when multiple parameters are established in a function?

A

Yes

They should line up to avoid producing errors

1:1 ratio of parameters to arguments

54
Q

Do parameters need quotation marks within a function?

A

No

55
Q

What separates parameters when multiple parameters are defined in the function?

A

commas

,

56
Q

How do you update the programming to include more than 1 parameter?

A

${item};

57
Q

What’s the term for the ${pastry}; in the programming?

A

Expression

58
Q

When you establish 2 expressions ${drink} or ${pastry} in the programming, do you need to specify two arguments also?

A

Yes

The function needs to have 2 arguments to run properly

59
Q

When you call a function, what punctuation do you need to specify the arguments?

A

Each argument should be wrapped in single quotation marks

goToCoffeeShop( ‘Expresso’, ‘Donut’);

60
Q

With multiple arguments, should the comma be inside or outside of the string?

A

Outside the string

goToCoffeeShop( ‘Latte’,‘Scone’);

61
Q

Can return specify more than 1 value?

A

No

62
Q

Would this function return what we need it to return?

A

No

It is missing string interpolation for the return value

63
Q

How does string interpolation help the return perform what is needed?

A

Using string interpolation, we can get the return area to specify the parameters we need for the program.

64
Q

How is string interpolation being used in this program?

A

Using expressions + string interpolation, the area and unit are defined within return

65
Q

Why isn’t every parameter listed as a return?

A

The calc is multiplying two values (width * length); it’s meant to condense into 1 value, rather than 2 measurements.

66
Q

How do you call this function?

A

functionName(#, #, ‘measurement’);

getArea(10, 20, ‘sq ft’);

67
Q

What would the console print out with getArea(10, 20, ‘sq ft’); ?

A

200 sq ft

68
Q

Should you create many parameters?

A

No

It complicates things at several places,
more to keep track of things

69
Q

What’s too many parameters/arguments?

A

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
Q

Is it possible that as a website code grows, you’ll accidentally or intentionally use the same variable name?

A

Yes, it is possible to reuse variable names

71
Q

Do functions create the scope in which variables work?

A

Yes

Variable names are contained within their function.

72
Q

What’s variable scope?

A

It’s the programming constraint a variable operates within.

73
Q

How are scopes helpful within JavaScript?

A

Scopes prevent conflicts and overriding of other programs.

74
Q

How many times is the variable named person defined in this code?

A

2 times

same const

same variable name

75
Q

What is a function scope?

A

Scopes defined within a scope is a function scope

76
Q

Is function greeting a global scope or functional scope?

A

Functional scope

77
Q

Is let person = ‘Lee’; a functional scope or global scope?

A

Global scope, not created within a function

78
Q

What’s a best practice for global variables
to prevent reassignment?

A

Use const for global variables to prevent
accidental usage inside of functions

79
Q

What is a function declaration?

A

It’s a function keyword with function name

function LearnBrainScape or

function getRandomNumber(upper)

80
Q

What is a function expression?

A

Function expressions let you assign a function to a variable

81
Q

What is an anonymous function?

A

An anonymous function without a name after the keyword function.

82
Q

With an anonymous function where does the name come from?

A

It comes from a variable name

83
Q

Can you call a function declaration before it loads?

A

Yes

You could use console.log(functionName() );
before it loads and it will return the value from the program

84
Q

What is hoisting?

A

Lifting function declarations
through the code, towards the top, near global scope

85
Q

Does a function expression get hoisted?

A

No, function expressions are not hoisted –

It is only active when the code reaches that level.

86
Q

If you call a function expression before it’s defined you’ll get an _____

A

error

87
Q

What does this error message mean?

Uncaught referenceError: cannot
access ‘getRandsomNumber’ before initialization

A

It means the call came before the function expression.

Function expressions have to be defined
first, before they’ll called upon.

88
Q

Is this a function expression?

A

Yes, it is a function expression

89
Q

With a function expression, is the function value stored in a variable

A

Yes

the function value is stored in a variable

90
Q

How do you know this is a function expression?

A

There is no name after the function keyword, just a parameter

It has an equal sign assigning it a named variable

91
Q

When should you use functions?

A

When there’s repetition in the code

Find yourself writing same code over and over

Functions can help you group reusable codes

92
Q

How do you create an arrow?

A

=>

93
Q

When converting a function into an arrow, what changes in the syntax?

A

function() is replaced with () =>

94
Q

Are arrow functions anonymous?

A

Arrow Functions Inherently anonymous

95
Q

Why are arrow functions like function expressions?

A

Arrow functions behave like function expressions because they are not hoisted up in the code.

96
Q

When are arrow functions called, inline or hoisted?

A

Arrow functions respond only when JS reaches the line of code it’s on; it is not hoisted.

97
Q

Are function declarations hoisted?

A

Functional Declarations are hoisted, unlike arrow functions

98
Q

Do you need parenthesis when you have 1 parameter
specified in an arrow function?

A

No

99
Q

What does this arrow function do?

A

It takes the specified value and multiples it

100
Q

Do you need parenthesis with multiple parameters
in an arrow function?

A

Yes

101
Q

How can this one line statement be made shorter in syntax?

A

Remove curly braces

102
Q

If you have a single line funciton with no parameters do you need parenthesis before the arrow token?

A

Yes

103
Q

Why does a single-line arrow function with no parameteres require parenthesis before the arrow token?

A

With a single-line arrow function with NO parameters it requires parentheses before the arrow token to avoid JS syntax errors.

104
Q

Looking at the code snippets, which is
correct top or bottom? Why?

A

Bottom is correct

Because with a single line, no parameters, in arrow function you need () between the = sign and arrow token

105
Q

Why is it helpful to establish
default values in function parameters?

A

If for any reason, you do not pass a specific argument to a function, the function falls back to the default value.

106
Q

Does this code snippet expect an argument?

A

Yes

It is expecting an argument

relating to the (name)

107
Q

If you enter the function name without specifying an argument, then what happens?

A

You get an error message, saying undefined.

108
Q

What happens if you don’t specify a function argument?

A

It can break your entire program.

109
Q

Why would you want to specify a default function argument?

A

To prevent the program from not working altogether.

110
Q

What is the default argument in this JS snippet?

A

student

111
Q

Why does it work correctly?

A

The argument is being passed as a string to the function allowing it to provide the greeting with the specified name.

112
Q

How do you specify a default argument?

A

You modify the parameter with an equal sign and string to give a default argument to the function.

113
Q

Can a function have multiple parameters?

A

Yes, it is possible

114
Q

How do you establish default parameters?

A

in the parameter you use the = sign to assign the value
and string to define the default parameter

115
Q

Even with established arguments, can you
leave a blank when calling function?

A

No, you can’t use blanks with default arguments
the arguments are still expected

116
Q

What word can you put in for an
argument when you want the default response?

A

undefined

117
Q

What happens when undefined is used to call the function?

A

Undefined works as a placeholder preventing an error
This means it will use the default argument in the function

118
Q

How do you begin a JS comment?

A

/**

119
Q

In a long comment, what symbol is used
to mark the beginning of a new line of comments?

A

* symbol starts each line after initial comment opens

120
Q

What purpose does the @param tag serve?

A

@param tag provides name, type, and description of function parameter

121
Q

Should you use one or many param tags per parameter?

A

Use one @param tag for each parameter you define

122
Q

What’s the purpose of the @return tag in a comment context?

A

@return documents the value the function returns with type and description

123
Q

Why are comments helpful to document code?

A

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
Q

Why is this comment well documented?

A

Each function component has a clear, concise, defined
purpose in the explanation found within the comment.