Last active
January 5, 2019 01:50
-
-
Save kenduigraha/9617dbc3bece1e7723bbb0b2ea327189 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 fractions = [100000, 50000, 20000, 10000, 5000, 1000, 500, 100, 50]; | |
var result = []; | |
var noFractionValue; | |
function doCheckNoFraction(input) { | |
return input < fractions[fractions.length-1] | |
} | |
function doCalculate(input) { | |
noFractionValue = doCheckNoFraction(input) ? input : 0; | |
if (noFractionValue > 0) { | |
return 'sisa : ' + noFractionValue; | |
} | |
for (var i = 0; i < fractions.length; i++) { | |
var total = 0; | |
if (input >= fractions[i]) { | |
if (input / fractions[i] > 0 || input === fractions[i]) { | |
total = (input - (input % fractions[i]))/fractions[i]; | |
input = input % fractions[i]; | |
if (doCheckNoFraction(input)) { | |
noFractionValue = input; | |
console.log('sisa : ' + noFractionValue); | |
} | |
result.push({ | |
fractionValue: fractions[i], | |
total: total | |
}); | |
} | |
} | |
} | |
return result; | |
} | |
console.log(doCalculate(12)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment