-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (42 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Im initializaing this outside,
// Since the Question wants to create a function
// that returns only the total Coins needed
let coinsNumber = {};
function getCoinDenominations(unSortedDenominations, amount) {
const hasValidInput = validateInput(unSortedDenominations, amount);
if (!hasValidInput) {
return -1;
}
// Sort the Denominations in Descending order
denominations = unSortedDenominations.sort().reverse();
return denominations.reduce((totalCoins, denomination) => {
// Gets the Maximum coins that can be converted to this denominator
const noOfCoinsForDenomination = parseInt(amount / denomination);
if (noOfCoinsForDenomination) {
coinsNumber[denomination] = noOfCoinsForDenomination;
amount = amount % denomination;
}
return totalCoins + noOfCoinsForDenomination;
}, 0) || -1;
}
function printOutput(totalCoins) {
const printOutput = Object.keys(coinsNumber).map((coinValue) => `${coinsNumber[coinValue]}x${coinValue}`);
if (totalCoins !== -1) {
console.log(`${totalCoins}(${printOutput.join(' + ')})`);
} else {
console.log('The Amount cannot be given with the provided denominations');
}
}
function validateInput(denominations, amount) {
if (denominations.length === 0) {
console.log('Please provide Denomniations to give the amount');
return false;
}
if (!amount) {
console.log('Amount cannot be 0');
return false;
}
return true;
}
const totalCoinsNeeded = getCoinDenominations([1, 5, 10], 7);
printOutput(totalCoinsNeeded);