Cash register - js certificstion

Hi there,

I am working on the last project (cash register) of the JavaScript Algorithm Certification and I am completly stuck! :scream:

I know that I am close to the result but I don’t find the trick !
Could you help me, please?

My code below :point_down:

function checkCashRegister(price, cash, cid) {
// Create an array with the amount for each currency unit in the same order as in example
  let currencyAmount = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];

  // Add this amount to cid with the exact number of each currency there are in cid
  let newCid = [];
  for (let i = 0; i < currencyAmount.length; i++) {
    newCid.push(cid[i].concat(currencyAmount[i]).concat(Math.round(cid[i][1]/currencyAmount[i] * 100) / 100));
  }
  console.log(newCid);

  // Create a function to calculate the total of register (cid)
  function totalInRegister(arr) {
    let register = 0;
    for (let i = arr.length-1; i >= 0; i--) {
      register += arr[i][1];
    }
    return Math.round(register * 100) / 100;
  }   
    console.log("Total in register : " + totalInRegister(cid) + "$");

  // Create a variable to store the amount to return back
  let cashToGive = cash - price;
    console.log("Cash to give : " + cashToGive + "$");

  // Write the three conditions asked
  let changeArr = [];
  if (totalInRegister(cid) < cashToGive) {
    return {
      status: "INSUFFICIENT_FUNDS",
      change: []
    };
    } else if (totalInRegister(cid) == cashToGive) {
      return {
        status: "CLOSED",
        change: cid
    };
    } else {
      for (let i = newCid.length-1; i >= 0; i--) {
      let value = 0;
        while (newCid[i][1] > 0 && cashToGive >= newCid[i][2]) {
console.log("Cid[i][1] " + newCid[i][1]);
          cashToGive -= newCid[i][2];
console.log("Cid[i][2] " + newCid[i][2]);
          cashToGive = Math.round(cashToGive * 100) / 100;
          newCid[i][1] -= newCid[i][2];
          value += newCid[i][2];       
console.log("New cash to give : " + cashToGive);
console.log("Value : " + value);
                  
          }
          if (value > 0) {
            changeArr.push([newCid[i][0], value]);
          }
             
      }
    }
}

console.log(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 0.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));

Thanks in advance !

Have a nice day and take care :pray:t2:

Manon

Hello there,

I have not checked the whole code, but the first thing that stood out is this whole else block does not contain any return:

} else {
      for (let i = newCid.length-1; i >= 0; i--) {
      let value = 0;
        while (newCid[i][1] > 0 && cashToGive >= newCid[i][2]) {
console.log("Cid[i][1] " + newCid[i][1]);
          cashToGive -= newCid[i][2];
console.log("Cid[i][2] " + newCid[i][2]);
          cashToGive = Math.round(cashToGive * 100) / 100;
          newCid[i][1] -= newCid[i][2];
          value += newCid[i][2];       
console.log("New cash to give : " + cashToGive);
console.log("Value : " + value);
                  
          }
          if (value > 0) {
            changeArr.push([newCid[i][0], value]);
          }
             
      }

I hope this helps

1 Like

If you add a return statement to the else statement for when you’re returning status: open, that’ll deal with most of the tests.
The only other thing I’d suggest is having another look at the condition

Return {status: "INSUFFICIENT_FUNDS", change: []}if you cannot return the exact change.

1 Like

For starters your else statement doesn’t return anything, neither does your entire checkCashRegister function. If you don’t specify a return statement the function outputs undefined.
Now for some help with this problem create an empty object that will be eventually returned by your function, write functions to update this status and change property of this empty object. I see in your code you just return an object hardcoded to what you want but having a variable hold that object would be so much better and save you time. Since you can write functions to update the status independently and the change array independently. Then at the end of it all return this object.
Hope this was enough to nudge you a bit, its a difficult problem for sure

1 Like

Thank you for your answer ! I found the solution yesterday but I have to write my code better for next time :slight_smile: !

1 Like

Thank you !
Yes, I wrote a return but it didn’t work so I cancel it and forgot to write it again. But I found the solution yesterday ! :smiley: