each: Iterating Over Objectsfunction 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 of each 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"