Program returning the same value as test case, but FCC still marking me wrong

My code is giving the same answer that the test case requires. I have no idea why my code is not passing the test. I’ve copied and pasted the passing test answer and the answer that my code is giving me below (along with my entire code below that). Thanks for your help!

The last test case for this challenge is supposed to return this value:

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

My code returns the following:

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

Your code so far


var values = {
	"PENNY": 0.01,
 "NICKEL": 0.05,
 "DIME": 0.10,
 "QUARTER": 0.25,
 "ONE": 1,
 "FIVE": 5,
 "TEN": 10,
 "TWENTY": 20,
 "ONE HUNDRED": 100
};

function rework(arr){
	return [arr[0], values[arr[0]], parseInt((arr[1]/values[arr[0]]).toFixed(), 10)];
}

function compareArrays(arr1, arr2){
	let state = true;
	for(let i = 0; i < arr1.length; i++){
 	if(arr1[i][0] !== arr2[i][0]){
   	state = false;
     break;
   }
   if(arr1[i][1] !== arr2[i][1]){
   	state = false;
     break;
   }
 }
 return state;
}

var concatArray = [];

function checkCashRegister(price, cash, cid) {
 let arrayVar = cid.concat([]);
	let newArr = arrayVar.reduce(function(acc, elem){
 	acc = acc.concat([rework(elem)]);
   return acc;
 }, []);
 newArr = newArr.reverse();
 let due = cash - price;
 newArr = newArr.reduce(function(acc, elem){
 	if(due > elem[1]){
   	let elem2 = elem[2];
     let elem1 = elem[1];
   	while(due >= elem1 && elem2 > 0){
     	due = parseFloat((due - elem1).toFixed(2), 10);
       elem2--;
     }
     acc = acc.concat([[elem[0], (elem[2] - elem2)*elem[1]]]);
     concatArray = concatArray.concat([[elem[0], (elem[2] - elem2)*elem[1]]]);
   }
   else{
   	concatArray = concatArray.concat([[elem[0], 0]]);
   }
   return acc;
 }, []);

	let inOrderArr = concatArray.concat([]).reverse();

	if(due === 0){
 	if(compareArrays(inOrderArr, cid)){
   	return {
       status: "CLOSED",
       change: concatArray.reverse()
       };
   }
   else{
   	return {
   	status: "OPEN",
     change: newArr
   	};
   }
 }
 else{
 	return {
   	status: "INSUFFICIENT_FUNDS",
     change: []
   };
 }
}

let newthing = checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

console.log(newthing);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

I didn’t really try to debug the code but calling the last function call twice results in an error Cannot read property '0' of undefined. You can see it in the browser console as well when running the tests.

It’s likely some mutation or issue with a global that the test is triggering a bug with. It is happening in the compareArrays function if (arr1[i][0] !== arr2[i][0]) code.

1 Like

Yep, here is the global. I suspect @lasjorg is 100% correct that it’s a global variable issue. I’d refactor to remove that global. A function isn’t reusable if it relies on a global variable to be reset in order to function correctly.

2 Likes

Yep, you just need to move that declaration inside the function that is using it and you should pass the test.

1 Like