** I passed some tests and some not, The cases that I passed are: Return {status: “INSUFFICIENT_FUNDS”, change: } if cash-in-drawer is less than the change due, and Return {status: “CLOSED”, change: […]} with cash-in-drawer as the value for the key change if it is equal to the change due.
But
when I tried to return {status: “OPEN”, change: […]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.
It’s wrong to push the whole [“TWENTY”, cidLookup.TWENTY] number into returnedObject.change array but I don’t know what I should do here??
I’m stuck with this case and I don’t know is I should rewrite the whole function that takes the change, and cid as input and changes the returnedObject value or not.
**
Your code so far
// Solution
// get the change amount and save it in change variable (change = cash - price)
// here we have different cases to calculate the change from cid
// if change > 20 subtract 20 from the change and add+ what subtracted to the returned object
// it must be a recursive to make the process automatically
// if change > 10 subtract 10 from the change and add+ what subtracted to the returned object
// and so on change > 0.25, 0.1, 0.05, and 0.01
// if we have 0 difference at the end we can make it and return what added to the object
// if it is 0 so it'll be the open state
// from the beginng Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due,
// or if you cannot return the exact change.
// Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change
// if it is equal to the change due.
// Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills,
// to make it easy create a lookup object the holds the penny, nickel, dime, one, five, ten, Twenty, 100
function checkCashRegister(price, cash, cid) {
var change = cash - price;
// cash in drawer lookup
// cidLookup1 variable refactored below into cidLookup
// let cidLookup1 = {
// penny: cid[0][1],
// nickel: cid[1][1],
// dime: cid[2][1],
// quarter: cid[3][1],
// one: cid[4][1],
// five: cid[5][1],
// ten: cid[6][1],
// twenty: cid[7][1],
// oneHundred: cid[8][1],
// };
// cidLookup1 refactoring
let cidLookup = cid.reduce((lookup, x) => {
lookup[x[0]] = x[1];
return lookup;
}, {});
// console.log(cidLookup); // { PENNY: 1.01, NICKEL: 2.05, ..., 'ONE HUNDRED': 100 }
// total cash in drawer
const totalCashInDrawer = cid.reduce((sum, x) => sum + x[1], 0);
// The object that will be returned
let returnedObject = { status: "", change: [] };
// a function that takes change, and cid as input and changes the returnedObject value.
// must return open with the change array or INSUFFICIENT_FUNDS
// logical error
// let getReturnedObjectValue = (change, cidLookup) => {
// change > 20 && cidLookup.TWENTY > 20
// ? returnedObject.change.push(["TWENTY", cidLookup.TWENTY]) &&
// (change = change - cidLookup.TWENTY)
// : 2;
// return returnedObject;
// };
// logical error
let getReturnedObjectValue = (change, cidLookup) => {
change > 20 && cidLookup.TWENTY > 20
? returnedObject.change.push(["TWENTY", cidLookup.TWENTY]) &&
getReturnedObjectValue(
change - cidLookup.TWENTY,
cidLookup.TWENTY - cidLookup.TWENTY
)
: change > 10 && cidLookup.TEN > 10
? returnedObject.change.push(["TEN", cidLookup.TEN]) &&
getReturnedObjectValue(
change - cidLookup.TEN,
cidLookup.TEN - cidLookup.TEN
)
: change > 5 && cidLookup.FIVE > 5
? returnedObject.change.push(["FIVE", cidLookup.FIVE]) &&
getReturnedObjectValue(
change - cidLookup.FIVE,
cidLookup.FIVE - cidLookup.FIVE
)
: change > 1 && cidLookup.FIVE > 1
? returnedObject.change.push(["ONE", cidLookup.ONE]) &&
getReturnedObjectValue(
change - cidLookup.ONE,
cidLookup.ONE - cidLookup.ONE
)
: change > 0.25 && cidLookup.QUARTER > 0.25
? returnedObject.change.push(["QUARTER", cidLookup.QUARTER]) &&
getReturnedObjectValue(
change - cidLookup.QUARTER,
cidLookup.QUARTER - cidLookup.QUARTER
)
: change > 0.1 && cidLookup.DIME > 0.1
? returnedObject.change.push(["DIME", cidLookup.DIME]) &&
getReturnedObjectValue(
change - cidLookup.DIME,
cidLookup.DIME - cidLookup.DIME
)
: change > 0.05 && cidLookup.NICKEL > 0.05
? returnedObject.change.push(["NICKEL", cidLookup.NICKEL]) &&
getReturnedObjectValue(
change - cidLookup.NICKEL,
cidLookup.NICKEL - cidLookup.NICKEL
)
: change > 0.01 && cidLookup.PENNY > 0.01
? returnedObject.change.push(["PENNY", cidLookup.PENNY]) &&
getReturnedObjectValue(
change - cidLookup.PENNY,
cidLookup.PENNY - cidLookup.PENNY
)
: 1;
// if must be written to check if the change is >0 || ? then return open or INSUFFICIENT_FUNDS
return cidLookup;
};
totalCashInDrawer < change
? (returnedObject.status = "INSUFFICIENT_FUNDS") // or if you cannot return the exact change.
: totalCashInDrawer == change
? (returnedObject.status = "CLOSED") && (returnedObject.change = cid)
: getReturnedObjectValue(change, cidLookup); // must return open with the change array or INSUFFICIENT_FUNDS
return returnedObject;
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: