- Notifications
You must be signed in to change notification settings - Fork0
License
JavaPrime/js-functions-lab-js-intro-000
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Write a function that returns a value
- Write a function that takes in a parameter
- Write a function that takes in multiple parameters
You'll be writing your solution inindex.js
.
In this lab, we're going to develop our communication skills in JavaScript.We're feeling festive, so we're going to be wrapping up common holiday greetingsas functions so that we don't have to repeat ourselves. The beauty of functionsis that we could reuse these functions for the text of greeting cards, forspoken greetings, for song lyrics, etc...
There are two ways main ways to include variables inside a string. Say we had a variable nameddate
that we assign to a value:
vardate="July 3rd"
In JavaScript, we can use operators to concatenate (join) two strings, or in this case, a string and a variable, like so:
console.log("My birthday is "+date)
Withdate
defined, the above code will logMy birthday is July 3rd
. However,by using a slightly modified syntax, we can achieve the same thing by embeddinga variable into a string. These are calledtemplateliteralsand rewriting the aboveconsole.log
with one would look like this:
console.log(`My birthday is${date}`)
This will also logMy birthday is July 3rd
.
Now, there are two important changes to the syntax: Any variables we want toinclude must be wrapped in curly braces ({ }
) and preceded by a dollar sign($
). In addition, instead of single,'
, or double quotes,"
, wemust use backticks,`
. If backticks are not used, JavaScriptwillinterpret the dollar sign and curly braces as a normal part of the string,resulting inMy birthday is ${date}
! Any expression can be included intemplate literals, not just variables, so we could write something like:
console.log(`I have${1+1} pets`)
And getI have 2 pets
. Note that this will not log the same thing if you didthe following:
console.log("I have "+1+1+" pets")
This logsI have 11 pets
! JavaScript converts both1
s into strings rather than adding them together first. You would need to write the following to get the same result as template literals:
console.log("I have "+(1+1)+" pets")
You can use either operators or template literals to construct larger stringswith dynamic values. Template literals are just a way to make your code a littleeasier to read and to help ensure JavaScript does not misinterpret whencombining different data types into strings, like we just saw.
Write a function named
happyHolidays
. This function should not accept anyparameters and should return the string"Happy holidays!"
.Write a function named
happyHolidaysTo
. This function should accept aparameter of the name of the person you want to wish happy holidays, andreturn the string`Happy holidays, ${name}!`
Write a function named
happyCustomHolidayTo
. This function should accept twoparameters, the holiday you want to wish them well for, and the name of theperson you're wishing well. Order of parameters matters, so make sure to first pass in the holiday and then the name. This function should return the string`Happy ${holiday}, ${name}!`
Write a function named
holidayCountdown
. This function should accept twoparameters, the holiday name and number of days until that holiday. Thefunction should return the string`It's ${days} days until ${holiday}!`
. Note that althoughdays
isused first when constructing the returned string,holidayCountdown
shouldtake in the holiday name first, then the days until that holiday.
ViewFunctions Lab on Learn.co and start learning to code for free.
About
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- JavaScript67.8%
- HTML32.2%