Cash Register final answer

Hi
I developed my program and got it working in VSC and pasted it into FCC.
It runs and returns all the required but did not pass.
How do I find out what I am missing ?
Thanks

For us to help you find the bug in your code, you’ll need to post your code.

1 Like
function checkCashRegister(price,cash,cid) {


var cid = cid;

console.log(cid);


var denominations = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1.0],
  ["FIVE", 5.0],
  ["TEN", 10.0],
  ["TWENTY", 20.0],
  ["ONE HUNDRED", 100.0]
];

var change =   [
    ["PENNY", 0.00],
  ["NICKEL", 0.00],
  ["DIME", 0.0],
  ["QUARTER", 0.0],
  ["ONE", 0.0],
  ["FIVE", 0.0],
  ["TEN", 0.0],
  ["TWENTY", 0.0],
  ["ONE HUNDRED", 0.0]
];



  var cidSum = cid.reduce((total,amt,index,array) => {
        total += array[index][1];
        return total;
      },0);

      //console.log("cid start sum is : ",cidSum);

      cidSum = parseFloat(cidSum).toFixed(2);

      //console.log("cid start sum is : ",cidSum);




var changeRev = change.reverse();

var changeRev = changeRev;

var denominationRev = denominations.reverse();


var cidRev = cid.reverse();

var cidRev = cidRev;

//console.log("Final cidRev : \n ",cidRev);
//console.log("Final changeRev : \n ",changeRev);


var denomsOnly = [];


for (let i = 0; i<denominationRev.length; i++) {
    denomsOnly.push(denominationRev[i][1]);
    //return denomsOnly;
}



var cashPayment = cash;  // what customer paid before changeBack
var price = price;


var denomAndBalance = [];
var denomPlace = 0;
var denom = [];
var changeRevSum = 0;
var nextLowest = 0;
var cashBackOwed = 0;



function checkSubtract(amount) {


  console.log("********** the amount from checkSubtract is : *********",amount);

        //  make balance only array here
        //  need denom only array to select denom
        //   need denom only array only having cash balances
        //   2 arrays 1, denoms only, 1 cid with balances only
        //  if true 
        //   put denom match in new aray
        //    and put cid with balance in another array


        // then here below filter cid but later must choose from these arrays
        //  for next Lowest

        //  will this solve selecting denom with no cash remaining
        //  because its the denom without knowing cash balance that goes on
        //  if amount owing greaterv than denom will still select it
        //  even though that denom is depleted

        //   is this still the same problem ??
        //  selecting depleted because denom under amount


    denom = denominationRev.filter((amt,index) => {

      //   maybe if denom less than amount and cid balance greater than threshold

      return amt[1] <= amount && cidRev[index][1] > (0.9*amt[1]);
  });

  var currentDenom = denom[0];
  var currentDenomName = denom[0][0];


  denomPlace = denominationRev.indexOf(currentDenom);


  denomAndBalance = cidRev[denomPlace];


  function checkBalance(denomAndBalance) {

    //console.log("denomAndBalance at line 146 ",denomAndBalance);
   
    var ninetyIndexNum = cidRev.indexOf(denomAndBalance);

    var cidThreshold = (.9*(denominationRev[ninetyIndexNum][1]));

    //console.log("denomAndBalance at line 152 ",denomAndBalance);

    //console.log("cidThreshold line 154 ",cidThreshold);


    if (denomAndBalance[1] > cidThreshold) {

      cidRev[ninetyIndexNum][1] = cidRev[ninetyIndexNum][1]- denominationRev[ninetyIndexNum][1];  // should look in denominations for 1 unit to subtract  


      changeRev[ninetyIndexNum][1] = changeRev[ninetyIndexNum][1] + denominationRev[ninetyIndexNum][1];


      changeRevSum = changeRev.reduce((total,amt,index,array) => {
        total += array[index][1];
        return total;
      },0);

      changeRevSum = parseFloat(changeRevSum).toFixed(2);

      cashBackOwed = (cashPayment - price - changeRevSum).toFixed(2);

      //console.log("cashBackOwed is : ",cashBackOwed);
      //console.log("look at changeRev : ",changeRev);

      

      //  if cashbackowed zero and cidsum at beginning = cash-price = cidsum
      //  if changeRev == cidSum from beginning

      if (changeRevSum == cidSum && cashBackOwed == 0) {
          // need return with updated change with denom
          // CLOSED ARRAY INCLUDES ZERO DENOMS
          //   CLOSED ARRAY DENOMS ARE LOWEST TO HIGHEST
          
          
          var preCloseArr = changeRev;

          console.log("look at preClose array",preCloseArr);

          var convFixed = preCloseArr.map(([key,value]) => {
              return [key,value.toFixed(2)*1];
          })

          console.log("look at converted to 2 fixed : ",convFixed);


          //const newArray = array.map(([key, value]) => [key, value * 2]);
          //console.log(newArray); // [['a', 2], ['b', 4], ['c', 6]]

          var closeArr = convFixed.reverse();

          console.log("look at closeArr 2 decimal only : ",closeArr);




          console.log("changeRevSum",changeRevSum);
          console.log("cidSum",cidSum);
          console.log("changeRev",changeRev);


          var result = {status: "CLOSED", change: closeArr}
          console.log(result);
          return result;
      }
    
      

      if (cashBackOwed == 0) {
        //console.log("cashBackOwed equals zero ")
        //console.log({status: "OPEN", change: changeRev})

        //  need return of only denoms with balances
        //  filter ?

        var openArr = changeRev.filter((amt,index) => {
            return amt[1] > 0;
        });

        //console.log("look at cut down open changeRev array : ",openArr);


        var result = {status: "OPEN", change: openArr}

        console.log(result);

        return result;

      }

      if ((denomAndBalance[0] == "PENNY" && cashBackOwed > denomAndBalance[1].toFixed(2)) /*|| cashBackOwed > cidSum*/) {
          result = {status: "INSUFFICIENT_FUNDS", change: []};
          //console.log("look current denomAndBalance s/b penny : ",denomAndBalance);
          //console.log("look at  penny balance",denomAndBalance[1]);
          //console.log("look at cashBackOwed : ",cashBackOwed);
          //console.log("look at cidSum : ",cidSum);
          console.log(result);
        return {status: "INSUFFICIENT_FUNDS", change: []};

      }




       checkSubtract(cashBackOwed);


    } else {


      nextLowest = cidRev.indexOf(denomAndBalance)+1

      //console.log(" look at nextLowest index :",nextLowest);

      //console.log(" look at nextLowest denom and balance :",cidRev[nextLowest]);

      denomAndBalance = cidRev[nextLowest];

      //console.log(" look at nextLowest denomAndBalance :",denomAndBalance);


      if (denomAndBalance[0] == "PENNY" && cashBackOwed > denomAndBalance[1] || cashBackOwed > cidSum) {
          result = {status: "INSUFFICIENT_FUNDS", change: []};
          //console.log("look at  penny balance",denomAndBalance[1]);
          //console.log("look at cashBackOwed : ",cashBackOwed);
          //console.log("look at cidSum : ",cidSum);
          console.log(result);
        return {status: "INSUFFICIENT_FUNDS", change: []};

      }

        checkBalance(denomAndBalance);  //   this is the recursive checkBalance

      }

    
  }   //   this end bracket for checkBalance function


  checkBalance(denomAndBalance);  //  this outside function and first time it is called


  return cidRev;


}   //   this end bracket for checkSubtract function

