Tell us what’s happening:
Everything seems to work ok but I am stuck with the if statement for the first condition
{ status: “INSUFFICIENT_FUNDS”, change: [ ] } if cash-in-drawer is less than the change due, or if you cannot return the exact change.
Sometimes Test 1 passes but Test 2 doesn’t and vice versa. Other times either Test 1 or Test 2 passes but not without some other Test failing alongside with it.
Your code so far
function checkCashRegister(price, cash, cid) {
var change = cash - price;
var changeSplit = String(change).split(".");
var billsToGiveBack = Number(changeSplit[0]);
console.log(billsToGiveBack);
var coinsToGiveBack = Number(`${'.'}${changeSplit[1]}`);
console.log(coinsToGiveBack);
let cashInDrawer = 0;
for (let i = 0; i < cid.length; i++) {
cashInDrawer += cid[i][1]
}
if (change === cashInDrawer) {
return {
status: "CLOSED",
change: [...cid]
}
}
if(change !== cashInDrawer) {
return {
status: "INSUFFICIENT_FUNDS",
change: []
}
}
if(cashInDrawer > change) {
// seprating Bills And Coins to return customer
let cidCoins = [];
for (let i = 0; i < 4; i++) {
cidCoins.push(cid[i])
}
let cidBills = [];
for (let i = 4; i <= 8; i++) {
cidBills.push(cid[i])
}
// bills and coins to return customer
let dollarsToGiveBack = [];
let sumUp = 0;
let hundredDollars = [cidBills[4]];
console.log(hundredDollars);
let noOfHundredDollars = [];
for (let i = 100; i <= hundredDollars[0][1]; i += 100) {
noOfHundredDollars.push(100);
}
if (hundredDollars[0][1] < billsToGiveBack) {
let sum = 0;
for (let i = 0; i <= noOfHundredDollars.length; i++) {
if (sumUp < billsToGiveBack) {
sumUp += noOfHundredDollars[i];
sum += noOfHundredDollars[i];
} else if (sumUp > billsToGiveBack) {
sumUp -= noOfHundredDollars[i];
sum -= noOfHundredDollars[i];
}
}
dollarsToGiveBack.push(["TWENTY", sum]);
}
let twentyDollars = [cidBills[3]];
console.log(twentyDollars)
let noOfTwentyDollars = [];
for (let i = 20; i <= twentyDollars[0][1]; i += 20) {
noOfTwentyDollars.push(20);
}
if (sumUp < billsToGiveBack) {
let sum = 0;
for (let i = 0; i < noOfTwentyDollars.length; i++) {
if (sumUp < billsToGiveBack) {
sumUp += noOfTwentyDollars[i];
sum += noOfTwentyDollars[i];
}
}
if (sumUp === billsToGiveBack && sum !== 0) {
dollarsToGiveBack.push(["TWENTY", sum]);
} else {
while (sumUp > billsToGiveBack) {
sumUp -= 20;
sum -= 20;
}
if (sumUp < billsToGiveBack && sum !== 0) {
dollarsToGiveBack.push(["TWENTY", sum]);
}
}
}
let tenDollars = [cidBills[2]];
console.log(tenDollars);
let noOfTenDollars = [];
for (let i = 10; i <= tenDollars[0][1]; i += 10) {
noOfTenDollars.push(10);
}
if (sumUp < billsToGiveBack) {
let sum = 0;
for (i = 0; i < noOfTenDollars.length; i++) {
if (sumUp < billsToGiveBack) {
sumUp += noOfTenDollars[i];
sum += noOfTenDollars[i]
}
}
if (sumUp === billsToGiveBack && sum !== 0) {
dollarsToGiveBack.push(["TEN", sum]);
} else {
while (sumUp > billsToGiveBack) {
sumUp -= 10;
sum -= 10;
}
if (sumUp < billsToGiveBack && sum !== 0) {
dollarsToGiveBack.push(["TEN", sum]);
}
}
}
let fiveDollars = [cidBills[1]];
console.log(fiveDollars);
let noOfFiveDollars = [];
for (let i = 5; i <= fiveDollars[0][1]; i += 5) {
noOfFiveDollars.push(5);
}
if (sumUp < billsToGiveBack) {
let sum = 0;
for (i = 0; i < noOfFiveDollars.length; i++) {
if (sumUp < billsToGiveBack) {
sumUp += noOfFiveDollars[i];
sum += noOfFiveDollars[i];
}
}
if (sumUp === billsToGiveBack && sum !== 0) {
dollarsToGiveBack.push(["FIVE", sum]);
} else {
while (sumUp > billsToGiveBack) {
sumUp -= 5;
sum -= 5;
}
if (sumUp < billsToGiveBack && sum !== 0) {
dollarsToGiveBack.push(["FIVE", sum]);
}
}
}
let oneDollars = [cidBills[0]];
console.log(oneDollars);
let noOfOneDollars = [];
for (let i = 1; i <= oneDollars[0][1]; i += 1) {
noOfOneDollars.push(1);
}
if (sumUp < billsToGiveBack) {
let sum = 0;
for (i = 0; i < noOfOneDollars.length; i++) {
if (sumUp < billsToGiveBack) {
sumUp += noOfOneDollars[i];
sum += noOfOneDollars[i];
}
}
if (sumUp === billsToGiveBack && sum !== 0) {
dollarsToGiveBack.push(["ONE", sum]);
}
else {
while (sumUp > billsToGiveBack) {
sumUp -= 1;
sum -= 1;
}
if (sumUp <= billsToGiveBack && sum !== 0) {
dollarsToGiveBack.push(["ONE", sum]);
}
}
}
let coinsToGiveBackCustomer = [];
let sumUp2 = Number(Number(0).toFixed(1));
let quarters = String([cidCoins[3][1]]);
console.log(quarters);
let quartersSplit = quarters.split(".");
console.log(Number(quartersSplit[0]));
if (sumUp2 < coinsToGiveBack) {
let sum1 = 0;
for (let i = 0; i <= Number(quartersSplit[0]); i++) {
if (sumUp2 < coinsToGiveBack) {
sumUp2 += 0.1
sum1 += 0.1;
}
}
if (Number(Number(sumUp2).toFixed(2)) === coinsToGiveBack) {
coinsToGiveBackCustomer.push(["QUARTER", sum1]);
} else {
while (sumUp2 > coinsToGiveBack) {
sumUp2 -= 0.1;
sum1 -= 0.1
}
if (Number(Number(sumUp2).toFixed(2)) < coinsToGiveBack && sum1 !== 0) {
coinsToGiveBackCustomer.push(["QUARTER", Number(Number(sum1).toFixed(1))]);
}
}
}
let dimes = String([cidCoins[2][1]]);
console.log(dimes);
let dimesSplit = dimes.split(".");
console.log(Number(dimesSplit[0]));
if (sumUp2 < coinsToGiveBack) {
let sum2 = 0;
for (let i = 0; i <= Number(dimesSplit[0]); i++) {
if (sumUp2 < coinsToGiveBack) {
sumUp2 += 0.1;
sum2 += 0.1;
}
}
if (Number(Number(sumUp2).toFixed(2)) === coinsToGiveBack) {
coinsToGiveBackCustomer.push(["DIME", sum2]);
} else {
while (sumUp2 > coinsToGiveBack) {
sumUp2 -= 0.1;
sum2 -= 0.1;
}
if (Number(Number(sumUp2).toFixed(2)) < coinsToGiveBack && sum2 !== 0) {
coinsToGiveBackCustomer.push(["DIME", Number(Number(sum2).toFixed(1))]);
}
}
}
let nickel = String([cidCoins[1][1]]);
console.log(nickel);
let nickelSplit = nickel.split(".");
console.log(Number(nickelSplit[0]));
if (sumUp2 < coinsToGiveBack) {
let sum3 = 0;
for (let i = 0; i <= Number(nickelSplit[0]); i++) {
if (sumUp2 < coinsToGiveBack) {
sumUp2 += 0.05
sum3 += 0.05;
}
}
if (Number(Number(sumUp2).toFixed(2)) === coinsToGiveBack) {
coinsToGiveBackCustomer.push(["NICKEL", sum3]);
} else {
while (sumUp2 > coinsToGiveBack) {
sumUp2 -= 0.05;
sum3 -= 0.05;
}
if (Number(Number(sumUp2).toFixed(2)) < coinsToGiveBack && sum3 !== 0) {
coinsToGiveBackCustomer.push(["NICKEL", sum3]);
}
}
}
let penny = String([cidCoins[0][1]]);
console.log(penny);
let pennySplit = penny.split(".");
console.log(Number(pennySplit[0]));
if (sumUp2 < coinsToGiveBack) {
let sum4 = 0;
for (let i = 0; i <= 3; i++) {
if (sumUp2 < coinsToGiveBack) {
sumUp2 += 0.01;
sum4 += 0.01;
}
}
if (Number(Number(sumUp2).toFixed(2)) === coinsToGiveBack) {
coinsToGiveBackCustomer.push(["PENNY", sum4]);
}
else {
while (sumUp2 > coinsToGiveBack) {
sumUp2 -= 0.01;
sum4 -= 0.01;
}
if (Number(Number(sumUp2).toFixed(2)) === coinsToGiveBack && sum4 !== 0) {
coinsToGiveBackCustomer.push(["PENNY", sum4]);
}
}
}
if (billsToGiveBack === 0) {
return {
status: "OPEN",
change: [...coinsToGiveBackCustomer]
}
} else {
return {
status: "OPEN",
change: [...dollarsToGiveBack, ...coinsToGiveBackCustomer]
}
}
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: