JavaScript Algorithms and Data Structures Projects - Cash Register

I have satisfied the first 4 test cases, but the last two i have questions about how/what exactly they are testing for. The last test case is looking for:

{status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}

My return is: { status: ‘CLOSED’, change: [ [ ‘PENNY’, 0.5 ] ] }

I see the expected return is including all the empty values, HUNDRED, TWENTY etc. Is this why I am not satisfying this test case? This reason wouldn’t really make sense to me bc there is two similar passed test cases that had expected results that did not include the empty values:

{status: “OPEN”, change: [[“QUARTER”, 0.5]]}

{status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}

My second question is on the other test case which I am not satisfying.

checkCashRegister(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: []}

The change due is .50 (20 - 19.50) and the drawer total is 1.01 ( [“ONE”, 1] + [“PENNY”, 0.01] ). The expected return is “INSUFFICIENT_FUNDS”. There is enough money in this drawer to satisfy giving the customer change and still having an “OPEN” status. My code checks to see if the change to be returned is greater than the drawer total. If change is greater, the “INSUFFICIENT_FUNDS” status is set. It could never do that with this test case because there is enough money in the drawer to give the customer change. Am I missing something?

I understand no help will be given on final projects which i fully agree with. I am just looking for test case clarification.

Thanks

function checkCashRegister(purchPrice, payment, cid) {
  let drawer = cid.slice().reverse()
  let drawerTotal = 0
  let change = payment - purchPrice
  let key = [[100, 0], [20, 0], [10, 0], [5, 0], [1, 0], [.25, 0], [.1, 0], [.05, 0], [.01, 0]]
  let final = {status: "OPEN", change: []}

  //Calculates drawer total
  for(let i = 0; i < drawer.length; i++){
    drawerTotal += drawer[i][1]
  }
  //Status
  if(change > drawerTotal){
    return {status: "INSUFFICIENT_FUNDS", change: []}
  } 
  else if(change === drawerTotal){
    final.status = "CLOSED"
  }
  //Calculates key
  for(let i = 0; i < drawer.length; i++){
      key.push([key[i-i][0], drawer[i][1]]);
      key.shift()
  }
  //return
   for(let i = 0; i < key.length; i++){
    let total = 0

    while(change >= key[i][0] && drawer[i][1] !== 0){
      change = Math.round(change * 100) / 100
      total += key[i][0]
      change -= key[i][0]
      drawer[i][1] -= key[i][0]
      drawerTotal -= key[i][0]
      change = Math.round(change * 100) / 100
    }
    if(total != 0){
      final.change.push([drawer[i][0], Math.round(total * 100) / 100])
    }
  }
  return final
}

console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]))



Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Yes. The output must give all the return values in the examples.

There isn’t enough of the correct money. Since the cashier only has a single unbreakable dollar coin, it cannot be given to the customer whose change is less than that. (It would be fine if he had 5 dimes or 2 quarters or 50 pennies)

1 Like

Very much appreciated!!

So i switched my code around to include the empty values:

Expected result:
{status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}

My result:
{ status: ‘CLOSED’,change: [ [ ‘ONE HUNDRED’, 0 ],[ ‘TWENTY’, 0 ], [ ‘TEN’, 0 ],[ ‘FIVE’, 0 ],[ ‘ONE’, 0 ], [ ‘QUARTER’, 0 ], [ ‘DIME’, 0 ], [ ‘NICKEL’, 0 ], [ ‘PENNY’, 0.5 ] ] }

The only difference i can see is that the expected result is an ascending order and my result is in descending order. There is a different test case that has its expected output in descending order. Does the order matter? 100-.01 or .01-100 ?

Also, the two test cases I was passing when not returning the empty values I am now failing when returning empty values.

Expected Result:
{status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}

My Result:
{ status: ‘OPEN’, change: [ [ ‘ONE HUNDRED’, 0 ], [ ‘TWENTY’, 60 ],[ ‘TEN’, 20 ],[ ‘FIVE’, 15 ],[ ‘ONE’, 1 ],[ ‘QUARTER’, 0.5 ],[ ‘DIME’, 0.2 ],[ ‘NICKEL’, 0 ],[ ‘PENNY’, 0.04 ] ] }

Only difference I can see is ‘ONE HUNDRED’ and ‘NICKEL’ empty values are present in my result.

Expected Result:
{status: “OPEN”, change: [[“QUARTER”, 0.5]]}

My Result:
{ status: ‘OPEN’, change: [ [ ‘ONE HUNDRED’, 0 ],[ ‘TWENTY’, 0 ], [ ‘TEN’, 0 ],[ ‘FIVE’, 0 ], [ ‘ONE’, 0 ],[ ‘QUARTER’, 0.5 ],[ ‘DIME’, 0 ],[ ‘NICKEL’, 0 ],[ ‘PENNY’, 0 ] ] }

Only difference I can see is empty values are present in my result.

Any guidance is very much appreciated.

Thanks

Try to reread the instructions.

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, sorted in highest to lowest order, as the value of the change key.

You only give the change (in highest to lowest order) if the register is open.

But if status is closed then return the cash in the drawer instead

1 Like

Code passed and course finished. Thank you very much for your guidance throughout the Javascript course.

1 Like