Help on Cash Register Project

For the cash register project, I’m running into an issue with passing the 2 ‘status: “OPEN”’ checks. The code runs fine and returns the correct change, however, (I think) that the output format may not be matching with the acceptable answer.

The only difference that I can see between my output and the acceptable output is single quotes (’) in my output vs double quotes (") in the acceptable output which is given when the check fails.

For example for the second check my output is:
{ status: ‘OPEN’, change: [ [ ‘QUARTER’, 0.5 ] ] }

and the acceptable output in the error message is:
{status: “OPEN”, change: [[“QUARTER”, 0.5]]}

Any help/guidance will be greatly appreciated. Thanks.

**(My code is below)

var correctChange = [ ];

function checkCashRegister(price, cash, cid) {

  var change = cash - price;

  var roundedChange = change.toFixed(2);

  var changeInDrawer = 0;

for(var i = 0; i<cid.length; i++){

  changeInDrawer += cid[i][1];

   }; 

if(changeInDrawer < roundedChange){

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

} else {

  if(changeInDrawer == roundedChange){

return {status: "CLOSED", change: cid};

  } else {

  let howMuchChange = {

  "ONE HUNDRED": [cid[8][1],100],

  "TWENTY": [cid[7][1],20],

  "TEN": [cid[6][1],10], 

  "FIVE": [cid[5][1],5],

  "ONE": [cid[4][1],1],

  "QUARTER": [cid[3][1],.25],

  "DIME": [cid[2][1],.10],

  "NICKEL": [cid[1][1],.05],

  "PENNY": [cid[0][1],.01]

  };

  let variableKey=0;

  let i = 0;

  let variable_value;

  let pushedchange = 0;

for(var value in howMuchChange){

  variable_value = value;

  if(roundedChange >= Object.values(howMuchChange)[i][1]) { if(roundedChange >= Object.values(howMuchChange)[i][0]){

    correctChange.push([variable_value, Object.values(howMuchChange)[i][0]]);

    roundedChange -= Object.values(howMuchChange)[i][0];

  Object.values(howMuchChange)[i][0]-=Object.values(howMuchChange)[i][0]

  } else{

    if(roundedChange <= Object.values(howMuchChange)[i][0]){

     pushedchange = roundedChange -( roundedChange%Object.values(howMuchChange)[i][1])

    correctChange.push([variable_value, pushedchange]);

    roundedChange -= pushedchange;

    roundedChange = roundedChange.toFixed(2);

    } 

  } 

  } i+=1;

    };

    if(roundedChange > 0 ){

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

    } if (roundedChange == 0){

 return { status: "OPEN", change: correctChange}; 

    };

};

};

}

console.log(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]]))

*End Code

Well … (1) your code in the post didn’t format very well, might want to fix that (so that anyone who wants to help you can paste into an editor more easily). And (2) did you try changing to double quotes and did it fix the issue?

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 (’).

1 Like

Hello and welcome to the FCC community~!

This might provide some insight:
image

When I log the results of the second failing test, I see… you’ve got an extra “quarter” array.

Here’s another hint - remove your function call from the very bottom (the console.log(checkCashRegister) part) :wink:

@themagicbean, @nhcarrigan
Ok, yes that did come out weird, will keep the backtick " ` " for code blocks in mind for future use. thank you

@themagicbean Yes i did try the double quotes and it did not fix the issue

@nhcarrigan, can you please double check that “extra quarter”, for some reason i cannot replicate that output in my code. (the console.log at the end on the other hand got left in while debugging…oops…the test still fails)

Let me add my code again (see below):

 
var correctChange = [];
function checkCashRegister(price, cash, cid) {
  var change = cash - price;
  var roundedChange = change.toFixed(2);
  var changeInDrawer = 0;
for(var i = 0; i<cid.length; i++){
  changeInDrawer += cid[i][1];
   }; 
if(changeInDrawer < roundedChange){
return {status: "INSUFFICIENT_FUNDS", change:[]};
} else {
  if(changeInDrawer == roundedChange){
return {status: "CLOSED", change: cid};
  } else {
  let howMuchChange = {
  "ONE HUNDRED": [cid[8][1],100],
  "TWENTY": [cid[7][1],20],
  "TEN": [cid[6][1],10], 
  "FIVE": [cid[5][1],5],
  "ONE": [cid[4][1],1],
  "QUARTER": [cid[3][1],.25],
  "DIME": [cid[2][1],.10],
  "NICKEL": [cid[1][1],.05],
  "PENNY": [cid[0][1],.01]
  };
  let variableKey=0;
  let i = 0;
  let variable_value;
  let pushedchange = 0;
for(var value in howMuchChange){
  variable_value = value;
  if(roundedChange >= Object.values(howMuchChange)[i][1]) { if(roundedChange >= Object.values(howMuchChange)[i][0]){
    correctChange.push([variable_value, Object.values(howMuchChange)[i][0]]);
    roundedChange -= Object.values(howMuchChange)[i][0];
  Object.values(howMuchChange)[i][0]-=Object.values(howMuchChange)[i][0]
  } else{
    if(roundedChange <= Object.values(howMuchChange)[i][0]){
     pushedchange = roundedChange -( roundedChange%Object.values(howMuchChange)[i][1])
    correctChange.push([variable_value, pushedchange]);
    roundedChange -= pushedchange;
    roundedChange = roundedChange.toFixed(2);
    } 
  } 
  } i+=1;
    };
    if(roundedChange > 0 ){
      return {status: "INSUFFICIENT_FUNDS", change:[]};
    } if (roundedChange == 0){
 return { status: "OPEN", change: correctChange}; 
    };
};
};
}


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]])

Hello~!

It’s that bottom function call that’s throwing off the tests. You call the function at the bottom of your code, which sets correctChange to be [["QUARTER", 0.5]]. This is correct - however, correctChange is a global variable, meaning it does not get cleared before the next function call. So the tests run, and are seeing the extra value from your function call. You have two options to fix this:

  1. Remove the function call from the bottom of your code.
  2. Move the correctChange declaration to be inside the function scope.

Hope this helps! :slight_smile:

Hi again, i initialized the correctChange variable inside the function, to fix the issue. I really appreciate your input. Thank You!

1 Like