This lab provides an opportunity to practice defining and coding some real-world functions.
Note: Feel free to work in pairs or groups to complete this lab. Also, Google/StackOverflow is a good friend to have around...
- Create an
index.html. - Create a directory named
js. - Create a file named
lab-functions.jsinside of thejsdirectory. - Use a
<script>element in the<head>ofindex.htmlto bring thelab-functions.jsfile into the web page. - The code in
lab-functions.jswill then execute whenever the page is loaded or refreshed.
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, my gift to you:
// 1.
function maxOfTwoNumbers(x, y) {
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-then-else construct. -
Define a function, as a function expression,
maxOfThreethat takes three numbers as arguments and returns the largest of them. -
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,
longestWordLengththat takes an array of strings (words) and returns the length of the longest word. -
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(['touch', 'me', 'in', 'the', 'morning'], 2);would return["touch", "the", "morning"].
If you finish the lab and want more to do, do the exercises we didn't get to from the JS Logic & Conditionals lesson this morning:
-
There is an event with ticket prices that are
$50,$65,$85for standard, premier, and premier plus (for drinks) seating. Seniors, veterans, and students receive a$10discount while standard patrons receive no discount. Based on hardcoded variables forticketTypeanddiscountType, print out a patronsticketPrice. -
Write this decision tree as a conditional:
(you'll need three variables: parentsVisiting, weather, and money, and the conditional should output what you should do).
For an extra bonus, wrap it up in a function called whatShouldIDoToday()
- Complete the following challenges using the ternary operator:
- Use conditionals to check if a hardcoded word is "javascript". If it is,
console.log"Great language!" - Use conditionals to check if a hardcoded number is
oddoreven, and thenconsole.logthe number isoddorevenwith the number's value. - Use conditionals to check if a hardcoded number is divisible by
2or3and thenconsole.logthat the number is divisible by two or three. - Use conditionals to check if a hardcoded
quantityis1or greater than one. If thequantityis one or greaterconsole.logeither1 petorquantity + " pets"respectively.

