Dominofoe
Dominofoe

Reputation: 603

Why is my function return different than my console.log() in Chrome browser

I am completely stumped. I don't even know if I am asking this question correctly. Here is my repl>it http://repl.it/BAQ0/151 My console.log and my return is what I expect here. But when I place the code into my code editor window for my training I get [array[2]] from console.log and my code will not pass. I have added the test that is not passing below the code in the repl>it. What am I missing? Thanks!

var values = {
'ONE HUNDRED':10000,
'TWENTY':2000,
'TEN':1000,
'FIVE':500,
'ONE':100,
'QUARTER':25,
'DIME':10,
'NICKEL':5,
'PENNY':1
};

var change = [];
var array = [];

function multValuesBy100(arr) {
  //return arr with larger values
  for(var i = 0; i < arr.length; i++) {
    arr[i][1] *= 100;
  }
  return arr;
}

function giveChange(changeDue,arr) {
    var rem = changeDue;
    for(var key in values) {
        var n = Math.floor(rem/values[key]);
        if(n !== 0) {
            change.push([key, ((n*values[key])/100)]);
        }
        rem = changeDue % values[key];
    }
    console.log(change);
    return change;
  }

function changeInDrawer(cid) {
  //returns a number representing the amount of change in the drawer
  var result = 0;
  for(var i = 0; i < cid.length; i++) {
    result += cid[i][1]*100;
  }
  return result;
}

function drawer(price, cash, cid) {
  price *= 100;
  cash *= 100;

  var changeDue = cash - price;
  // Here is your change, ma'am.
  if(changeInDrawer(cid) < changeDue) {
    return "Insufficient Funds";
  } else if(changeInDrawer(cid) === changeDue) {
    return "Closed";
  } else {
    return giveChange(changeDue, cid);
  }
}

// Example cash-in-drawer array:
// [['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]]

drawer(19.50, 20.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]]);


//------------------ Test below -----------------//

/*assert.deepEqual(drawer(19.50, 20.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]]), [['QUARTER', 0.50]], 'return correct change');return correct change: expected [ Array(2) ] to deeply equal [ [ 'QUARTER', 0.5 ] ]*/

Upvotes: 0

Views: 120

Answers (1)

fuyushimoya
fuyushimoya

Reputation: 9813

Probably because you define the change out of giveChange, so if the tester run something else before, there's probably when you run the current test, the change has already have some value which is pushed by previous tests.

Try declare that inside giveChange, so it would be like :

function giveChange(changeDue,arr) {
    // Ensure we use a pure array.
    var change = [];
    var rem = changeDuem;
    for(var key in values) {
        var n = Math.floor(rem/values[key]);
        if(n !== 0) {
            change.push([key, ((n*values[key])/100)]);
        }
        rem = changeDue % values[key];
    }
    console.log(change);
    return change;
  }

Another point is [array[2]] is the unexpanded format when chrome log object or array which contains non-primitive types. Repl.it should just expaned it for user to read easier, the result should be the same.

Upvotes: 1

Related Questions