For this lab, you'll practice creating and using functions in JavaScript. You'll start with simple functions and gradually work up to more complex problems involving parameters, return values, and higher-order functions.
In functions.js, complete each prompt by writing a JavaScript function that satisfies the requirements. Make sure to invoke each function to run your code and verify it works as expected.
function example(){
console.log('This function prints this message, but returns undefined')
}
example()
// => This function prints this message but returns undefined
// => undefined- Basic Function
Write a function namedgreetthat prints "Hello, World!" to the console.
-
Single Parameter Function
Write a function namedgreetPersonthat takes in anameparameter and prints "Hello, [name]!" to the console. -
Multiple String Parameters
Write a function namedintroducePersonthat takes in two parameters:nameandoccupation. It should print "Hi, my name is [name] and I am a [occupation]." -
Number Parameter
Write a function nameddoubleNumthat takes in a number and returns its double. -
Multiple Number Parameters
Write a function namedaddNumsthat takes in two numbers and returns their sum. -
Mixed Parameters
Write a function nameddescribePetthat takes in three parameters:petName(string),species(string), andage(number). It should print "My pet [petName] is a [age]-year-old [species]."
-
Check Even or Odd
Write a function namedisEventhat takes in a number and returnstrueif the number is even andfalseif the number is odd. -
Calculate Area of a Rectangle
Write a function namedcalculateAreathat takes in two parameters:lengthandwidth, and returns the area of the rectangle. -
Reverse a String
Write a function namedreverseStringthat takes in a string and returns the reversed version of the string. -
Count Vowels in a String
Write a function namedcountVowelsthat takes in a string and returns the number of vowels in the string. -
Return Object from Mixed Parameters
Write a function namedcreatePetthat takes in three parameters:petName(string),species(string), andage(number). It should return an object with the keys: 'name', 'species', 'age'.
-
Function with Default Parameters
Write a function namedwelcomeMessagethat takes in anameparameter and prints "Welcome, [name]!" If no name is provided, the function should default to "Guest." -
Function Calling Another Function
Write a function namedcalculateAndLogthat takes in two numbers, calls theaddNumbersfunction to calculate their sum, and logs the result to the console. -
Arrow Function
Rewrite thedoubleNumberfunction as an arrow function nameddoubleNum. -
Higher-Order Function
Write a function namedapplyFunctionthat takes in two parameters: a number and a callback function. It should apply the callback to the number and return the result.Example:
const result = applyFunction(5, doubleNum); console.log(result); // Should print 10
- Complete all drills in the file functions.js.
- Test each function to ensure it works as expected.
- Push your completed work to your repository and submit the link on Google Classroom.
- Be prepared to explain how your code works during a follow-up discussion.
Good luck and happy coding! 🚀