Skip to content

Instantly share code, notes, and snippets.

@valeriecodes
Last active February 12, 2018 18:09
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 valeriecodes/e9efc12b4094a9d11589a41ec760e4c6 to your computer and use it in GitHub Desktop.
Save valeriecodes/e9efc12b4094a9d11589a41ec760e4c6 to your computer and use it in GitHub Desktop.
All Tests Pass: Week 4
/**
* All Tests Pass: Week 4
*
* Working with native Array methods.
*
* This week, you're working at Acme Artwork, a small art store whose owner needs help understanding their sales for any given
* month. The last two month's worth of sales are available to you as an array of structured objects, with various bits of data
* describing who bought what for what price.
*
* For the purposes of this puzzle, the sales records are kept at the bottom of the puzzle file. Check there for the structure
* of each record.
*
* The owner has asked that you keep the implementation as flexible as possible, so you've decided to split up the processing
* of records into a few discrete functions.
*
* - `function getPrice(item)` given an object representing the sales record, extract the price from the record and return it.
* - `priceStringToNumber(price)` Sales stored in a string format `$1,000.00`. This function will take a price string and
* return a numeric representation.
* - `isHighValueSale(saleValue)` - This function will take the value of a sale and return true/false based on whether it's
* considered high value or not. The owner of the store considers anything over $100 to be a high value sale.
* - `sum(a, b)` - This function will return the sum of the two passed in variables `a` and `b`.
*/
function totalSales(listOfSales) {
return listOfSales.map(getPrice)
.map(priceStringToNumber)
.reduce(sum);
}
function totalHighValueSales(listOfSales) {
return listOfSales.map(getPrice)
.map(priceStringToNumber)
.filter(isHighValueSale)
.reduce(sum);
}
function getPrice(item) {
return item.price;
}
function priceStringToNumber(price) {
return parseFloat(price.replace(/\$|\,/, ''));
}
function isHighValueSale(saleValue) {
return saleValue > 100;
}
function sum(accumulator, nextValue) {
return accumulator + nextValue;
}
function toDecimalPlace(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
// Don't edit anything after this line.
assertEqual(toDecimalPlace(totalSales(januarySales()), 2), 10756.66, "January Total Sales."); //10756.66
assertEqual(toDecimalPlace(totalSales(februarySales()), 2), 11020.08, "February Total Sales."); //11020.08
assertEqual(toDecimalPlace(totalHighValueSales(januarySales()), 2), 10632.37, "January High Value Sales."); //10632.37
assertEqual(toDecimalPlace(totalHighValueSales(februarySales()), 2), 11020.08, "February High Value Sales."); //11020.08
console.log("All clear 🎉");
/**
* Basic assert.
*
* Example: assert(foo == true, "Foo equal true");
*
* @param assertion
* @param message
*/
function assert(assertion, message) {
if (!assertion) {
console.trace();
throw (!!message) ? `Assertion fail: ${message}.` : `Assertion fail.`;
} else {
console.log((!!message) ? `Pass: ${message}` : "Assertion passed.");
}
}
/**
* Helper function for deep object equal.
*
* Example: assertEqual({foo: "bar"}, {foo: "bar"}, "Objects equal.");
*
* @param first
* @param second
* @param message
*/
function assertObjectEqual(first, second, message) {
if (!first || !second) {
throw (!!message) ? `Assertion fail: ${message}.` : `Assertion fail.`;
}
let firstKeys = Object.keys(first),
secondKeys = Object.keys(second);
if (firstKeys.length !== secondKeys.length) {
throw (!!message) ? `Assertion fail: ${message}.` : `Assertion fail.`;
}
for (let key in firstKeys) {
if (typeof first[key] === "object") {
assertObjectEqual(first[key], second[key], message);
} else if (first[key] !== second[key]) {
throw (!!message) ? `Assertion fail: ${message}.` : `Assertion fail.`;
}
}
console.log((!!message) ? `Pass: ${message}` : "Assertion passed.");
}
/**
* Assert equal.
*
* Example: assertEqual(true, true, "True equals true.");
*
* @param first
* @param second
* @param message
*/
function assertEqual(first, second, message) {
if (typeof first === "object") {
assertObjectEqual(first, second, message);
} else {
assert(first === second, message);
}
}
function januarySales() {
return [ { name: 'Arielle Barrows',
address: '11882 Stevie Crossroad Suite 778',
city: 'Tannerburgh',
state: 'South Dakota',
zip: '04742',
purchase_date: 'Tue Feb 06 2018 19:49:48 GMT-0500 (EST)',
item: 'ipsum et omnis',
price: '$495.08' },
{ name: 'Magdalena Thiel',
address: '201 Emilio Mill Suite 340',
city: 'Schroedertown',
state: 'Oregon',
zip: '91876-2249',
purchase_date: 'Tue Feb 06 2018 08:58:17 GMT-0500 (EST)',
item: 'iure nesciunt reprehenderit',
price: '$923.46' },
{ name: 'Anne Mraz',
address: '274 Langworth Corner Apt. 915',
city: 'Rodriguezshire',
state: 'Wisconsin',
zip: '17953',
purchase_date: 'Tue Feb 06 2018 11:41:25 GMT-0500 (EST)',
item: 'veniam eum autem',
price: '$770.05' },
{ name: 'Nelson Satterfield',
address: '2610 Hermiston Union Apt. 444',
city: 'West Murielfort',
state: 'Maryland',
zip: '21417',
purchase_date: 'Tue Feb 06 2018 05:27:34 GMT-0500 (EST)',
item: 'similique praesentium aperiam',
price: '$598.85' },
{ name: 'Orpha Gerhold',
address: '64866 Lacey Vista Suite 403',
city: 'New Traceyland',
state: 'Minnesota',
zip: '50906-1091',
purchase_date: 'Tue Feb 06 2018 13:55:29 GMT-0500 (EST)',
item: 'magni ipsum et',
price: '$730.48' },
{ name: 'Petra Kertzmann',
address: '84010 Cyrus Corners Apt. 972',
city: 'Abelshire',
state: 'Arkansas',
zip: '59404-2924',
purchase_date: 'Tue Feb 06 2018 16:12:22 GMT-0500 (EST)',
item: 'vel accusantium omnis',
price: '$138.58' },
{ name: 'Gail Konopelski',
address: '10410 Stroman Light Suite 397',
city: 'Mosciskiborough',
state: 'New York',
zip: '35092',
purchase_date: 'Tue Feb 06 2018 17:07:34 GMT-0500 (EST)',
item: 'sint dolorum velit',
price: '$686.91' },
{ name: 'Maryse Leannon',
address: '663 Roob Mountains Apt. 953',
city: 'Augustbury',
state: 'Florida',
zip: '32305',
purchase_date: 'Tue Feb 06 2018 19:45:44 GMT-0500 (EST)',
item: 'est qui possimus',
price: '$582.34' },
{ name: 'Jackie Homenick',
address: '45841 Herta Mountain Apt. 656',
city: 'Mrazmouth',
state: 'Iowa',
zip: '42260-1649',
purchase_date: 'Tue Feb 06 2018 06:38:07 GMT-0500 (EST)',
item: 'ut explicabo quis',
price: '$513.13' },
{ name: 'David Balistreri',
address: '01450 Ibrahim Burgs Apt. 971',
city: 'Haneport',
state: 'Oregon',
zip: '29629',
purchase_date: 'Mon Feb 05 2018 21:41:39 GMT-0500 (EST)',
item: 'eligendi commodi vero',
price: '$69.73' },
{ name: 'Francesco Gutmann',
address: '608 Mohamed Field Suite 767',
city: 'Grantside',
state: 'Kentucky',
zip: '26195',
purchase_date: 'Tue Feb 06 2018 03:20:22 GMT-0500 (EST)',
item: 'rem facere harum',
price: '$525.96' },
{ name: 'Florencio Pouros',
address: '7570 Harvey Road Apt. 898',
city: 'Aylafort',
state: 'Kentucky',
zip: '15953-6825',
purchase_date: 'Tue Feb 06 2018 04:25:35 GMT-0500 (EST)',
item: 'eum ea eligendi',
price: '$710.60' },
{ name: 'Madge Von',
address: '94358 Bahringer Throughway Apt. 197',
city: 'West Kattieview',
state: 'Oklahoma',
zip: '59196-4115',
purchase_date: 'Tue Feb 06 2018 08:49:41 GMT-0500 (EST)',
item: 'eum qui odio',
price: '$674.39' },
{ name: 'Jordan Thiel',
address: '64717 Keagan Stream Apt. 345',
city: 'West Roxane',
state: 'Oregon',
zip: '05450-7187',
purchase_date: 'Tue Feb 06 2018 11:34:44 GMT-0500 (EST)',
item: 'omnis sed est',
price: '$54.56' },
{ name: 'Teagan Hegmann',
address: '9391 Langosh Locks Apt. 761',
city: 'Lucileside',
state: 'Arkansas',
zip: '08728-0601',
purchase_date: 'Tue Feb 06 2018 17:37:55 GMT-0500 (EST)',
item: 'magni saepe deleniti',
price: '$989.93' },
{ name: 'Bernadine Bernhard',
address: '128 Chauncey Streets Apt. 048',
city: 'Murphyshire',
state: 'Texas',
zip: '61250-8458',
purchase_date: 'Tue Feb 06 2018 11:02:04 GMT-0500 (EST)',
item: 'in voluptas nesciunt',
price: '$397.89' },
{ name: 'Flavie Metz',
address: '541 Ophelia Key Suite 497',
city: 'Lake Estel',
state: 'North Dakota',
zip: '57430',
purchase_date: 'Tue Feb 06 2018 03:18:38 GMT-0500 (EST)',
item: 'ut laboriosam illo',
price: '$447.72' },
{ name: 'Robin Wintheiser',
address: '8229 Carter Lane Suite 471',
city: 'South Kristian',
state: 'Arkansas',
zip: '72629-6632',
purchase_date: 'Tue Feb 06 2018 16:57:55 GMT-0500 (EST)',
item: 'eius corporis sit',
price: '$813.36' },
{ name: 'Lindsey Padberg',
address: '82886 Spinka Forks Apt. 380',
city: 'Lake Jacintostad',
state: 'Hawaii',
zip: '20592-9857',
purchase_date: 'Tue Feb 06 2018 06:06:51 GMT-0500 (EST)',
item: 'repellat id dolorem',
price: '$412.22' },
{ name: 'Freida Weimann',
address: '7412 Monahan Wells Suite 421',
city: 'Vivianemouth',
state: 'Louisiana',
zip: '21955-2778',
purchase_date: 'Tue Feb 06 2018 20:23:39 GMT-0500 (EST)',
item: 'sed impedit ut',
price: '$221.42' } ];
}
function februarySales() {
return [ { name: 'Nolan Koepp',
address: '8876 Strosin Pike Suite 806',
city: 'Port Gerdastad',
state: 'Indiana',
zip: '98892-3986',
purchase_date: 'Tue Feb 06 2018 07:02:55 GMT-0500 (EST)',
item: 'tenetur quo voluptatem',
price: '$422.34' },
{ name: 'Raymundo Wintheiser',
address: '18499 Easter Landing Apt. 918',
city: 'Krajcikview',
state: 'North Dakota',
zip: '84007-2834',
purchase_date: 'Tue Feb 06 2018 01:47:05 GMT-0500 (EST)',
item: 'neque et ut',
price: '$783.08' },
{ name: 'Violet Runolfsdottir',
address: '571 Amir Turnpike Apt. 027',
city: 'East Tamara',
state: 'West Virginia',
zip: '75065-4453',
purchase_date: 'Tue Feb 06 2018 08:26:56 GMT-0500 (EST)',
item: 'accusamus eos alias',
price: '$832.36' },
{ name: 'Akeem Mayert',
address: '1854 Morar Pass Suite 544',
city: 'West Devanteborough',
state: 'Illinois',
zip: '54222',
purchase_date: 'Tue Feb 06 2018 13:38:54 GMT-0500 (EST)',
item: 'a architecto eum',
price: '$996.90' },
{ name: 'Melissa Stehr',
address: '306 Roberto Rapid Apt. 783',
city: 'Joshuaton',
state: 'Arizona',
zip: '52676',
purchase_date: 'Tue Feb 06 2018 02:25:45 GMT-0500 (EST)',
item: 'ipsa neque voluptatem',
price: '$219.85' },
{ name: 'Franco Cruickshank',
address: '020 Prosacco Mission Apt. 379',
city: 'Vestamouth',
state: 'Arizona',
zip: '14177-0680',
purchase_date: 'Mon Feb 05 2018 23:00:13 GMT-0500 (EST)',
item: 'quis architecto pariatur',
price: '$235.57' },
{ name: 'Emanuel Haag',
address: '77302 Edythe Spring Apt. 383',
city: 'Hermanmouth',
state: 'Iowa',
zip: '98523',
purchase_date: 'Tue Feb 06 2018 19:16:56 GMT-0500 (EST)',
item: 'ipsam accusamus assumenda',
price: '$190.17' },
{ name: 'Carmelo Quigley',
address: '0448 Brendon Oval Suite 281',
city: 'Ernserburgh',
state: 'North Carolina',
zip: '13797-2319',
purchase_date: 'Tue Feb 06 2018 21:37:55 GMT-0500 (EST)',
item: 'nihil enim et',
price: '$939.83' },
{ name: 'Clementina Hessel',
address: '660 Medhurst Square Suite 307',
city: 'West Shakira',
state: 'Mississippi',
zip: '60422',
purchase_date: 'Mon Feb 05 2018 22:25:52 GMT-0500 (EST)',
item: 'maxime iure aut',
price: '$452.86' },
{ name: 'Willis Bednar',
address: '175 Edison Ranch Apt. 969',
city: 'Lake Bettie',
state: 'Missouri',
zip: '62470',
purchase_date: 'Tue Feb 06 2018 03:17:10 GMT-0500 (EST)',
item: 'autem eos quis',
price: '$646.36' },
{ name: 'Marietta Keebler',
address: '133 Cassin Flats Suite 067',
city: 'East Antone',
state: 'Alabama',
zip: '11450',
purchase_date: 'Tue Feb 06 2018 10:17:25 GMT-0500 (EST)',
item: 'et dolore eos',
price: '$819.43' },
{ name: 'Laisha Raynor',
address: '428 Nitzsche Circle Apt. 167',
city: 'Roweport',
state: 'South Dakota',
zip: '06560-8334',
purchase_date: 'Tue Feb 06 2018 03:39:08 GMT-0500 (EST)',
item: 'aut dicta qui',
price: '$873.39' },
{ name: 'Jazlyn Ward',
address: '55282 Felton Villages Apt. 988',
city: 'Nealstad',
state: 'Hawaii',
zip: '59482',
purchase_date: 'Tue Feb 06 2018 06:13:52 GMT-0500 (EST)',
item: 'delectus quam omnis',
price: '$230.47' },
{ name: 'Kamron McClure',
address: '708 Mraz Union Suite 331',
city: 'Port Garrison',
state: 'Nevada',
zip: '35989-0389',
purchase_date: 'Tue Feb 06 2018 16:12:12 GMT-0500 (EST)',
item: 'eos nesciunt est',
price: '$356.52' },
{ name: 'Irving Cronin',
address: '485 O\'Reilly Lakes Suite 681',
city: 'Eliasmouth',
state: 'Iowa',
zip: '38505-1556',
purchase_date: 'Tue Feb 06 2018 00:11:26 GMT-0500 (EST)',
item: 'vel numquam saepe',
price: '$469.30' },
{ name: 'Aiden Ziemann',
address: '6861 Tod Fords Apt. 692',
city: 'Kenview',
state: 'Michigan',
zip: '00330-8068',
purchase_date: 'Tue Feb 06 2018 05:20:00 GMT-0500 (EST)',
item: 'dolorem sunt laboriosam',
price: '$330.21' },
{ name: 'Gail Trantow',
address: '6477 Prosacco Parks Suite 823',
city: 'Cassinton',
state: 'California',
zip: '04990',
purchase_date: 'Tue Feb 06 2018 15:45:13 GMT-0500 (EST)',
item: 'est ut quia',
price: '$867.21' },
{ name: 'Evert Prohaska',
address: '076 Neal Roads Apt. 418',
city: 'Lake Candace',
state: 'Nebraska',
zip: '21159-8752',
purchase_date: 'Tue Feb 06 2018 19:32:17 GMT-0500 (EST)',
item: 'libero fugit magnam',
price: '$568.24' },
{ name: 'Breana Huel',
address: '7515 Barney Fort Suite 961',
city: 'Gordonhaven',
state: 'Montana',
zip: '33846',
purchase_date: 'Tue Feb 06 2018 05:29:18 GMT-0500 (EST)',
item: 'necessitatibus sit aut',
price: '$342.13' },
{ name: 'Krystina Kuhlman',
address: '110 Hyatt Terrace Suite 758',
city: 'Baumbachtown',
state: 'Minnesota',
zip: '11787-5922',
purchase_date: 'Tue Feb 06 2018 17:11:36 GMT-0500 (EST)',
item: 'omnis dolor et',
price: '$443.86' } ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment