Comparing strings in 2d array

My problem is comparing the cid[z] to the result[z][0] (lines 50-54). I’m trying to only have one list of [‘PENNY’, 0.5] in theresult array, but my attempts to compare it to the [‘PENNY’, 0 ] in the cid array are failing.

Thanks!


function checkCashRegister(price, cash, cid) {
  var change = cash - price;
  var result = []; 
  var response = ''
  var val = [["ONE HUNDRED",100],["TWENTY",20],["TEN",10],["FIVE",5],["ONE",1],["QUARTER",.25],["DIME",.1], ["NICKEL",.05],["PENNY",.01]];
 var total = 0, i;
 for (i in cid){
   total += cid[i][1];
  total = Math.round(100*total)/100;
  }
  cid = cid.reverse();
  if(change == 0){
      response = 'CLOSED';
  } else if (change > total){
      response = 'INSUFFICIENT_FUNDS';
  } else{
      var x;
      for (x in val){
        var currentCid = cid[x][1];
        var currentVal = val[x][1];
          var y = 0;
          if (change >= currentVal && currentCid != 0){
            while (currentCid != 0 && change >= currentVal){
              y += currentVal
              change -= currentVal;
              change = Math.round(change*100)/100;
              currentCid -= currentVal;
              }
            y = Math.round(y*100)/100;
            result.push([val[x][0],y]);
            cid[x][1] = Math.round(currentCid*100)/100;
          }
        }
        if (change == 0){ response = 'OPEN'; } 
        else { 
          response = 'INSUFFICIENT_FUNDS';
          result = [];
        }
  }
  var zero = 0;
  for (x in cid){
    if (cid[x][1] == 0){
      zero +=1
    }
  }
  if (zero == 9){
    response = 'CLOSED'
    cid = cid.reverse();
    var z;
    for (x in cid){
      if (cid[x] != result[x][0]){
        console.log(cid[x][0])
        console.log(result[x][0])
        result.push(cid[x]);
      }
    }
  }

  console.log('return:', {status: response, change: result})
  return {status: response, change: result}
}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

if (cid[x] != result[x][0]){

you can’t compare a string and an array.
cid[x] is an array, result[x][0] is a string
whatever you want to do with them, the comparison simply will not work.

Ok I said that wrong. I’m trying to compare a string element of one array to a string element of another array.

if one of the elements you are comparing is an array, but you want to compare the string inside it…
do you remember how to access elements inside an array?