// function declaration
function square(x) {
return x * x;
}
// function expression
var square = function(x) {
return x * x;
};-
We have two ways of writing a function. The function declaration is what we've used so far, and the function expression is new to us. Rewrite the following function declarations using a function expression:
// 1. function cube(x) { return x * x * x; } // 2. function fullname(first, last) { return first + " " + last; } // 3. function power(base, exp) { if (exp === 0) { return 1; } return base * power(base, exp - 1); } // 4. function sumCubes(numbers) { var total = 0; for (var i = 0; i < numbers.length; i++) { total = total + cube(numbers[i]); } return total; }
-
Write
eachas seen below in yourmain.jsfile.function each(array, func) { for (var i = 0; i < array.length; i++) { func(array[i]); } }
-
Finish the implementation of
sumSquaresbelow (usingeach):function sumSquares(numbers) { var total = 0; // ... return total; }
-
Rewrite
sumCubesusingeach:function sumCubes(numbers) { var total = 0; for (var i = 0; i < numbers.length; i++) { total = total + cube(numbers[i]); } return total; }
-
Write a function called
productthat calculates the product of an array of numbers using aforloop; then, refactor it to useeach. -
Write a function called
cubeAllthat cubes each number in an array, and returns an array of all the numbers cubed using aforloop; then, refactor it to useeach. -
Write a function called
oddsthat accepts an array as a parameter and returns an array of just the odd numbers. If you wrote it usingeach, great! If you used anything else, refactor it to use aforloop.
-
Write a function
sumByAllElementsMultipliedByFourthat takes an array as an argument and returns the sum of all elements multiplied by four. -
Observe that
sumByAllElementsMultipliedByFouris a terrible name for a function – you should also notice thatsumByAllElementsMultipliedByFourlooks a lot likesumCubesandsumSquares.Write a function
sumBythat accepts two arguments: an array of numbers and a function. The function will be invoked upon each element in the array, and its result will be used to compute the sum.function sumBy(numbers, f) { // ... } var numbers = [1, 2, 3, 4]; sumBy(numbers, square); // => 30 sumBy(numbers, cube); // => 100 sumBy(numbers, function(number) { return number * 4; }); // => 40
-
How can you use
sumByto compute the sum of an array of numbers (just the plain sum)? -
Write a function
productBythat works likesumBy, but for products.
As an extended exercise, run back through your data modeling code from the past
few days and look for opportunities to refactor your use of for loops using
each, map and filter.
-
Write a function
doubleAllthat takes an array of numbers as a parameter and returns an array of all of those numbers doubled:function doubleAll(numbers) { // ... } doubleAll([1, 3, 10, 4, 7]); // => [2, 6, 20, 8, 14]
-
Write a function
halveAllthat takes an array of numbers as a parameter and returns an array of all of those numbers halved (divided by two). -
Write a function
uppercaseAllthat takes an array of strings as a parameter, and returns an array of all of those strings, but transformed to upper case. You can usetoUpperCaseto convert a string to upper case."hello, world".toUpperCase(); // => "HELLO, WORLD"
-
You should at this point notice a similarity between all of the above functions, as well as the
cubeAllfunction from the warmup. These functions all define what we call mappings; that is, they map from one value to another.// doubleAll maps from an array of numbers to an array of doubled numbers // [1, 2, 3, 4] => [2, 4, 6, 8] // halveAll maps from an array of numbers to an array of halved numbers // [1, 2, 3, 4] => [0.5, 1, 1.5, 2]
-
Write a function
mapthat takes two parameters: an array and a function that, when applied to a single element, produces a new element.mapshould return an array of all elements in the input array transformed using the input function. Your function should work like this:function map(array, f) { // ... } map([1, 2, 3, 4], function(x) { return x * 2; }); // => [2, 4, 6, 8]
-
Complete the invocations of
mapbelow to produce the desired output (you'll need to replace???with a function):map(["hello", "world"], ???); // => ["HELLO", "WORLD"] map(["HelLo", "WorLD"], ???); // => ["hello", "world"] map(["the", "quick", "brown", "fox", "jumped"], ???); // => [3, 5, 5, 3, 6] var people = [ {name: "Alyssa P. Hacker", age: 26}, {name: "Ben Bitdiddle", age: 34}, {name: "Eva Lu Ator", age: 19}, {name: "Lem E. Tweakit", age: 40} ]; map(people, ???); // => ["Alyssa P. Hacker", "Ben Bitdiddle", "Eva Lu Ator", "Lem E. Tweakit"] map(people, ???); // => ["Alyssa P. Hacker is 26", "Ben Bitdiddle is 34", "Eva Lu Ator is 19", "Lem E. Tweakit is 40"]
-
Write a function called
evensthat takes an array of numbers as a parameter, and returns an array of only the even numbers in the parameter. -
Write a function called
multiplesOfThreethat takes an array of numbers as a parameter, and returns an array of only the numbers that are multiples of three. -
Write a function called
positivesthat takes an array of numbers as a parameter, and returns an array of only the numbers that are positive. -
Write a function called
evenLengththat takes an array of strings and returns an array of only the strings with an even length. -
At this point, you should notice a pattern; write a function called
filterthat takes two parameters: an array and a function that, when invoked with an argument, will returntrueorfalse.filtershould return a new array of only the elements for which the function returnstrue:function filter(array, f) { // ... } filter([1, 2, 3, 4], function(x) { return x % 2 === 0; // x is even? }); // => [2, 4]
-
Use
filterto write/rewrite:oddspositivesnegativesevenLengthlargerThanSix(given an array of numbers, returns those larger than 6)
-
Using
filter, write a functionstartsWithCharthat accepts two parameters: an array of strings, and a character (e.g. "a"), and returns an array of only the strings that start with that character:function startsWithChar(strings, character) { // ... } var words = "the quick brown fox jumps over the lazy dog".split(" "); startsWithChar(words, "q"); // => ["quick"] startsWithChar(words, "t"); // => ["the", "the"]