Tell us what’s happening:
I’m able to get the it almost working but i need to reduce the object arr to have only the highest cash value for each dollar amount listed. This is what it gives now:
TWENTY,2000,TWENTY,4000,TWENTY,6000,TEN,7000,TEN,8000,FIVE,8500,FIVE,9000,FIVE,9500,ONE,9600,QUARTER,9625,QUARTER,9650,DIME,9660,DIME,9670,PENNY,9671,PENNY,9672,PENNY,9673,PENNY,9674
i just want TWENT,6000,TEN,8000, etc, please help!
Your code so far
var cashValue = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1];
var cashName = ['ONE HUNDRED', 'TWENTY', 'TEN', 'FIVE', 'ONE', 'QUARTER', 'DIME', 'NICKEL', 'PENNY'];
function checkCashRegister(price, cash, cid) {
let tempPrice = price*100;
let tempCash = cash*100;
var tempArr = cid.map(x => {return x[1]*100;});
var change = (cash*100 - price*100), total, arr = cid.map(x => [x[0], x[1]]);
var output = { status: "OPEN", change: [] };
var sum = 0;
total = cid.reduce((tot, inc) => {
return tot+(inc[1]*100);},0)
tempArr = tempArr.reverse();
if(total < change){
console.log('first if');
return {status: "INSUFFICIENT_FUNDS", change: []};
} else if(total === change){
console.log('we made it');
return {status: "CLOSED", change: cid};
} else if (total > change){
for (let i = 0; i < tempArr.length; i++){
while (change >= cashValue[i] && tempArr[i] > 0){
change -= cashValue[i];
tempArr[i] -= cashValue[i];
sum += cashValue[i];
output.change.push([cashName[i], sum]);
}
}
console.log('this is the chang e ' + change + ' and this is the total ' + total)
if (change > 0){
console.log('we in here');
return {status: "INSUFFICIENT_FUNDS", change: []};
}
//this is the code i'd like to replace with a reduce method
else{
console.log(output.change);
for(let i = 0; i < output.change.length;i++){
if (output.change[i][0] == output.change[i+1][0]){
delete output.change[i];
i++;
}
}
console.log(output.change);
return {status: "OPEN", change:[]};
}
}
// Here is your change, ma'am.
return change;
}
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/