Can someone help me with this challenge.It is by far the most difficult one I have came across.
I have passed all the challenges but two
***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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.
***
***This text will be hiddencheckCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.
***
I really need some help here . This is by far the most difficult thing I have ever experienced in coding.
This is my code so far:
const statusDictionary = {
closed : "CLOSED",
insufficent : "INSUFFICIENT_FUNDS",
open : "OPEN",
};
function checkCashRegister(price, cash, cid) {
var output = {status : "", change : cid};
var change = cash - price;
var cashAvailable = cashAv(cid);
if (cashAvailable < change){
output.status = statusDictionary.insufficent;
output.change = [];
}
else if (cashAvailable == change){
output.status = statusDictionary.closed;
output.change = [...cid];
}
else if (cashAvailable > change){
output.status = statusDictionary.open;
const denominations = [
{"name":"PENNY","value":0.01},
{"name":"NICKEL","value":0.05},
{"name":"DIME","value":0.10},
{"name":"QUARTER","value":0.25},
{"name":"ONE","value":1},
{"name":"FIVE","value":5},
{"name":"TEN","value":10},
{"name":"TWENTY","value":20},
{"name":"HUNDRED","value":100}]
var answer = [];
var tracker = 0;
for(let i = cid.length - 1; i >= 0; i--){
while(change >= denominations[i].value && tracker < cid[i][1]){
change -= denominations[i].value
tracker += denominations[i].value
} if(tracker > 0){
answer.push([denominations[i].name, tracker]);
} tracker = 0;
output.change = answer;
}
}
return output;
};
function cashAv (cid) {
let available = 0;
for (let i = 0; i < cid.length; i++){
available += cid[i][1]
};
return available.toFixed(2);
};
console.log(JSON.stringify(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]])));
Someone please look at my code and guide me what is wrong in it.
Here is the link to the challenge: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/