File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,10 @@ const parseCalorieData = (data) => {
1717}
1818
1919const findElfWithMost = ( data ) => {
20+ return sortElvesByCalories ( data ) [ 0 ] // Sort for the elf with the most calories
21+ }
22+
23+ const sortElvesByCalories = ( data ) => {
2024 const sum = ( a , b ) => { return a + b }
2125 const compare = ( a , b ) => {
2226 // compare sums of array values for sum-based sorting
@@ -27,10 +31,11 @@ const findElfWithMost = (data) => {
2731 )
2832 }
2933 data . sort ( compare )
30- return data [ 0 ] // Sort for the elf with the most calories
34+ return data
3135}
3236
3337module . exports = {
3438 findElfWithMost,
35- parseCalorieData
39+ parseCalorieData,
40+ sortElvesByCalories
3641}
Original file line number Diff line number Diff line change 11/* eslint-env mocha */
22const { expect } = require ( 'chai' )
3- const { findElfWithMost, parseCalorieData } = require ( './calories' )
3+ const { findElfWithMost, parseCalorieData, sortElvesByCalories } = require ( './calories' )
44
55const calorieData = `1000
662000
@@ -39,5 +39,17 @@ describe('--- Day 1: Calorie Counting ---', () => {
3939 . to . equal ( 24000 )
4040 } )
4141 } )
42+ describe ( 'sortElvesByCalories()' , ( ) => {
43+ it ( 'Sorts the list of elves by calories carried, maximum first' , ( ) => {
44+ expect ( sortElvesByCalories ( parsedCalorieData ) )
45+ . to . deep . equal ( [
46+ [ 7000 , 8000 , 9000 ] ,
47+ [ 5000 , 6000 ] ,
48+ [ 10000 ] ,
49+ [ 1000 , 2000 , 3000 ] ,
50+ [ 4000 ]
51+ ] )
52+ } )
53+ } )
4254 } )
4355} )
You can’t perform that action at this time.
0 commit comments