Algorithm returning correct answers but doesn't pass

Algorithm returning correct answers but doesn't pass
0

Javascript Cash Register - This algorithm doesn’t pass conditions 2,3, and 6 but seems to return the required answers for those given inputs. I would love to have it pass all conditions without making huge changes.

function checkCashRegister(price, cash, cid) {

let changeObject = 
{"ONE-HUNDRED": 0,
"TWENTY": 0,
"TEN": 0,
"FIVE": 0,
"ONE": 0,
"QUARTER": 0,
"DIME": 0,
"NICKEL": 0,
"PENNY": 0} 

var box = [];
 
let balance = cash - price; 
console.log(balance); // 

let drawerArray = cid.map((elem) => elem[1]);
console.log(drawerArray); // [ 1.01, 2.05, 3.1, 4.25, 90, 55, 20, 0, 100 ]

let typeOfMoney = [1,5,10,25,100,500,1000,2000,10000]


function giveMoney() {

//let typeOfMoney = [.01,.05,.1,.25,1,5,10,20,100]   // use "delete" to solve


// this kills the function and returns the final change object
if (balance < 0.001) {
 for (var prop in changeObject) 
 {
   if(changeObject[prop] != 0){ box.push([prop,changeObject[prop]/100])} 
 }
let status1 = "OPEN";
if (drawerArray[0] === 0) {
  status1 = "CLOSED";
};

let answerObj = {};
answerObj["status"] = status1;
answerObj["change"] = box
 console.log(drawerArray);
 console.log(box);
 console.log(answerObj);
 console.log(balance);
 return answerObj;
} 
// --------------------------------------------------------------

if (balance > 0 && typeOfMoney.length === 0) {
  let status1 = "INSUFFICIENT_FUNDS";
  var answerObj = {};
  answerObj["status"] = "INSUFFICIENT_FUNDS" ;
  answerObj["change"] = [];
  return answerObj;
}


// this removes one unit of type of money if balance is below that unit value
if (balance > 0 && balance < typeOfMoney[typeOfMoney.length - 1]) {
  //console.log(balance);
  //console.log(typeOfMoney);
  typeOfMoney.pop();
  drawerArray.pop()
  console.log(typeOfMoney);
  console.log(drawerArray);
  console.log(balance)
  return giveMoney()
}
//-----------------------------------------------------------------------


//This says what to do if you don't have enough big bills
if (balance >= typeOfMoney[typeOfMoney.length - 1] && drawerArray[drawerArray.length -1] <  typeOfMoney[typeOfMoney.length - 1]) {
  drawerArray.pop()
  typeOfMoney.pop();
  return giveMoney()
}
//-------------------------------------------------------------------------



if (balance >= typeOfMoney[typeOfMoney.length - 1] && drawerArray[drawerArray.length -1] >= typeOfMoney[typeOfMoney.length - 1]) 
// ^if change due is more than(or =) type of money and more than (or =) what you hold of that type
{ drawerArray[drawerArray.length -1] -= typeOfMoney[typeOfMoney.length - 1];
  // ^subtract one unit of type of money from drawer
  balance -= Math.round(typeOfMoney[typeOfMoney.length - 1]*100)/100;
  balance = Math.round(balance * 100) / 100;
  console.log(balance);
  console.log(changeObject);
  //^ subtract one unit of type of money from balance
  //console.log(balance + "check");


  switch(typeOfMoney[typeOfMoney.length -1]){
    case 10000:
    changeObject["ONE-HUNDRED"] += 10000;
    break;  
    case 2000:
    changeObject["TWENTY"] += 2000;
    break;
    case 1000:
    changeObject["TEN"] += 1000;
    break;
    case 500:
    changeObject["FIVE"] += 500;
    break;
    case 100:
    changeObject["ONE"] += 100;
    break;
    case 25:
    changeObject["QUARTER"] += 25;
    break;
    case 10:
    changeObject["DIME"] += 10;
    break;
    case 5:
    changeObject["NICKEL"] += 5;
    break;
    case 1:
    changeObject["PENNY"] += 1;
    break;

    //^add one unit of type of money to change given(changeObject);
    }
  console.log(balance);
  return giveMoney();
}



}

return giveMoney();

}

 





// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

checkCashRegister(1950, 2000, [["PENNY", 50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

/*
96.74
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]])should return an object.
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]])should return {status: "OPEN", change: [["QUARTER", 0.5]]}.
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]]}.
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])should return {status: "INSUFFICIENT_FUNDS", change: []}.
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])should return {status: "INSUFFICIENT_FUNDS", change: []}.
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])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]]}.

Design a cash register drawer function checkCashRegister()that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.

cidis a 2D array listing available currency.

The checkCashRegister()function should always return an object with a statuskey and a changekey.

Return {status: "INSUFFICIENT_FUNDS", change: []}if cash-in-drawer is less than the change due, or if you cannot return the exact change.

Return {status: "CLOSED", change: [...]}with cash-in-drawer as the value for the key changeif it is equal to the change due.

Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the changekey.

Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.

Currency Unit	Amount
Penny	$0.01 (PENNY)
Nickel	$0.05 (NICKEL)
Dime	$0.1 (DIME)
Quarter	$0.25 (QUARTER)
Dollar	$1 (DOLLAR)
Five Dollars	$5 (FIVE)
Ten Dollars	$10 (TEN)
Twenty Dollars	$20 (TWENTY)
One-hundred Dollars	$100 (ONE HUNDRED)


*/

try diffrend browser but its more likely that u made a typo

I mean it doesn’t pass on Freecodecamp.com. I want to submit it. The answers seem to match up but the algorithm isn’t accepted.

I would still appreciate it if someone could take a few minutes to compare the output of this algorithm to the required output on Freecodecamp. It looks to me as this should pass all tests.

you have some conversion issues

remember that the inputted numbers are in dollars, not in pennies.

this function call below returns {"status":"CLOSED","change":[["PENNY",0.5]]}, but the change due in this case is 50$.

checkCashRegister(1950, 2000, [["PENNY", 50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

the function below returns {"status":"INSUFFICIENT_FUNDS","change":[]}, but there are enough coins to give the due change of 0.5$

checkCashRegister(19.50, 20.00, [["PENNY", 50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

You are mistaken as I divide final answers by 100. If someone can run this code, they will see that my answers appear exactly the same as the required output for each input supplied on Freecodecamp.

I say there is something wrong with conversions because if price is 50$, cash is 100$ the change should be 50$ but your code say to give back 0.5$

so I think that your issue is that you never convert the input values (price and in pennies, if you add the conversion to pennies then it works fine.

Or if you want to do the conversion only one I think you can do it with the variable balance

Price is in cents. Please view the cash register challenge below:

JavaScript Algorithms and Data Structures Projects: Cash Register

Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument ( price ), payment as the second argument ( cash ), and cash-in-drawer ( cid ) as the third argument.

cid is a 2D array listing available currency.

The checkCashRegister() function should always return an object with a status key and a change key.

Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.

Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.

Otherwise, return {status: "OPEN", change: [...]} , with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.

Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.

Currency Unit	Amount
Penny	$0.01 (PENNY)
Nickel	$0.05 (NICKEL)
Dime	$0.1 (DIME)
Quarter	$0.25 (QUARTER)
Dollar	$1 (DOLLAR)
Five Dollars	$5 (FIVE)
Ten Dollars	$10 (TEN)
Twenty Dollars	$20 (TWENTY)
One-hundred Dollars	$100 (ONE HUNDRED)
Passed
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]])should return an object.
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]])should return {status: "OPEN", change: [["QUARTER", 0.5]]}.
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]]}.
Passed
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])should return {status: "INSUFFICIENT_FUNDS", change: []}.
Passed
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])should return {status: "INSUFFICIENT_FUNDS", change: []}.
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])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]]}

look at here, 19.5 and 20 needs to return 0.5
see what your code returns instead for this