Created
March 30, 2022 20:49
-
-
Save alvissraghnall/7906783816b3f425c43c1b9d96f04eb8 to your computer and use it in GitHub Desktop.
A brief solution at the Tribinnov Africa Assessment test question.
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
/** | |
* Collects inputs in percentages, for example: COOP(30, 20, 10, 15, 25) means 30%, 20%, 10%, 15%, and 25% respectively. | |
* | |
*/ | |
function COOP (loan, convertibleNote, grants, equity, sharedProfits, baseAmount = 5000) { | |
const args = [ ...arguments ]; | |
if(args.length < 5) throw new Error("Must provide percentages for all sub-wallets"); | |
if(baseAmount !== 5000) { | |
throw new Error("Invalid Base Amount. Ensure it is 5000."); | |
} | |
if(args.find(arg => arg > 50)) { | |
throw new Error("Sub-wallet percentage cannot be more than 50%."); | |
} | |
const totalPercentages = args.reduce((acc, curr) => acc += curr, 0); | |
if(totalPercentages > 100) { | |
throw new Error("Sum of percentages must not be more than 100."); | |
} | |
if(totalPercentages !== 100) throw new Error("Sum of all inputed percentages must be 100."); | |
const actualValues = args.map(arg => arg / 100 * baseAmount); | |
return { | |
loan: actualValues[0], | |
convertibleNote: actualValues[1], | |
grants: actualValues[2], | |
equity: actualValues[3], | |
sharedProfits: actualValues[4] | |
} | |
} | |
//console.log(COOP(10, 20, 30, 20, 20)); | |
//COOP(45, 16, 50); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment