
JavaScript Functions Lab
Introduction
This lab provides an opportunity to practice defining and coding some real-world functions.
Note: Feel free to work in pairs to complete this lab. Also, Google/StackOverflow is a good friend to have around...
Exercise
Setup & Instructions
Create a new HTML/CSS/JS repl in repl.it for these exercises.
Title your repl JS Function Lab.
This lab is not a deliverable.
Requirements
Define and code the functions below.
Define the functions using the approach as specified (either as a function expression or declaration).
Be sure to number each function with a comment above it.
After each function, call it at least once and console.log the results.
For example, here's the first function, our gift to you:
// 1.
function maxOfTwoNumbers(x, y) {
if (x >= y) {
return x;
} else {
return y;
}
// or more "elegantly" using the fantastic ternary expression!
// return x >= y ? x : y;
}
console.log(maxOfTwoNumbers(3, 9));
// 2.
...Here are the functions:
- (completed above) Define a function, as a function declaration,
maxOfTwoNumbersthat takes two numbers as arguments and returns the largest of them. If they are the same, return that number. Use the if-else construct or a ternary expression - the Math.max method is not allowed. - Define a function, as a function expression,
maxOfThreethat takes three numbers as arguments and returns the largest of them. Again, the Math.max method is not allowed. - Define a function, as a function declaration,
isCharAVowelthat takes a character as an argument and returns true if it is a vowel, false otherwise. - Define a function, as a function expression,
sumArraythat takes an array of numbers and returns the sum of those numbers. For example,sumArray([2, 4, 5]);would return11. - Define a function, as a function declaration,
multiplyArraythat takes an array of numbers and returns the product those numbers. For example,multiplyArray([2, 4, 5]);would return40. - Define a function, as a function expression,
numArgsthat returns the number of arguments passed to the function when called. - Define a function, as a function declaration,
reverseStringthat takes a string, reverses the characters, and returns it. For example,reverseString('rockstar');would return the string "ratskcor". - Define a function, as a function expression,
longestStringInArraythat takes an array of strings as an argument and returns the length of the longest string. - Define a function, as a function declaration,
stringsLongerThanthat takes an array of strings and a number as arguments; and returns an array of the strings that are longer than the number passed in. For example,stringsLongerThan(['say', 'hello', 'in', 'the', 'morning'], 3);would return["hello", "morning"].