function each(array, func) {
for (var i = 0; i < array.length; i++) {
func(array[i], i);
}
}
-
Using our new version of each, write a function called
indexedExponentials
that, when given an array of numbers as an argument, returns a new array of numbers where each number has been raised to the power of its index, e.g.:function indexedExponentials(numbers) { // TODO: your code here } indexedExponentials([2, 5, 7, 4]); // => [2^0, 5^1, 7^2, 4^3] => [1, 5, 49, 64]
-
Write a function called
evenIndexedOddNumbers
that, when given an array of numbers as an argument, returns an array of only the odd numbers with even indices.function evenIndexedOddNumbers(numbers) { // TODO: Your code here } evenIndexedOddNumbers([1, 3, 3, 4, 7, 10]); // => [1, 3, 7]
-
Write a function called
evenIndexedEvenLengths
(yeah, a bit of a mouthful) that accepts an array of strings as a parameter, and returns only the strings that are found at an even index that also have an even length, e.g.:function evenIndexedEvenLengths(strings) { // TODO: your code here } evenIndexedEvenLengths(["lion", "monkey", "aardvaark", "cat", "doge"]); // => ["lion", "doge"] evenIndexedEvenLengths(["red", "green", "purple", "blue", "yellow"]); // => ["purple", "yellow"]
function each(coll, func) {
if (Array.isArray(coll)) {
for (var i = 0; i < coll.length; i++) {
func(coll[i], i);
}
} else {
for (var key in coll) {
func(coll[key], key);
}
}
}
-
Using our updated version of
each
, write the following functions:values
: accepts an object as a parameter, and outputs an array of the object's values.keys
: accepts an object as a parameter, and outputs an array of the object's keys.
-
Write a function called
keysLongerThan
that accepts two parameters -- an object and a number -- and returns a new object with only the key/value pairs in the input object whose keys are longer than the numeric argument, e.g.:keysLongerThan({name: "Annyeong", age: 25, favoriteColor: "blue"}, 3); // => {name: "Annyeong", favoriteColor: "blue"} keysLongerThan({name: "Annyeong", age: 25, favoriteColor: "blue"}, 4); // => {favoriteColor: "blue"}
Make sure to use the new version of
each
for this. -
Write a function called
incrementValues
that accepts an object as a parameter, and outputs an object with all of its numeric values incremented by one. You'll want to use the updated version ofeach
for this, and you will need to check the type of each value to determine whether or not it should be incremented.HINT: Try entering the following expressions in to your console:
typeof "hello"; // => "string" typeof 1; // => "number" typeof true; // => "boolean"
function each(coll, f) {
if (Array.isArray(coll)) {
for (var i = 0; i < coll.length; i++) {
f(coll[i], i);
}
} else {
for (var key in coll) {
f(coll[key], key);
}
}
}
function filter(array, predicate) {
var acc = [];
each(array, function(element, i) {
if (predicate(element, i)) {
acc.push(element);
}
});
return acc;
}
Use filter
to write the following functions:
-
Write a function called
evens
that takes an array of numbers as a parameter, and returns an array of only the even numbers in the parameter. -
Write a function called
multiplesOfThree
that 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
positives
that takes an array of numbers as a parameter, and returns an array of only the numbers that are positive. -
Write a function called
evenLength
that takes an array of strings and returns an array of only the strings with an even length.
-
Use
filter
to write the following functions:odds
: Accepts an array of numbers, and returns the odd ones.negatives
: Likepositives
, but with negatives!largerThanSix
: given an array of numbers, returns those larger than 6.
-
Using
filter
, write a functionstartsWithChar
that 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"]
-
Extend the
filter
function to pass the index of each element to the predicate; then, complete the following:Write a function called
evenIndexedEvenLengths
(yeah, a bit of a mouthful) that accepts an array of strings as a parameter, and returns only the strings that are found at an even index that also have an even length, e.g.:function evenIndexedEvenLengths(strings) { // TODO: your code here } evenIndexedEvenLengths(["lion", "monkey", "aardvaark", "cat", "doge"]); // => ["lion", "doge"] evenIndexedEvenLengths(["red", "green", "purple", "blue", "yellow"]); // => ["purple", "yellow"]
HINT: You'll need to ensure that your version of
filter
also passes each elements' index (or key in the case of objects) to the predicate. -
Write a version of
filter
that works on arrays and objects; then, use your updated version offilter
to filter an object with values of mixed types to an object with only numeric values. You should be able to use your modified version offilter
as follows:filter({a: 1, b: "dog", c: true}, function(value) { return typeof value === "number"; }); // => {a: 1}