My code works strange

My code works strange
0

#1

I’m doing the Exact Change exercise. All the tests are complete except one, and for this particular one case my code does crasy thing. After executing line 26 it changes value. This is the line:

toGive[toGive.length-1][1] += value[i][1];

It should add the value to toGive array, how can it increase the value? Value is constant in my case, I do not want to change it in any case.

If you have any ideas why it might happen, please let me know.

function checkCashRegister(price, cash, cid) {
   
  var value = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
  var change = Math.round((cash - price)*100)/100;
  var toGive = [];
  
  function findSum (arr) {
    var sum = 0;
    for (var i=0; i<arr.length; i++) {
      sum += arr[i][1];
    }
    return Math.round(sum*100)/100;
  }
  if (change == findSum(cid)) {
    return "Closed";
  }
  for (i = value.length-1; i >= 0; i--) { //loop through bills of cid from big to small
    if (value[i][1] <= change) { //if the bill is smaller than what we have to give back
      if (cid[i][1] > 0) { //if this bill is available in cid
        cid[i][1] -= value[i][1]; //remove the value of that bill from cid
        change -= value[i][1];  //remove the value of that bill from what is left to return
        if (toGive.length > 0 && toGive[toGive.length-1][0] === value[i][0]) { //if the bill is already in toGive array
          toGive[toGive.length-1][1] += value[i][1]; //add to the amount THIS LINE CHANGES VALUE
        } else {
          toGive.push(value[i]); //otherwise add it there
        }
        i++;                   
      } 
    }
  }
  if (change > 0) {
    return "Insufficient Funds";
  } else {
    return toGive;
  }     
}

#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

This line is doing what its meant to do … add value [i][1] to whatever value toGive[toGive.length-1][1] is
if you want to add (insert) the value … value[i][1] to the toGive array you would … toGive.push(value[i][1]) … note this would only push the number part … probably better push(value[i])
anyway to finsh += means to add together two numbers … to add something to an array we have push() … unShift() (which add things to the beginning of an array)


#4

thank you for the answer, but you didn’t get the problem at all. i see the difference between += and push. let me try to explain again.
i did x += y, and this should change x, but instead it changed both x and y. it happened because of this line:
toGive.push(value[i]);
i was advised to change it to
toGive.push(value[i][1], value[i][2]);
and now it works.

thank you for trying to help anyway. you were the only one to answer. it’s the second time i try to ask help on the forum, and you are the only one to answer. it’s a pity that asking for help here doesn’t really work.


#5

ok had another look at your code and ran and debugged it with this calling function
checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]])

Result should be > [ ‘TWENTY’,60, ‘TEN’, 20, ‘FIVE’,‘15.00’, ‘ONE’, ‘1.00’,‘QUARTER’, ‘0.50’,‘DIME’, '0.20 ‘PENNY’,0.04 ]
yours returns insufficient funds … which obviously is wrong … but your code is working properly just with a couple of minor flaws.

  1. if (value[i][1] <= change) when debugging i found that for this code with the above function call it was returning $80 in TWENTYs … as there is only $60 in TWENTYs in the till this is one error …after looking at this going to be tricky to sort out without adding another array of cash drawer values as you change both value and cid each loop … this … if (value[i][1] <= change) needs also to able to check if there is sufficent TWENTYs in the till not just if value[i][1] is <=change
  2. this is the one that breaks on this … we get down to the last 4 cent and when it reaches change left is two cent and runs if (value[i][1] <= change) value[i][1] is reading as 0.02 but change is showing as 0.01999999999999487 and instantly fails you as having insufficient funds so you need to round your numbers
    Overall two not major problems to sort out and your good to go … hope this helps

#6

the code works. it was fixed the same day with the help of a friend. the 1st problem was solved in the way i’ve described in the previous comment. the 2nd problem was fixed just by changing
value[i][1] <= change
to
value[i][1] <= Math.round(change*100)/100