Created
May 2, 2016 20:09
-
-
Save shtakai/ef55f27e72ba2d37e2a1b678a4948e98 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
convertCoin(10.25); | |
convertCoin(22.99); | |
convertCoin(159.79); | |
convertCoin(3159.69); | |
function convertCoin(input) { | |
var result = [{ | |
'name': 'dollar', | |
'amount': 0, | |
'cent': 100 | |
}, { | |
'name': 'half-dollar', | |
'amount': 0, | |
'cent': 50 | |
}, { | |
'name': 'quarter-dollar', | |
'amount': 0, | |
'cent': 25 | |
}, { | |
'name': 'dime', | |
'amount': 0, | |
'cent': 10 | |
}, { | |
'name': 'nickel', | |
'amount': 0, | |
'cent': 5 | |
}, { | |
'name': 'pennie', | |
'amount': 0, | |
'cent': 1 | |
}]; | |
remain = parseInt(input * 100); | |
for (var i in result) { | |
result[i]['amount'] = parseInt(remain / result[i]['cent']); | |
remain -= result[i]['cent'] * result[i]['amount']; | |
} | |
console.log(); | |
console.log('input(dollar)', input); | |
for (var i in result) { | |
if(result[i]['amount'] > 0) | |
console.log(result[i]['amount'] + ' ' + result[i]['name']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`✗ node convertCoin.js
input(dollar) 10.25
10 dollar
1 quarter-dollar
input(dollar) 22.99
22 dollar
1 half-dollar
1 quarter-dollar
2 dime
4 pennie
input(dollar) 159.79
159 dollar
1 half-dollar
1 quarter-dollar
4 pennie
input(dollar) 3159.69
3159 dollar
1 half-dollar
1 dime
1 nickel
4 pennie`