This works. I was just wondering if anyone had any thoughts on if it’s good or if it could have been done better.
function checkCashRegister(price, cash, cid) {
let diff = Number((cash-price).toFixed(2));
let sumAll = 0;
cid.forEach(e => sumAll += e[1]);
if (diff > sumAll)
return {status: 'INSUFFICIENT_FUNDS', change: []};
const currencyHash = {'ONE HUNDRED': 100, 'TWENTY': 20, 'TEN': 10, 'FIVE': 5, 'ONE': 1, 'QUARTER': .25, 'DIME': .1, 'NICKEL': .05, 'PENNY': .01};
let changeArr = [];
let sumChange = 0;
cid.slice().reverse().forEach(e => {
let currencyType = e[0];
let availableCurrency = e[1];
while (currencyHash[e[0]] <= Number((diff-sumChange).toFixed(2))
&& availableCurrency > 0 ) {
sumChange = Number((currencyHash[currencyType]+sumChange).toFixed(2));
availableCurrency = Number((availableCurrency-currencyHash[currencyType]).toFixed(2));
if (!changeArr.length || changeArr[changeArr.length-1][0] != currencyType) {
changeArr.push([currencyType, currencyHash[currencyType]]);
}
else
{
changeArr[changeArr.length-1][1] =
Number((changeArr[changeArr.length-1][1]+currencyHash[currencyType]).toFixed(2));
}
}
});
if (diff != sumChange)
{
return {status: "INSUFFICIENT_FUNDS", change: []};
}
else if (diff == sumAll){
return {status: "CLOSED", change: cid};
}
else {
return {status: "OPEN", change: changeArr};
}
}
Looks good to me nice work
It kind of bothers me that I do all the work of building changeArr and it doesn’t get used in every case.
Hi, guys!
Here is my own version of the ‘Cash Register’ challenge. I’d like to read you thought about my code. I also tried to comment everything I’ve done in order to understand better what I did.))
So, be free in critique, and thanks. 
// 1. Create an objetc with denominations to compare:
let denom = {
'PENNY': 0.01,
'NICKEL': 0.05,
'DIME': 0.1,
'QUARTER': 0.25,
'ONE' : 1,
'FIVE': 5,
'TEN': 10,
'TWENTY': 20,
'ONE HUNDRED': 100,
}
// 2. Reverse CID array in order to compare and calculate values from bigger to lesser:
cid.reverse();
// 3. Declare some Global variables:
let changeTotal = cash - price;// total change
let changeRest = changeTotal;// rest of the total change after calculation inside a for loop
let totalCID = 0;// total CID
let restCID = 0;// CID remained after calculation in for loop
let changeArr = [];
// 4. Iterate trought the CID array
for(let [cashName,cashVal] of cid){
// * accumulate cashVal according to the 'denomination';
let acc = 0;
// * create total CID
totalCID += cashVal;
// * defines the range of 'denomination' we start the calculation:
if( changeRest >= denom[ cashName ] ){
// * iteration is made in base if:
// - total change is bigger than 'denomination' item;
// - and value of CID 'denomination' item is bigger than 0;
while( changeRest >= denom[ cashName ] && cashVal){
// * Caluclation and rounding the values;
// * .toFixed(2) on each calculation means - two digits after the point;
// * as .toFixed(2) transform number into the string the '+' sign convert it into the number:
cashVal = +( cashVal - denom[ cashName ]).toFixed(2);
changeRest = +( changeRest - denom[ cashName ]).toFixed(2);
acc = +( acc + denom[ cashName ] ).toFixed(2);
}
changeArr.push( [cashName,acc] );
}
}
// 5. Declare restCID to compare with in the for loop
restCID = +( totalCID - changeTotal).toFixed(2);
//console.log(`total CID : ${totalCID} | rest CID : ${restCID} | change total : ${changeTotal} | change rest : ${changeRest}`);
// Defines the return object
if(totalCID === changeTotal && restCID < 1){
return {status: "CLOSED", change: cid.reverse()};
}else if(changeRest > 0){
changeRest = changeTotal;
return {status: "INSUFFICIENT_FUNDS", change : []};
}
return {status: "OPEN", change: changeArr};
checkCashRegister(19.5, 20, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.1], [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], [“TWENTY”, 60], [“ONE HUNDRED”, 100]]);
good job commenting. that’s what I should have done.