Before this lesson, you should already be able to:
- Differentiate between data types (strings, numbers, Booleans).
- Use if/else if/else conditionals to control program flow based on Boolean conditions.
- Create and manipulate arrays.
- Use loops to iterate through the elements in an array.
- Describe why functions are useful.
- Describe how parameters relate to functions.
- Explain how return statements are used in JavaScript.
Let's practice writing some functions!
Follow the requirements in the list below:
- Define a function
maxOfTwoNumbers
that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in JavaScript. Do some Googling to figure this out if you forget how conditionals work. - Define a function
maxOfThree
that takes three numbers as arguments and returns the largest of them. - Write a function
isCharacterAVowel
that takes a character (i.e., a string of length 1) and returns true if it is a vowel and false otherwise. - Define a function
sumArray
and a functionmultiplyArray
that sums and multiplies (respectively) all the numbers in an array of numbers. For example,sumArray([1,2,3,4])
should return 10, andmultiplyArray([1,2,3,4])
should return 24. - Write a function that returns the number of arguments passed to the function when called.
- Define a function
reverseString
that reverses a string. For example, reverseString("jag testar") should return the string "ratset gaj". - Write a function
findLongestWord
that takes an array of words and returns the length of the longest word in the array. - Write a function
filterLongWords
that takes an array of words and a numberi
and returns a new array of words that are longer thani
characters long.
Bonus 1
Define a function logBetween(lowNum, highNum)
that will print every number
from lowNum
to highNum
, inclusive. Inclusive means that the range includes
lowNum
and highNum
. Hint: this function only needs to print using console.log
,
it does not need to return.
Examples:
logBetween(-1, 2); // prints
// -1
// 0
// 1
// 2
logBetween(14, 6); // prints nothing
logBetween(4,6); // prints
// 4
// 5
// 6
Bonus 2 For this bonus, you'll have to do some research on objects.
- Write a
function
that takes a string as an argument and returns an object where:
- The keys are the characters that occur in the string.
- The values are the number of occurrences for each letter, regardless of the case.
For example, calling the function with the string "General Assembly" will return:
{
a: 2,
b: 1,
e: 3,
g: 1,
l: 2,
m: 1,
n: 1,
r: 1,
s: 2,
y: 1
}
Open the file main.js
. All the function names are already inside the file; you just need to implement the functions by adding code inside each one.
Use the Chrome Dev Tools console to practice executing your code.
Alternately, you can add console.log();
statements in your main.js
file; then you should see the values displayed in the console when you open the index.html
file in your browser.
You'll review the answers with the instructor in class.
- Readings