I think are a problem in one check certification javascript

Hi

I´m try to do cash register certification.

This is my code:

function checkCashRegister(price, cash, cid) {
  let change = {
    'status':"OPEN",
    'change':[]
    };
// This count only used to state closed return cid.length;
  let countToClosed = 0;
  let moneyValues = {
    'PENNY':0.01,
    'NICKEL':0.05,
    'DIME':0.1,
    'QUARTER':0.25,
    'ONE':1.00,
    'FIVE':5.00,
    'TEN':10.00,
    'TWENTY':20.00,
    'ONE HUNDRED':100.00
  }
  let moneyBack = {
    'PENNY':0,
    'NICKEL':0,
    'DIME':0,
    'QUARTER':0,
    'ONE':0,
    'FIVE':0,
    'TEN':0,
    'TWENTY':0,
    'ONE HUNDRED':0
  };
    let backMoney = cash-price;
  
/*
  This function only calculate
  value to the coins give back money
  to the customer
*/

  function moneyBackCalculate(change){
    if(change >= 100){
     let changeNew = change - moneyValues['ONE HUNDRED'];
     moneyBack['ONE HUNDRED'] = moneyBack['ONE HUNDRED']+1;
     return moneyBackCalculate(changeNew);
    };
    if(change < 100 && change >= 20){
     let changeNew = change - moneyValues.TWENTY;
     moneyBack.TWENTY= moneyBack.TWENTY+1;
     return moneyBackCalculate(changeNew); 
    };
    if(change < 20 &&change >= 10){
     let changeNew = change - moneyValues.TEN;
     moneyBack.TEN = moneyBack.TEN+1;
     return moneyBackCalculate(changeNew);
    };
    if(change < 10 &&change >= 5){
     let changeNew = change - moneyValues.FIVE;
     moneyBack.FIVE = moneyBack.FIVE+1;
     return moneyBackCalculate(changeNew);
    };
    if(change < 5 &&change >= 1){
     let changeNew = change - moneyValues.ONE;
     moneyBack.ONE = moneyBack.ONE+1;
     return moneyBackCalculate(changeNew);
    };
    if(change < 1 &&change >= 0.25){
     let changeNew = change - moneyValues.QUARTER;
     moneyBack.QUARTER = moneyBack.QUARTER+1;
     return moneyBackCalculate(changeNew);
    };
    if(change < 0.25 &&change >= 0.1){
     let changeNew = change - moneyValues.DIME;
     moneyBack.DIME = moneyBack.DIME+1;
     return moneyBackCalculate(changeNew);
    };
    if(change < 0.10 &&change >= 0.05){
     let changeNew = change - moneyValues.NICKEL;
     moneyBack.DIME = moneyBack.NICKEL+1;
     return moneyBackCalculate(changeNew);
    };
    if(change < 0.05 &&change >= 0.01){
     let changeNew = change - moneyValues.PENNY;
     moneyBack.PENNY = moneyBack.PENNY+1;
     return moneyBackCalculate(changeNew);
    };
    if(change < 0.01 && change > 0.00){
     let changeNew = change - moneyValues.PENNY;
     moneyBack.PENNY = moneyBack.PENNY+1;
     return moneyBackCalculate(changeNew);
    };
    if(change == 0){
      return moneyBack;
    };
  }
  moneyBackCalculate(backMoney);

  for(let i = 0;i<cid.length;i++){
    for(let property in moneyBack){
      if(property == cid[i][0]){
        let value = moneyBack[property]*moneyValues[property];
        if(cid[i][0] < value){
          return {status: "INSUFFICIENT_FUNDS", change: []}
        }else if(cid[i][0] == value){
          countToClosed++;
          change.change.unshift([property,value])
        }else{
          if(value != 0){
            change.change.unshift([property,value])
          }
        }
      }else{
        continue;
      }
    }
  }
  return change
}

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

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

I but in this test

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]]) debe devolver {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

maybe have a problem because my code give one more to twenty and the correct form is five have 10 more and ten have 10 more. This for me is incorrect because my logic say to me : why not pass one of twenty?

I know my code not check open, closed, and insufficient founds. Im going to part. I dont wanna solution. I only post this to any development can check this part to say me im true or im wrong.

I dont wanna solutions only can people see and why im wrong. And i dont know how to inform if see a problem in this certifications.

Thanks for read

I’ve edited your code 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

Thanks

Is the first post build i dont know that.

Thanks for the edit.

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

For that test case, your solution return this :
image

But, test case told us that your cash register only have TWENTY worth of $60.

So, did you check your balance ?

1 Like

I don’t see that i’m focus to do my solution calculate the back and later check if have or not the money.

But that only to a cash register dont work. Is that the problem and need think how can calculate the back and check correct.

Thanks for read my post

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