Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active June 18, 2016 19:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhawkins/4e29c547c9391fa270c2 to your computer and use it in GitHub Desktop.
Save alexhawkins/4e29c547c9391fa270c2 to your computer and use it in GitHub Desktop.
Hack Reactor Underscore Exercise Answers
'use strict';
var _ = require('underscore');
//######### EXAMPLE 1 #################/
//use _.reduce to multiply all the values in an array
var arr = [10,20,40,50,60,70];
var multiplied = _.reduce(arr, function (x, y) { return x * y; });
//tests
console.log(multiplied); //1680000000
//######### EXAMPLE 2 #################/
//Use _.reduce to concatenate all strings in an array.
var str = ['cool', 'wicked', 'slick', 'dope'];
var output1 = _.reduce(str, function(word1, word2) { return word1.concat(word2); });
var output2 = _.reduce(str, function(word1, word2) { return word1 + ' ' + word2; });
//tests
console.log(output1); //coolwickedslickdope
console.log(output2); //cool wicked slick dope
//######### EXAMPLE 3 #################/
//Write a function that takes an array of names and congratulates them.
var input = ['Steve', 'Sally', 'George', 'Gina'];
var newString = 'Congratulations ' + _.reduce(input, function(x, y) { return x + ', ' + y; }) + '!';
//tests
console.log(newString); //Congratulations Steve, Sally, George, Gina!
//######### EXAMPLE 3i #################/
//BASIC _.PLUCK WORKS LIKE THIS
var objArray = [{ name : 'Biff Tannin', email: 'bifftanin@gmail.com'}, {name: 'Hans Gruber', email: 'hansgruber@gmail.com' },
{ name : 'Napolean Dynamite', email: 'ndyno@gmx.com'}, { name: 'Boo Radley', email: 'myboo@babyboy.com' } ];
//_.pluck takes an object and a property name and returns the property name's value.
var getNames = _.pluck(objArray, 'name'); //pluck the values belonging to the key/property 'name'
var getEmails = _.pluck(objArray, 'email'); //pluck the values belonging to the key/property 'email'
console.log(getNames.join(', ')); //Biff Tannin, Hans Gruber, Napolean Dynamite, Boo Radley
console.log(getEmails.join(', ')); //bifftanin@gmail.com, hansgruber@gmail.com, ndyno@gmx.com, myboo@babyboy.com
//Write your own version of pluck called myPluck.
var myPluck = function(objArr, property){
var newArr = []; //initialize blank array that we will push to
objArr.forEach(function(object){
for (var prop in object)
if (prop === property) { newArr.push(object[prop]); }
});
return newArr;
};
//or using a good ol' classic for loop
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
var myPluck = function(obj, propName) {
var arr = [];
for (var i = 0; i < obj.length; i++) {
for (var prop in obj[i]) {
if (prop === propName)
arr.push(obj[i][prop]);
}
}
return arr;
}
//tests
console.log(myPluck(stooges, 'name')); //[ 'moe', 'larry', 'curly' ]
console.log(myPluck(stooges, 'age')); // [ 40, 50, 60 ]
console.log(myPluck(objArray, 'name')); //[ 'Biff Tannin','Hans Gruber','Napolean Dynamite', 'Boo Radley' ]
console.log(myPluck(objArray, 'email')); // [ 'bifftanin@gmail.com','hansgruber@gmail.com','ndyno@gmx.com','myboo@babyboy.com' ]
//############EXAMPLE 3ii###################/
var addThem = _.partial(function(a,b,c){ return a + b + c; }, 5, 6);
//power function that allows you to determine the exponential
var powerAny = _.partial(Math.pow, 2); //2^3;
//power function with a set exponential ie. everything squared, everything cubed
var powerTwo = _.partial(Math.pow, _, 2); //everything will be squared
var powerThree = _.partial(Math.pow, _, 3); //everything will be cubed
//tests
console.log(addThem(4)); //15 (adds 4 + 5 + 6 = 15)
//Evaluates Math.power(2,3)
console.log(powerAny(3)); //8
//Evaluates Math.power(3,2)
console.log(powerTwo(3)); //9
//Evaluates Math.power(3,3)
console.log(powerThree(3)); // 27
//########### EXAMPLE 3iii ###############/
// Use _.filter to return all strings in an array that start with the letter 'Z'
var str = ['Zed', 'zeek', 'Zoro', 'god', 'jumanji'];
var zeeWord = _.filter(str, function(val) { return val.charAt(0) === 'Z'; } );
//use regExp and match instead
var zeeWord2 = _.filter(str, function(val) { return val.match(/^Z/); } );
//tests
console.log(zeeWord); //[ 'Zed', 'Zoro' ]
console.log(zeeWord2); //[ 'Zed', 'Zoro' ]
//########### EXAMPLE 3iv ###############/
//Use _.where to return all animals who contain the value 3 at the property name space.
var input = [ { name: 'Tiger', space: 7}, { name: 'Tiger2', space: 1}, { name: 'Tiger3', space: 3}, { name: 'Tiger4', space: 3} ];
// return array of objects where property space is set to 3
var output = _.where(input, { space: 3 });
//tests
console.log(output);
//[ { name: 'Tiger3', space: 3 }, { name: 'Tiger4', space: 3 } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment