JavaScript Algorithms and Data Structures Projects: Cash Register, all tests pass except the last one

Hello all,

I am currently trying to pass this last challenge for the JavaScript certification.

Currently it passes all of the tests except the last one. When I run the code in a separate browser window, I get the expected output for the last test however it doesn’t pass for some reason.

Can anyone help?

My code so far is below.


let changeInDrawer;

let unreversedcashArr = [];

let cashArr = [];

let finalChangeArr;

let output = { status: null, change: []};

let change;

function checkCashRegister(price, cash, cid){

    const denom = [

        ["PENNY", 0.01],

        ["NICKEL", 0.05],

        ["DIME", 0.10],

        ["QUARTER", 0.25],

        ["ONE", 1],

        ["FIVE", 5],

        ["TEN", 10],

        ["TWENTY", 20],

        ["ONE HUNDRED", 100]

       

      ];

      

      const reverseDenom = denom.reverse();

    output = { status: null, change: [] };

    change = cash - price;

    change = (Math.round( change * 100 ) / 100).toFixed(2);

    change = parseFloat(change);

    changeInDrawer = (cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] + cid[6][1] + cid[7][1] + cid[8][1]).toFixed(2);

    changeInDrawer = parseFloat(changeInDrawer);

    if (changeInDrawer - change === 0){

        output.status = "CLOSED";

        output.change = [cid];

        return output;

    }

    unreversedcashArr = [cid[0][1],cid[1][1],cid[2][1],cid[3][1],cid[4][1],cid[5][1],cid[6][1],cid[7][1],cid[8][1]];

    cashArr = unreversedcashArr.reverse(); 

    if (changeInDrawer < change){

        output.status = "INSUFFICIENT_FUNDS";

        return output;

    } else {

        for(let i = 0 ; i<cashArr.length; i++){

            if (change >= reverseDenom[i][1] && change >= cashArr[i]){

                change = (Math.round( change * 100 ) / 100).toFixed(2);

                change = change - cashArr[i];

                change = (Math.round( change * 100 ) / 100).toFixed(2);

                change = parseFloat(change);

    

                cashArr[i] = 0;

            } else if (change >= reverseDenom[i][1] && change < cashArr[i]){

                change = (Math.round( change * 100 ) / 100).toFixed(2);

                change = parseFloat(change);

                let changeCopy = change;

                change = change % reverseDenom[i][1];

                change = (Math.round( change * 100 ) / 100).toFixed(2);

                change = parseFloat(change);

                cashArr[i] = cashArr[i] - changeCopy + change;

                cashArr[i] = (Math.round( cashArr[i] * 100 ) / 100).toFixed(2);

                change = (Math.round( change * 100 ) / 100).toFixed(2);

            }

        }

    }

if (change > 0 && change != cash - price){

    output.status = "INSUFFICIENT_FUNDS";

    output.change = [];

    return output;

}

if (change < 0.01){

    change = Math.floor(change);

}

changeInDrawer = changeInDrawer - change;

finalChangeArr = reverseDenom;

for (let j = 0; j<finalChangeArr.length; j++){

    finalChangeArr[j][1] = cid[cid.length-1-j][1] - cashArr[j];

    finalChangeArr[j][1] = (Math.round( finalChangeArr[j][1] * 100 ) / 100).toFixed(2);

    finalChangeArr[j][1] = parseFloat(finalChangeArr[j][1]);

}

for (let k = finalChangeArr.length-1; k>=0; k--){

    if (finalChangeArr[k][1] == 0){

        finalChangeArr.splice(k, 1);

    }

    output.change = finalChangeArr;

}

if (changeInDrawer > 0){

    output.status = "OPEN";

    return output;

}

if (change > 0.01){

    output.status = "INSUFFICIENT_FUNDS";

    output.change = [];

}

return output;

}

Your browser information:

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

Challenge: Cash Register

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

you return

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

when you should return

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

can you see the difference? if you see that you should be able to pin point what’s the issue

Yes thank you. I was returning the array inside an array instead of just the array itself. Thank you for your help!