In an attempt to up my JS
game, I want to get away from for loops
and use filter()
, map()
, reduce()
instead.
To that end, as a first step in my code, I want to add up all the values in the given array, cid
. I am using reduce()
to add all those values up to get the total value of cash in drawer. But am getting unexpected results. Do I need to use map()
to pull out all values from cid
first THEN reduce()
?
Can anyone help please?
Thank you.
Your code so far
function checkCashRegister(price, cash, cid) {
//Calc total of cash in register.
let totalCashRegister = cid.reduce((arr, sum) => {
return sum + arr[1];
})
console.log(totalCashRegister);
//Calc change due = cash - price
//If change due > cash in register, return {status: "INSUFFICIENT_FUNDS", change: []}
//If change due < cash in register,
//and can return exact change
//and there is still change left in drawer: return {status: "OPEN", change: [...]} with the change due in coins and bills, sorted in highest to lowest
//but there is no more change left in drawer: return {status: "CLOSED", change: [...]} with cash-in-drawer as the value
//but cannot return exact change, return {status: "INSUFFICIENT_FUNDS", change: []}
}
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register