Functions Flashcards
What is a function?
It’s a set of instructions,
you want to run over and over again.
How does a function begin?
Let’s look at the structure of a function.
using the function keyword
What is the function name in this example?
[goToCoffeeShop]
What is the function parameter?
(drink)
What role does the curly braces serve?
start and end of function
What is an argument?
An argument is a value (primitive or object)
passed as input to a function.
How do you call a function?
use the function name followed by parenthesis and semi-colon
goToCoffeeShop();
Why use functions?
To avoid unnecessary repetition and effort
To avoid having to re-enter code in multiple places
What does this function do?
It generates a random number between 1 and 6.
In this example, where are the functions in this snippet?
Find by ()
floor is a function
random is a function
Will this code run?
No
The function hasn’t been called
What does the alert(); function do?
It creates a simple pop up window
How do you call a function?
functionName();
Why will this JavaScript run successfully?
The function is being called
What’s the advantage of using functions?
It produces multiple results,
without repeating code.
How is the function being called in this example?
alertRandom();
How do you call a function multiple times in a row?
repeat the function call
In this example, how many times is the function being called?
4 times
When the function is called 4 times, how is the number appearing on the screen?
It’s basically creating 4 popups, one after the other.
Each popup has a randomly generated number.
What is a return?
A return is a function that gives you the value of a variable.
Is return more flexible?
Yes
You can manipulate output as alert, console,
or store a value in a variable.
In this example, why does alert removed and return added?
In the original function, the alert was hardwired into it; this limits its functionality to only one way to output the code.
Why was the function renamed?
It no longer made sense to use alert in
the function name, as it was going to
be capable of more than that.
How do you call a function outcome from an alert?
alert( functionName() );
How do you store the value of a function in a variable?
variable vName = functionName();
In this example, how was the function stored in a variable?
const dieRoll = getRandomNumber();
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.
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.
If you don’t specify a return value what happens?
It produces an error message: undefined
Is it possible to have multiple return statements?
Yes
You can combine return statements to accomplish an objective.
What is a multiple return statement?
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.
Should the return statement(s) be at the
beginning or end of the function?
Return statements should be at the
end of the function.
Can returns retrieve multiple bits of information?
No
Only one type, one value at a time
What are the 4 types of values a return can retrieve?
CBS N
Content of a variable
Booleans
Strings
Numbers
What’s the first step?
Create a function & name it
function isFieldEmpty
What’s step 2?
Create a variable that equals to the info ID of the document.
const field = document.querySelector(‘#info’);
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.
What’s the code for the 3rd step?
if else conditional statement consists of:
if (!field.value) {
return true;
} else {
return false;
}
Can a conditional statement take two paths?
No
It’s either true or false, not both.
What is step 4?
Create a variable named fieldtest
which stores the value of the function
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.’);
}
What is an argument?
An argument is a value passed as input to a function.
What do parameters work like?
They have alot in common with ….
Variables
What is a parameter?
It’s a value passed to the function, so it can run a program.
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.
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.
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.
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.