Skip to content

Instantly share code, notes, and snippets.

@vicradon
Last active April 6, 2020 06:02
Show Gist options
  • Save vicradon/eff5d925cd098664c1fc6d4c5293aa63 to your computer and use it in GitHub Desktop.
Save vicradon/eff5d925cd098664c1fc6d4c5293aa63 to your computer and use it in GitHub Desktop.
/**
* This code uses the principle of single return per function.
* I did not reassign any object.
* This enables a cleaner experience and a testable code.
*
* Also note that I did not use "let" or "var (which is forbidden)".
* I only used const, to prevent reassignment errors.
*/
const data = [
{ principal: 2500, time: 1.8 },
{ principal: 1000, time: 5 },
{ principal: 3000, time: 1 },
{ principal: 2000, time: 3 },
]
const rateFinder = (interestObject) => {
const { principal, time } = interestObject;
if (principal >= 2500 && time > 1 && time < 3) {
return 3;
} else if (principal >= 2500 && time >= 3) {
return 4;
} else if (principal < 2500 && time <= 1) {
return 2;
} else {
return 1;
}
}
const singleInterestCalc = (interestObject) => {
const { principal, time } = interestObject;
const rate = rateFinder(interestObject);
const interest = (principal * rate * time) / 100;
return interest;
}
const interestCalculator = (data) => {
const interestData = data.map(i => singleInterestCalc(i));
console.log(interestData);
return interestData;
}
interestCalculator(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment