Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhawkins/c8927fbd890809a7af0d to your computer and use it in GitHub Desktop.
Save alexhawkins/c8927fbd890809a7af0d to your computer and use it in GitHub Desktop.
Hack Reactor Callback Exercise Answers
'use strict';
var _ = require('underscore'); //require underscore library
/* NOTE: to use the above you need to install underscore on your computer first.
If you are using node.js, type 'npm install underscore' at the command line. */
//##############EXERCISE 1######################
var slang = ['cool', 'sick', 'dope', 'wicked', 'boss'];
var slangObj = { word1: 'cool', word2: 'sick', word3: 'dope', word4: 'wicked', word5: 'boss' };
//using underscore
_.each(slang, function(word){console.log(word);});
//or use native js forEach method
slang.forEach(function(word){console.log(word);});
//now loop through an ojbect. Use underscore,
//native js forEach can't be done in a single line
_.each(slangObj, function(value) {console.log(value);});
//you can also get the key or both value and key
_.each(slangObj, function(value, key) {console.log(key);});
//##############EXERCISE 2######################
var helloArr = ['bonjour', 'hello', 'hola'];
//use native js 'some' method to check if any values in our array
//are equal to the specified value. 'some' returns a boolean
var checkValue = function(arr, val) {
return arr.some(function(word){ return word === val; });
};
//could also use underscore some
var checkValue2 = function(arr, val) {
return _.some(arr, function(word){ return word === val; });
};
//tests
console.log(checkValue(helloArr, 'hello')); //true
console.log(checkValue(helloArr, 'hell3')); //false
console.log(checkValue2(helloArr, 'hola')); //true
console.log(checkValue2(helloArr, 'ewrge')); //false
//##############EXERCISE 3######################
var languages = ['ruby', 'javascript', 'python'];
var checkValue3 = function(arr, val) {
var check = false; //intialize check var to false, must prove true
_.each(languages, function(language){ if (val === language) check = true; });
return check; //returns true if the language was found in the each loop othwerside defaults to false
};
//tests
checkValue3(languages, 'ruby') ? console.log('We found it!') : console.log('Could\'t find it buddy! Sorry.');
//We found it!
checkValue3(languages, 'dimsum') ? console.log('We found it!') : console.log('Could\'t find it buddy! Sorry.');
//Could't find it buddy! Sorry.
//##############EXERCISE 4######################
input = {six: 6, four: 4, three: 3, twelve: 12};
var output = []; //initialize empty array
//loop through the object using for in and push object property value to new array
for (var prop in input)
output.push(input[prop]);
//tests
console.log(output); //[6, 4, 3, 12]
//##############EXERCISE 5######################
input = {two: 2, four: 4, three: 3, twelve: 12};
var output1 = _.map(input, function(value){ return value; })
//you can also return the key
var output2 = _.map(input, function(value, key){ return key; });
//or return the key and value
var output3 = _.map(input, function(value, key){ return key + ': ' + value;});
//tests
console.log(output1); //[2, 4, 3, 12]
console.log(output2); //["two", "four", "three", "twelve"]
console.log(output3); //["two: 2", "four: 4", "three: 3", "twelve: 12"]
//##############EXERCISE 6######################
var input = [9,8,7,6,5,2];
//map returned undefined for odds so just use native filter method
var output1 = input.filter(function(num){return num % 2 === 0;});
//or underscore ._filter
var output2 = _.filter(input, function(num){return num % 2 === 0;});
//tests
console.log(output1); //[8, 6, 2]
console.log(output2); //[8, 6, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment