- Notifications
You must be signed in to change notification settings - Fork8.9k
License
learn-co-curriculum/phase-0-pac-3-function-parameters-lab
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Define a function that uses a parameter
- Define a function that uses two parameters
- Define a function with a parameter that has a default value
In this lab, we'll practice using parameters in our functions. We'll also learnhow to create a default value for a parameter.
If you haven't already, fork and clone this lab into your local environment.Remember tofork a copy into your GitHub account first, then clone from thatcopy. Once you've cloned it down, navigate into its directory in the terminal,then runcode .
to open the files in Visual Studio Code. (If you are using adifferent text editor, the command will be different.)
You will be writing your code in theindex.js
file and running the tests byrunningnpm test
in the terminal. Remember to runnpm install
first toinstall the lab's dependencies.
Instructions: Define a function calledintroduction
that defines aparameter,name
, and returns the phrase: "Hi, my name is ${name}."
Setting up your function to use two parameters is straightforward: simplyinclude both parameters in the parentheses in the function declaration line,separated by a comma:
functionlogTwoValues(value1,value2){console.log(`The two values are${value1} and${value2}.`);}
As you might expect, if we instead wanted to log three values — or tenvalues — we can just continue listing the parameters in the parentheses,with commas between each.
Instructions: To pass the second test, you'll need to define a functioncalledintroductionWithLanguage
that defines two parameters,name
andlanguage
, and returns the phrase: "Hi, my name is [name] and I am learning toprogram in [language]."
In a previous lesson, we created a function that logs a personalized greeting:
functionsayHelloTo(firstName){console.log(`Hello,${firstName}!`);}
What if we wanted to make this function workwhether or not a first name ispassed in as an argument? We can do this by setting adefault value for thefirstName
parameter:
functionsayHelloTo(firstName="User"){console.log(`Hello,${firstName}!`);}
Note that we have used the assignment operator (=
) here to assign a defaultvalue. The way this works is, if the function is calledwith an argument, theargument's value will supersede the default value. If it's calledwithout anargument, the function will use the default value, logging "Hello, User!"
Instructions: Copy the function you created for the second test and name itintroductionWithLanguageOptional
. It should have two parameters,name
andlanguage
, and the second parameter should have a default value of"JavaScript".
After you have all the tests passing, remember to commit and push your changesup to GitHub, then submit your work to Canvas using CodeGrade. If you need areminder, go back to theComplete Your First Software EngineeringAssignment lesson to review the process.
About
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.