I can't get the 3rd test to pass, the penny is not being counted when the change remaining is 4

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function checkCashRegister(price, cash, cid) {
const status = {
  1: "INSUFFICIENT_FUNDS",
  2: "CLOSED",
  3: "OPEN",
};

const denomination = {
  "ONE HUNDRED": 10000,
  "TWENTY": 2000,
  "TEN": 1000,
  "FIVE": 500,
  "ONE": 100,
  "QUARTER": 25,
  "DIME": 10,
  "NICKEL": 5,
  "PENNNY": 1,
};

let change = cash * 100 - price * 100;

let amountCid = cid
  .filter((f) => f[1] !== 0)
  .reduce((prev, curr) => {
    return prev + curr[1] * 100;
  }, 0);
const sortedCid = cid
  .map((obj) => [obj[0], obj[1] * 100])
  .reverse()
  // .sort((a, b) => {
  //   return a[1] === b[1] ? 0 : a[1] > b[1] ? -1 : 1;
  // });

if (change > amountCid) return { status: status[1], change: [] };
else if (change === amountCid) return { status: status[2], change: cid };
else {
  let sukli = []
  sortedCid.forEach((elem) => {
    let amount = 0;
    while(change >= denomination[elem[0]] && elem[1] > 0) {
      amount += denomination[elem[0]] / 100;
      change -= denomination[elem[0]];
      elem[1] -= denomination[elem[0]];

    }

    if (amount > 0)
      sukli.push([elem[0], amount])

* List item

});

  

  if (change > 0) return { status: status[1], change: [] };
  else return {status: status[3], change: sukli}
}
}

const result = 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]])

console.log(result);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36

Challenge: Cash Register

Link to the challenge:

Hi @kerubi !

I like the way you built the maps to access values, strong foundations in there :wink:

Even when we could go directly into the problem, it is important that yourself notice whats wrong.

Let’s see whats wrong with your code.

The test says:
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]]} .

But your attempt is returning { status: 'INSUFFICIENT_FUNDS', change: [] }

How can we figure out whats wrong?

Step 1. Find out what code block is malfunctioning.

The status INSUFFICIENT_FUNDS is returned in two cases. Lets expand those curly braces to check which branching is failing.

if (change > amountCid) {
  console.log('change > amountCid')
  return { status: status[1], change: [] }
}
  if (change > 0) {
     console.log('change > 0');
     return {status: status[1], change: []}
  }

We can see the following console output: change > 0. So, lets see how the change is being calculated.

So, we can say that there’s a problem with this code block. We can add a console.log to it, just to check if it is going through the while loop. Any problem so far?
1.

  sortedCid.forEach((elem) => {
    let amount = 0;
    console.log('Im inside the forEach loop!');

    while(change >= denomination[elem[0]] && elem[1] > 0) {
      amount += denomination[elem[0]] / 100;
      change -= denomination[elem[0]];
      elem[1] -= denomination[elem[0]];

    }

    if (amount > 0)
      sukli.push([elem[0], amount])
});

And it is inside the block! awesome!

Step 2. With the code block identified, print out everything!

Sometimes we just assume that everything should work, and in programming, if something is failing, it could be the dumbest thing. So, I encourage you to print out every variable that you think could be failing:

Maybe, this condition change >= denomination[elem[0]] && elem[1] > 0 itself does not tell you anything, but what if we debug every member of the prove, on every loop?

  sortedCid.forEach((elem, index) => {
    let amount = 0;
    // console.log(change >= denomination[elem[0]] && elem[1] > 0);
    console.log(`forEach loop ${index}`)
    console.log(change)
    console.log(elem[0])
    console.log(denomination[elem[0]])
    console.log(elem[1])
    console.log('------------------')
    while(change >= denomination[elem[0]] && elem[1] > 0) {

Check the console. Do you see any value unexpected ? First, do a quick check of the values before checking every value seriously. Maybe some nullish values ? Maybe accessing an undefined property?

If you still does’t find out, just reply and I will point out exactly whats wrong, but give this post a try!

1 Like

Hi Parasite,

Thank you so much for your detailed input. It gave me a very detailed and a good thinking on the process on how to debug errors in vanilla JS and I have just realized how stupid and silly my error was until I ran the final test, I saw that it was just an error in the naming of the variable because in the denomination

const denomination = {
  "ONE HUNDRED": 10000,
  "TWENTY": 2000,
  "TEN": 1000,
  "FIVE": 500,
  "ONE": 100,
  "QUARTER": 25,
  "DIME": 10,
  "NICKEL": 5,
  "PENNNY": 1,
};

While the CID that is being passed on that I filtered has the right name which is “PENNY” with two N’s hence when the change reaches 4 which is only substractable by pennies it does not enter the while loop because here:

while(change >= denomination[elem[0]] && elem[1] > 0) 

denomination[elem[0]]  returns undefined

The denomination returns undefined because elem[0] is “PENNY” while in my denomination its “PENNNY”

Thank you so much for this insightful answer.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.