checkCashRegister won´t pass some tests

I´m trying to finish the checkCashRegister certification challenge of JS algorithms, but I´m having troubles passing the tests. It seems the object returned is not recognized at all.

However, when I run the script on the console, the returned values seems to be fine… but it´s a different story when running from the freecodecamp console…
The only tests passing are the ones of INSUFFICIENT_FUNDS.

I debugged on chrome and consoled.log all the tests that returns the correct objects, for example:

Could it be something related to units / float problems?
Could you please enlighten me?

Link to challenge:


  
function checkCashRegister(price, cash, cid) {
  let change = cash-price;
  let output = { status: null, change: [] };

  let currencies = [
    ["PENNY", .01],
    ["NICKEL", .05],
    ["DIME", .1],
    ["QUARTER", .25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20],
    ["ONE HUNDRED", 100]
  ];  

  function findCash(change, cid, coins = []) {
    if (change === 0) {
      // base case -> change = 0
      output.status = "OPEN";
      output.change = coins;
      return (output);
    } else if(checkTotal(cid) === 0 && change > 0) {
      output.status = "INSUFFICIENT_FUNDS";
      return (output);  // no cash in CID
    } else {
      // recursive call
      let highestUnit = currencies.reduce((highestNum, cur, index) => {

        if (Number(((change % cur[1]).toFixed(2)) < change) && cid[index][1] > 0) {  // if remainder is less than change & there is the specific money in CID

          let partialChange = parseInt(change / cur[1]).toFixed(2) * cur[1]; // 40
          highestNum = [cur[0], partialChange, index]; // overwrites previous values the last resulting the biggest one 
        }
        return highestNum;
      }, []);
      // it returns an ideal value , then I calculate if there is change possible in CID

 
      typeof highestUnit;

      if(highestUnit.length === 0) {
        output.status = "INSUFFICIENT_FUNDS";
        return (output);  
      }  

      if (cid[highestUnit[2]][1] >= highestUnit[1]) {  
        remainAmt = change % highestUnit[1];
        change = Number(remainAmt.toFixed(2));

        coins.push([highestUnit[0], highestUnit[1]]);
        cid[highestUnit[2]][1] -= highestUnit[1];
      } else {    // in CID there is less 
        // give everything there is 
        change -= cid[highestUnit[2]][1];   
        change = Number(change.toFixed(2));
        coins.push([highestUnit[0], cid[highestUnit[2]][1]]);  

        cid[highestUnit[2]][1] = 0;
      }

      return findCash(change, cid, coins); 
    }
}

Start by console.log() whatever you are going to return and compare it to the expected result - so you get a picture of what the difference is.
Also please add a link to the project.

Without knowing the actual error, debugging your code is quite the massive task.

Hey Jagaya! thanks for your answer. Sorry about that. I´ve edited the post with more useful information. It´s difficult for me to point to a specific part of the code in which the error could be found… I´ve debugged it and consoled the results and variables… everything is working well but not in the freecodecamp console.

You have a missing } somewhere.

This can’t possibly be working as written, because you only call findCash inside of itself.

I plicked your code into the project editor and it doesn’t run at all.
I can’t call the function because it complaints that “coins” is not defined and if I just add that, “checkTotal” is not defined - plus the missing call of the inner-function as JeremyLT pointed out…

If your code editor isn’t showing these, there seems something wrong either with the code you presented here or with the way you call it.

1 Like

Thank you all!
First of all, I´m truly sorry about the code shared, it lacked the last part of it and I didn´t realise…
I found the errors and all the tests passed!
I had some undeclared variables like remainAmt that passed well in chrome debugging tests (don´t know why! ) .
Then I added the “CLOSED” case, that was missing…
This is the final code :

function checkCashRegister(price, cash, cid) {
  let change = cash-price;
  let output = { status: null, change: [] };

  let currencies = [
    ["PENNY", .01],
    ["NICKEL", .05],
    ["DIME", .1],
    ["QUARTER", .25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20],
    ["ONE HUNDRED", 100]
  ];  

  function findCash(change, cid, coins = []) {
    if (change === 0 && checkTotal(cid) === 0) {
      output.status = "CLOSED";
      console.log(coins);
      // changes cid array from all items being 0's as all cash has been given to include change on it 
      coins.forEach(el => {
        let index = cid.findIndex(elem => elem[0] === el[0]);
        cid[index][1] = el[1];
      })
      output.change = cid;
      return (output);

    } else if (change === 0) {
      // base case -> change = 0
      output.status = "OPEN";
      output.change = coins;
      return (output);
    } else if(checkTotal(cid) === 0 && change > 0) {
      output.status = "INSUFFICIENT_FUNDS";
      return (output);  // no cash in CID
    } else {
      // recursive call
      let highestUnit = currencies.reduce((highestNum, cur, index) => {

        if (Number(((change % cur[1]).toFixed(2)) < change) && cid[index][1] > 0) {  // if remainder is less than change & there is the specific money in CID

          let partialChange = parseInt(change / cur[1]).toFixed(2) * cur[1]; // 40
          highestNum = [cur[0], partialChange, index]; // overwrites previous values the last resulting the biggest one 
        }
        return highestNum;
      }, []);
      // it returns an ideal value , then I calculate if there is change possible in CID

      typeof highestUnit;

      if(highestUnit.length === 0) {
        output.status = "INSUFFICIENT_FUNDS";
        return (output);  
      }  

      if (cid[highestUnit[2]][1] >= highestUnit[1]) {  
        let remainAmt = change % highestUnit[1];
        change = Number(remainAmt.toFixed(2));
        coins.push([highestUnit[0], highestUnit[1]]);
        cid[highestUnit[2]][1] -= highestUnit[1];
      } else {    // in CID there is less 
        // give everything there is 
        change -= cid[highestUnit[2]][1];   
        change = Number(change.toFixed(2));
        coins.push([highestUnit[0], cid[highestUnit[2]][1]]);  
        cid[highestUnit[2]][1] = 0;
      }

      return findCash(change, cid, coins); 
    }
}

  function checkTotal(cid) {
    return cid.reduce((acum,cur) => {
      return acum += cur[1];
    }, 0)
  }

return findCash(change, cid);
}

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