Cash Project - problem with getting input array

Could somebody explain me why return cid placed in the end does not return original values of cid?

Instead of it, I am geting reversed array coinsincash. Why?

const coins = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
function checkCashRegister(price, cash, cid) {

var changedue = cash - price;
var coinsincash = [];
var totalincash = 0;
for (var z = cid.length - 1; z >= 0; z--) {coinsincash.push(cid[z]); totalincash += cid[z][1];}

//if(changedue > totalincash) {return "insufficient funds";}
//else if(changedue == totalincash) {return coinsincash;} 
//else if(changedue < totalincash) {return "mniejsza reszta niz totalincash"}

for (var y=0; y < coinsincash.length; y++) {
while (changedue > 0 && changedue >= coins[y] && coinsincash[y][1] > 0) {
changedue = Math.round((changedue - coins[y] ) * 100) / 100
coinsincash[y][1] = Math.round((coinsincash[y][1] - coins[y]) * 100) / 100
}}

return cid


} checkCashRegister(4700, 5000, [["PENNY", 1.0], ["NICKEL", 0.25], ["DIME", 12.5], ["QUARTER", 12.5], ["ONE", 100], ["FIVE", 20], ["TEN", 2000], ["TWENTY", 300], ["ONE HUNDRED", 400]]);

I’m not sure what you mean. When I put it in a pen, it returns the same array with unchanged data.

const coins = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
function checkCashRegister(price, cash, cid) {
  console.log('here1', cid);
  var changedue = cash - price;
  var coinsincash = [];
  var totalincash = 0;
  for (var z = cid.length - 1; z >= 0; z--) {
    coinsincash.push(cid[z]);
    totalincash += cid[z][1];
  }

  //if(changedue > totalincash) {return "insufficient funds";}
  //else if(changedue == totalincash) {return coinsincash;}
  //else if(changedue < totalincash) {return "mniejsza reszta niz totalincash"}
  console.log('here2', cid)
  for (var y = 0; y < coinsincash.length; y++) {
    while (changedue > 0 && changedue >= coins[y] && coinsincash[y][1] > 0) {
      changedue = Math.round((changedue - coins[y]) * 100) / 100;
      coinsincash[y][1] =
        Math.round((coinsincash[y][1] - coins[y]) * 100) / 100;
    }
  }

  return cid;
}

const input = [
  ["PENNY", 1.0],
  ["NICKEL", 0.25],
  ["DIME", 12.5],
  ["QUARTER", 12.5],
  ["ONE", 100],
  ["FIVE", 20],
  ["TEN", 2000],
  ["TWENTY", 300],
  ["ONE HUNDRED", 400]
];

const output = checkCashRegister(4700, 5000, input);

const haveTheSameData = JSON.stringify(input) === JSON.stringify(output);
const areTheSameArray = input === output;

// console.log('input', input);
// console.log('output', output);

console.log('They have the same data?', haveTheSameData);
// They have the same data? true
console.log('They are the same array?', areTheSameArray);
// They are the same array? true

Hello, thank you for quick reply.

I meant return cid just after for with var y loop.

If I return cid as a 2D array I get value of hundreds 100
If I return cid[0][1] I get value of hundreds 400 as it is original

I don’t know where you are seeing cid[0][1] returning a value of hundreds 400. I put console.log(cid[0][1]); all over the place (and for input and output) and always got the same response.

The response is always 1, because cid[0] is ["PENNY", 1.0] so cid[0][1] is 1.0 which gets simplified to 1. If you are getting something else then I’d need to see the exact code including your console.log statements.

Hello,

I moved a bit toward completing the project. Namely, It passes 2 out of 6 tests.

Output I get is:

{"status": "OPEN", "change": [["QUARTER", 0.5]]}

I am sure I assigned status and change as keys of output, so I don’t know why it shows them in quotes.
Except that I cannot see any other difference.

According to website it should be:

{status: "OPEN", change: [["QUARTER", 0.5]]}

It looks now like:

const coins = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
var names = [["ONE HUNDRED"], ["TWENTY"], ["TEN"], ["FIVE"], ["ONE"], ["QUARTER"], ["DIME"], ["NICKEL"], ["PENNY"]]
function checkCashRegister(price, cash, cid) {
var changedue = cash - price; const changedue2 = cash - price;
var coinsincash = []; var status = ["INSUFFICIENT_FUNDS", "CLOSED", "OPEN"];
var totalincash = 0; var output = {};

//creating array with values from "cid"
for (var z = cid.length - 1; z >= 0; z--) {
coinsincash.push(cid[z][1]);
totalincash += cid[z][1];}

const coinsincash2 = [...coinsincash];

//calculating how many coins will be in tray
for (var y=0; y < coinsincash.length; y++) {
while (changedue > 0 && changedue >= coins[y] && coinsincash[y] > 0) {
changedue = Math.round((changedue - coins[y] ) * 100) / 100
coinsincash[y] = Math.round((coinsincash[y] - coins[y]) * 100) / 100
}}

//calculating change in different coins
var diff = [];
for(var x=0; x < coinsincash2.length; x++) {
diff.push(Math.round((coinsincash2[x] - coinsincash[x]) * 100) / 100);
names[x].push(diff[x]);}

//creating array with coins when change is equal to money in tray
var names2 = []; for (var m = 0; m < names.length; m++) {names2.push(names[m]);}
var names2r = names2.reverse();

//creating array with change only coins
var names3 = [];
for (var m = 0; m < names.length; m++) {
if (names2[m][1] !==0) {names3.push(names2[m]);}}
var names3r = names3.reverse();

if(changedue2 > totalincash) {output["status"] = status[0]; output["change"] = [];}
else if(changedue2 == totalincash) {output["status"] = status[1]; output["change"] = names2r;} 
else if(changedue2 < totalincash) {output['status'] = status[2]; output['change'] = names3r;}
return output;
} 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]])

Does anybody has any idea?