Created
May 2, 2017 04:22
-
-
Save betterkenly/5aa67e8f2d9ba39af03e8873263cf838 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var salesTeam = [{name: {first: 'aleen', last: 'atkins'}, age: 26, sales: '$2314'}, | |
{name: {first: 'alvaro', last: 'angelos'}, age: 55, sales: '$1668'}, | |
{name: {first: 'denese', last: 'dossett'}, age: 29, sales: '$9248'}, | |
{name: {first: 'douglas', last: 'denney'}, age: 53, sales: '$5058'}, | |
{name: {first: 'earline', last: 'erickson'}, age: 19, sales: '$18876'}, | |
{name: {first: 'herman', last: 'hazell'}, age: 25, sales: '$2746'}, | |
{name: {first: 'homer', last: 'hirth'}, age: 26, sales: '$474'}, | |
{name: {first: 'hwa', last: 'heidt'}, age: 53, sales: '$9607'}, | |
{name: {first: 'hyon', last: 'hampshire'}, age: 46, sales: '$13598'}, | |
{name: {first: 'issac', last: 'ingerson'}, age: 45, sales: '$5225'}, | |
{name: {first: 'jeraldine', last: 'joplin'}, age: 39, sales: '$2891'}, | |
{name: {first: 'jin', last: 'jeffrey'}, age: 17, sales: '$14402'}, | |
{name: {first: 'joleen', last: 'jolin'}, age: 45, sales: '$15736'}, | |
{name: {first: 'jude', last: 'jarrett'}, age: 53, sales: '$7557'}, | |
{name: {first: 'magda', last: 'mireles'}, age: 18, sales: '$1498'}, | |
{name: {first: 'mistie', last: 'montealegre'}, age: 31, sales: '$6920'}, | |
{name: {first: 'nancy', last: 'napoli'}, age: 49, sales: '$5255'}, | |
{name: {first: 'regine', last: 'rohrbaugh'}, age: 33, sales: '$7881'}, | |
{name: {first: 'rolando', last: 'riebel'}, age: 35, sales: '$8573'}, | |
{name: {first: 'scarlett', last: 'stagg'}, age: 36, sales: '$7126'}, | |
{name: {first: 'sherron', last: 'strawn'}, age: 36, sales: '$8848'}, | |
{name: {first: 'susan', last: 'shilling'}, age: 29, sales: '$8542'}, | |
{name: {first: 'tama', last: 'tworek'}, age: 16, sales: '$9200'}, | |
{name: {first: 'tonisha', last: 'taunton'}, age: 41, sales: '$5219'}, | |
{name: {first: 'vergie', last: 'villescas'}, age: 25, sales: '$8712'}]; | |
// Let's start with pretty-ing up our data a little bit. Looks like we forgot to capitalize the first and last names of our employees when we entered them into our system. | |
// Write a function capitalize that takes in the salesTeam array and modifies it so that the first letter of the first and last names are capitalized. | |
// Note: this function does not have a return value, it only modifies the passed it argument. | |
function capitalize(salesTeam){ | |
return salesTeam.map(makeit); | |
} | |
function makeit(obj){ | |
obj.name.first = obj.name.first.substring(0,1).toUpperCase() + obj.name.first.substring(1); | |
obj.name.last = obj.name.last.substring(0,1).toUpperCase() + obj.name.last.substring(1); | |
return obj; | |
} | |
console.log(capitalize(salesTeam)); | |
function assert(expectedBehavior, descriptionOfCorrectBehavior) { | |
if (!expectedBehavior) { | |
console.log(descriptionOfCorrectBehavior); | |
} else { | |
console.log('test passed'); | |
} | |
} | |
//Edit the below invocation of the below assert function to use JSON.stringify() where necessary to test your capitalize function. | |
capitalize(salesTeam); | |
assert(JSON.stringify(salesTeam[2].name) === JSON.stringify({first: 'Denese', last: 'Dossett'}), 'should capitalize the first letter of the first and last names'); | |
// We are interested in knowing how our team is generally performing. Write a function averageTeamSales that takes in the salesTeam array and returns a single number, the average of all sales for the team. Make sure to round up your result. | |
// Tests will use a different team with the exact same data structure | |
// No other input values will be tested | |
// Tests will inspect the output value and type | |
var averageTeamSales = function (salesTeam) { | |
var total = salesTeam.reduce(function(a,b){ | |
return a + parseInt(b.sales.substring(1)); | |
},0); | |
return Math.round(total/ salesTeam.length); | |
}; | |
console.log(averageTeamSales(salesTeam)); | |
// Use the assert function provided in Part 2 to write a single meaningful test for averageTeamSales, using salesTeam as the input. | |
// Note: The average team sales is 7487. | |
function assert(expectedBehavior, descriptionOfCorrectBehavior) { | |
if (!expectedBehavior) { | |
console.log(descriptionOfCorrectBehavior); | |
} else { | |
console.log('test passed'); | |
} | |
} | |
var input = averageTeamSales(salesTeam); | |
assert(input === 7487, " should give the correct average team sales"); | |
// Write a function highestEarner that takes in the salesTeam array and returns a single string of the team member's first name, last name, and amount of sales. | |
// console.log(highestEarner(salesTeam)); // should output "Earline Erickson: $18876" | |
// {name: {first: 'vergie', last: 'villescas'}, age: 25, sales: '$8712'}]; | |
function highestEarner(salesTeam){ | |
var highestsales = 0; | |
var highestsalesperson; | |
for( var i = 0 ; i < salesTeam.length ; i++ ){ | |
var curr = salesTeam[i]; | |
if( parseInt(curr.sales.substring(1)) > highestsales ){ | |
highestsales = parseInt(curr.sales.substring(1)); | |
highestsalesperson = curr; | |
} | |
} | |
return highestsalesperson.name.first + " " + highestsalesperson.name.last + ": " + highestsalesperson.sales; | |
} | |
console.log(highestEarner(salesTeam)); | |
// Starter Code | |
// DO NOT ADJUST BELOW FUNCTIONS | |
function reduce(collection, accumulator, initialValue) { | |
each(collection, function(val) { | |
if (initialValue !== undefined) { | |
initialValue = accumulator(initialValue, val); | |
} else { | |
initialValue = val; | |
} | |
}) | |
return initialValue; | |
} | |
function each (collection, callback) { | |
if (Array.isArray(collection)) { | |
for (let i = 0; i < collection.length; i++) { | |
callback(collection[i], i, collection); | |
} | |
} else { | |
for (let key in collection) { | |
callback(collection[key], key, collection); | |
} | |
} | |
} | |
function filter(collection, predicate) { | |
// Your code below. You must use reduce in your solution. | |
return reduce(collection, function(newArr,cur){ | |
if(predicate(cur)){ | |
newArr.push(cur); | |
} | |
return newArr | |
},[]) | |
} | |
var arr = [1, 2, 3, 4]; | |
var obj = { | |
a: 1, | |
b: 2, | |
c: 3, | |
d: 4 | |
} | |
function isEven(val) { | |
return val % 2 === 0; | |
} | |
function isOdd(val) { | |
return !isEven(val); | |
} | |
var output1 = filter(arr, isEven); | |
console.log(output1); // --> [2, 4]; | |
var output2 = filter(obj, isOdd); | |
console.log(output2); // --> [1, 3]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment