Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

learn-co-curriculum/phase-0-pac-3-function-parameters-lab

Repository files navigation

Learning Goals

  • 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

Introduction

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.

Define a Function That Uses a Parameter

Instructions: Define a function calledintroduction that defines aparameter,name, and returns the phrase: "Hi, my name is ${name}."

Define a Function That Uses Two Parameters

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]."

Define a Function That Uses an Optional Parameter

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

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors23


[8]ページ先頭

©2009-2025 Movatter.jp