Coder Social home page Coder Social logo

phase-0-pac-3-function-parameters-lab's Introduction

JavaScript Function Parameters Lab

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 learn how to create a default value for a parameter.

If you haven't already, fork and clone this lab into your local environment. Navigate into its directory in the terminal, then run code . to open the files in Visual Studio Code. (If you are using a different text editor, the command will be different.)

You will be writing your code in the index.js file and running the tests by running learn test in the terminal.

Define a Function That Uses a Parameter

Instructions: Define a function called introduction that defines a parameter, 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: simply include both parameters in the parentheses in the function declaration line, separated by a comma:

function logTwoValues(value1, value2) {
  console.log(`The two values are ${value1} and ${value2}.`);
}

As you might expect, if we instead wanted to log three values โ€” or ten values โ€” 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 function called introductionWithLanguage that defines two parameters, name and language, and returns the phrase: "Hi, my name is [name] and I am learning to program in [language]."

Define a Function That Uses an Optional Parameter

In a previous lesson, we created a function that logs a personalized greeting:

function sayHelloTo(firstName) {
  console.log(`Hello, ${firstName}!`);
}

What if we wanted to make this function work whether or not a first name is passed in as an argument? We can do this by setting a default value for the firstName parameter:

function sayHelloTo(firstName = "User") {
  console.log(`Hello, ${firstName}!`);
}

Note that we have used the assignment operator (=) here to assign a default value. The way this works is, if the function is called with an argument, the argument's value will supersede the default value. If it's called without an argument, the function will use the default value, logging "Hello, User!"

Instructions: Copy the function you created for the second test and name it introductionWithLanguageOptional. It should have two parameters, name and language, and the second parameter should have a default value of "JavaScript".

phase-0-pac-3-function-parameters-lab's People

Contributors

7kingdavid7 avatar alpha-convert avatar annjohn avatar aturkewi avatar aviflombaum avatar bal360 avatar cernanb avatar dakotalmartinez avatar drakeltheryuujin avatar egomadking avatar gj avatar graciemcguire avatar ihollander avatar ivalentine avatar jessrudder avatar jnoconor avatar kwebster2 avatar lisaychuang avatar lizbur10 avatar maxwellbenton avatar pletcher avatar wolfhoward avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.