checkSubtract(cash-price); // here with cashRegister inputs




}   //   this end of checkCashRegister function

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

what does your function return?

image

Hi
Below is the output from running the code.
It looks like it has all the required to me ?

I get the same result in my VSC.

Thanks



// tests completed
// console output
{ status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }
{ status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }
{ status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }
{ status: 'OPEN',
  change: 
   [ [ 'TWENTY', 60 ],
     [ 'TEN', 20 ],
     [ 'FIVE', 15 ],
     [ 'ONE', 1 ],
     [ 'QUARTER', 0.5 ],
     [ 'DIME', 0.2 ],
     [ 'PENNY', 0.04 ] ] }
{ status: 'INSUFFICIENT_FUNDS', change: [] }
{ status: 'INSUFFICIENT_FUNDS', change: [] }
{ status: 'CLOSED',
  change: 
   [ [ 'PENNY', 0.5 ],
     [ 'NICKEL', 0 ],
     [ 'DIME', 0 ],
     [ 'QUARTER', 0 ],
     [ 'ONE', 0 ],
     [ 'FIVE', 0 ],
     [ 'TEN', 0 ],
     [ 'TWENTY', 0 ],
     [ 'ONE HUNDRED', 0 ] ] }
{ status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }

Looking again I have 8 returns for 6 tests.
All correct but maybe duplicate results for 2 of the tests throwing a reject for code ?

I think that what @ilenia was trying to point out to you is that if you run this console.log at the end of your code you are getting undefined.

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

what happens if you remove all console.logs from inside the function and print the output of the function call?

the console.logs inside the function are good for debugging but don’t let htem trip you up

I reran in 5 of 9 reply without console log.
Maybe some still left.
I will prune again.
I was looking up some stuff about undefined (i.e. say vs declared ) and will also look for print methods.
Unclear about print vs console log so will review that also.
Maybe just placement of returns.
Thanks again.

Got it !!
Thanks a million.

I initialized the "result " variable to be scoped globally.
Prior only function scoped.
Believe this solved it because undefined was gone and it was accepted.

Please don’t do this. This makes extremely fragile code. You do not need a global variable for this challenge.