Last active
August 1, 2019 22:47
-
-
Save nkhil/b28078ae39ccc6872080b788a8a53df3 to your computer and use it in GitHub Desktop.
This file contains 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
const order = [ | |
{ | |
width: 8, | |
height: 9, | |
depth: 7, | |
}, | |
{ | |
width: 18, | |
height: 29, | |
depth: 37, | |
}, | |
{ | |
width: 81, | |
height: 92, | |
depth: 74, | |
}, | |
{ | |
width: 104, | |
height: 240, | |
depth: 30, | |
}, | |
]; | |
function isParcelSmallerThanMaxDimensions(parcel, maxDimensions) { | |
return ( | |
parcel.width < maxDimensions && | |
parcel.height < maxDimensions && | |
parcel.depth < maxDimensions | |
); | |
} | |
function returnsParcelSize(parcel) { | |
let result; | |
switch (true) { | |
case isParcelSmallerThanMaxDimensions(parcel, 10): // make these constants in a separate file instead of hard coding eg: 10 | |
result = { smallParcel: 3 }; | |
break; | |
case isParcelSmallerThanMaxDimensions(parcel, 50): | |
result = { mediumParcel: 5 }; | |
break; | |
case isParcelSmallerThanMaxDimensions(parcel, 100): | |
result = { largeParcel: 15 }; | |
break; | |
default: | |
result = { extraLargeParcel: 30 }; | |
} | |
return result; | |
} | |
function calculateTotal(arrayOfParcels) { | |
const arrayOfPrices = arrayOfParcels.map(parcel => Object.values(parcel)); | |
return arrayOfPrices.reduce((sum, value) => sum + value[0], 0); | |
} | |
function createResultObject(array, totalPrice) { | |
const result = {}; | |
array.map(obj => { | |
const key = Object.keys(obj)[0]; | |
result[key] = obj[key]; | |
}); | |
result.totalPrice = totalPrice; | |
return result; | |
} | |
function resultOrchestrator(ordersArray) { | |
const parcelSizes = ordersArray.map(parcel => returnsParcelSize(parcel)); | |
const totalPrice = calculateTotal(parcelSizes); | |
return createResultObject(parcelSizes, totalPrice); | |
} | |
// To make it work, call the resultOrchestrator function and pass in the order array | |
// eg: resultOrchestrator(order) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment