Cash register help plzz

Hi guys I’m unable to notice where is going wrong as im failing last 2 conditions. i wish you guys can lend me ur eyes. much appreciated. link to challenge : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

const money = [
["ONE HUNDRED",10000],
["TWENTY",2000],
["TEN",1000],
["FIVE",500],
["ONE",100],
["QUARTER",25],
["DIME",10],
["NICKEL",5],
["PENNY",1]
];

function iHaveChange (moneyValue,totalCashValue,change) { 
    return totalCashValue >= moneyValue && change - moneyValue >= 0;
}


function cidToObject(cid) { 

const myCash = {};

cid.forEach(money=> myCash[money[0]] = money[1]* 100)

return myCash;

}

function iHaveNoBills(myCash) {
let iHaveChange = false;

Object.keys(myCash).forEach(function(key){
if (myCash[key] > 0) { 
    iHaveChange = true;
}
})
return !iHaveChange;
}


function checkCashRegister(price, cash, cid) {
 
let change = cash*100 - price*100;


let myCash = cidToObject(cid);


let clientCash = {};

let i = 0;

while (i < money.length && change > 0) { 
  let moneyName = money[i][0];
  let moneyValue = money[i][1]; 

  if (iHaveChange(moneyValue,myCash[moneyName],change)) { 
clientCash[moneyName] = 0;

while(iHaveChange(moneyValue,myCash[moneyName],change) ) { 
    clientCash += moneyValue/100;
    myCash[moneyName] = parseInt(myCash[moneyName] - moneyValue);
   change-=moneyValue;
   
}

  }
  
  i++
}
console.log(clientCash)

if (change === 0 ) {

 if (iHaveNoBills(myCash) ) {
return { 
  status:"CLOSED",
  change:cid
}

 }

return  {
    status:"OPEN",
    change:Object.keys(clientCash).map(function(key){
let arr = [key,clientCash[key]];
return arr
    })

}

}

else { 
return {status:"INSUFFICIENT_FUNDS",change:[]}

}




}






checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);

This is your problem. Use debugging with console.log and chrome debugger more actively. There are a lot of guides out there that teach you how to use the power of these tools :stuck_out_tongue:

how did i not spot that error… oh wait i’ve been up for too long 24 hours

now there might be only 1 more thing i’m unsure of in this code (this isn’t my code, i couldnt do this challenge myself so i had to examine other people’s ) ---->

function iHaveChange (moneyValue,totalCashValue,change) { 
    return totalCashValue >= moneyValue && change - moneyValue >= 0;
}

now i understand “return totalCashValue >= moneyValue”. it says if the cash register is more than or equal to a penny/nickel or any other currency, then it can continue deducting from the register.

for the second piece of code “change - moneyValue >= 0” not so much… to me it doesn’t make sense. why is it “>=0” ? shouldn’t it be just “>0” because if the change is more than 0, then there’s still change i owe the customer but “>=0” leaves me so confused.

i hope you get what i’m trying to say. a reply would be appreciated.

It’s not about checking if you still owe the customer. It’s about checking if you can give the change to the customer using a certain denomination. If you owe 20$ to the customer and you have a 20$ bill in the cash register then you CAN give 20$ to the customer. In that situation 20-20 will be equal to zero and this condition will be true.

As a result of this condition being true, the loop will execute and add the denomination and the amount to the object that gets returned as a result of your function. If you had > 0 instead of >= 0 you’d never be able to give out the exact change and finish the last step of your loop, the step when giving your customer a certain amount of money will mean reaching the exact change owed.

The first part of that comparison

totalCashValue >= moneyValue

is not about

it says if the cash register is more than or equal to a penny/nickel or any other currency, then it can continue deducting from the register.

It’s about the same thing the second part of the comparison is about. They are not separate, they’re building the same question together. That question is - CAN i deduct
x amount of money from the register right now, using this denomination and if i can do it i want to make sure i’m not giving the customer more money than i owe him in doing so.

so the first part

totalCashValue >= moneyValue

isn’t that the same thing though? since “>” means eg. our $0.75 worth of quarters can give out $0.25 ( 1 quarter ) since $0.75 is more than $0.25. AND “=” means IF we ONLY have $0.25 worth of quarters ( 1 quarter ), and if we only owe 1 quarter ($0.25 ). then $0.25 - $0.25. so totalCashValue >= moneyValue makes sense.

and after reading ur explanation… the second part

change - moneyValue >= 0

it has to be “>=” because “>” means we don’t pay the exact total change every loop and “=” means eventually we’re have to finish looping by returning the total change required?

Replace your condition with this one. Maybe it will help you understand this better. :

totalCashValue > 0 && change - moneyValue >= 0

First part has nothing to do with what we owe. It makes sure that the cash register has enough money to pay out in a certain denomination. That’s it. Has nothing to do with change/what we owe. You can use this particular condition if you know that your cash register object is not buggy and all the values inside it are legit multiples of certain denominations. In this situation it’s going to be enough that a certain value is > 0.

For example, you know that if the value of ‘quarter’ key in your cash register is more than 0 you can be sure that it has at least one quarter in it ( the value is at least 25).

Whichever comparison you are using, this algorithm that we are using won’t work in some situations, but this is outside of the scope of this discussion and doesn’t really matter all that much since the tests in this challenge don’t cover these cases.

okay so the first part just means having enough money to pay out in every different type of currency, starting from the lowest “penny” to highest “hundred” since every type of currency ( or denomination as u say ) is more than 0.

what about the second part? did i understand that correctly as i commented in my previous comment?

Let’s try a simple one. What happens if you need to give 50$ as change and only have one 50$ bill in your cash register? Ask yourself that and you’ll understand why it has to be >= instead of >.

are you testing me for the second part? if so “>=” is required because the “=” ensures that the we have to deduct exactly $50 from our $50 so that there’s 0 change required.

meaning we paid fully for the change. as for the “>” that is, same scenario if we owe $50 to the customer but we only pay like $20 for the first loop, then there’s $30 left which is MORE THAN 0 which explains the “>” required.

then we continue looping second time paying $20 with the “>” required since $10 left of change is more than 0. then loop the 3rd time ( last time ) paying the $10 change required with a $10 dollar note. now the change is 0 which explains the “=” required because change - moneyValue is equal to zero now.

right? or am i wrong? because if the change is equal to 0 then doesn’t the condition return true and the loop continues looping?

In your example the body of the loop never runs if you put 10 > 10 instead of 10 >= 10. That means the change never becomes 0. We don’t modify change inside iHaveChange function, we do it inside the body of the loop. And since the body of the loop never runs we don’t add the last 10 to the clientCash, we don’t subtract the last 10 from myCash, and we don’t get the correct answer as a result.

Now, IF you put 10 >= 10 what happens is you get one more execution of the loop, where change becomes 0 and all the things I’ve described in the last paragraph happen. Then, you call iHaveChange again, this time with change being 0. and since ‘0 - moneyValue’ will always be less than zero the function returns false and the execution of the while loop stops.

you keep saying 10>10 doesn’t make sense and we need >= because we need to deduct all the change. but why GREATER THAN symbol > with “=”? “>” for 10>2? because in the while loop im deducting lower currencies.

so i just did some console logging with “>” without the “=” and it there was nothing was printed in the object. which i guess the loop didn’t even run once. i thought moneyValue means like value of each currency.

like for eg. to pay the full $70 of change to the customer, i have to do it like this:
first loop: pay $20 ( moneyValue ) <------ $70 - $20 = $50, $50 is more than 0 demonstrating the “>”
second loop pay $20 ( moneyValue ) <— $50 - $20 = $30
3rd loop pay $20 ( moneyValue ) <---- $30 - $20 = $10
4th loop/last loop pay $10 ( moneyValue ) $10 - $10 = 0 demonstrating the “=”
hence “>=” ? but then again like i said, i did some console logging and not even a single loop was done because there was nothing in the object that i checked in the console.

but it seems to me that you keep saying change - moneyValue has to be “>=”,

Learn to debug and you can see for yourself which variables are equal to what at different stages of execution and what happens when you change things around. I give up, sorry, i tried explaining the best i could.

I would especially pay attention to watch list, step by step execution and breakpoints. Debugging using console.log can be tedious and printing all the intermediate states of objects inside each iteration of the loop can take up a lot of space. Plus for more complicated projects, which you’ll eventually start building, console.log debugging can be flat out impossible. Good luck!

1 Like

can u go at it just 1 more time?

change - moneyValue >= 0

what is moneyValue EXACTLY? and you keep explaining why “=” has to be there. but u don’t explain why “>” has to be present with it too. give it 1 more guy for me…:sleepy: I KNOW i can understand this

So, moneyValue isthe current bill/coin considered 's value, so if you are looking at quartes, it is equal to 0.25$ (line that say let moneyValue = money[i][1];)

And change is how much you need to give to the client

if you need to give 0.5 and you are using quarters
first: 0.5 - 0.25 >= 0 is true, then that is subtracted from the change due
second: 0.25 - 0.25 >= 0 is true, and a quarter is substracted from the change due

if you don’t have the > you don’t go anywhere (if the change due is not equal to any denomination, you can’t give change at all, as you would have check 0.5 - 0.25 === 0)
if you don’t have = you can’t give last coin (0.25 - 0.25 > 0 is false and the last coin is not given)

thanks your explanation was so much better than the other guys