Do Now: Let’s create a function that prints a reminder to the console. Using a function declaration, create a function called getReminder().
In the function body of getReminder(), log the following reminder to the console: 'Water the plants.'
Let’s create another function that prints a useful Spanish travel phrase to the console.
Do Now 2: Using a function declaration, create a function called greetInSpanish().
Add code to the function body of greetInSpanish():
In the function body console.log() the following Spanish phrase to the console: 'Buenas Tardes.'
Parameters allow you to pass data to the function and use those variables in the function.
Part 1 Directions: Create the functions below and the examples.
Imagine that you manage an online store. When a customer places an order, you send them a thank you note. Let’s create a function to complete this task:
Define a function called sayThanks() as a function declaration.
In the function body of sayThanks(), add code such that the function writes the following thank you message to the console when called: 'Thank you for your purchase! We appreciate your business.'
Call sayThanks() to view the thank you message in the console.
Functions can be called as many times as you need them.
Imagine that three customers placed an order and you wanted to send each of them a thank you message. Update your code to call sayThanks() three times.
The sayThanks() function works well, but let’s add the customer’s name in the message.
Add a parameter called name to the function declaration for sayThanks().
With name as a parameter, it can be used as a variable in the function body of sayThanks().
Using name and string concatenation, change the thank you message into the following: 'Thank you for your purchase '+ name + '! We appreciate your business.'
A customer named Cole just purchased something from your online store. Call sayThanks() and pass 'Cole' as an argument to send Cole a personalized thank you message.
Directions: Create a function for finding the area of a rectangle. Call the function 3 times using the data above.
The function makeShoppingList() creates a shopping list based on the items that are passed to the function as arguments.
Imagine that you always purchase milk, bread, and eggs every time you go shopping for groceries. To make creating a grocery list easier, let’s assign default values to the parameters in makeShoppingList().
Change the parameters of makeShoppingList() into default parameters :
Assign ‘milk’ as the default value of item1.
Assign ‘bread’ as the default value of item2.
Assign ‘eggs’ as the default value of item3.
Imagine if we needed to order monitors for everyone in an office and this office is conveniently arranged in a grid shape. We could use a function to help us calculate the number of monitors needed!
Declare a function monitorCount() that has two parameters. The first parameter is rows and the second parameter is columns.
Let’s compute the number of monitors by multiplying rows and columns and then returning the value.
In the function body of the function you just wrote, use the return keyword to return rows * columns.
Now that the function is defined, we can compute the number of monitors needed. Let’s say that the office has 5 rows and 4 columns.
Declare a variable named numOfMonitors using the const keyword and assign numOfMonitors the value of invoking monitorCount() with the arguments 5 and 4.
To check that the function worked properly, log numOfMonitors to the console.
Now let’s write another function that uses the monitorCount function to figure out the price.
Below monitorCount Create a function declaration named costOfMonitors that has two parameters, the first parameter is rows and the second parameter is columns.
Add a return statement that returns the value of calling monitorCount(rows, columns) multiplied by 200.
Declare a variable named totalCost using the const keyword. Assign to totalCost the value of calling costOfMonitors() with the arguments 5 and 4 respectively.
To check that the function worked properly, log totalCost to the console.
Part 2 Directions: Create the functions and examples below.