Here is my code:
uniq = function(array) {
var result = [];
for (var i = 0; i < array.length; i++) {
console.log(array[i], " is in ", result, (array[i] in result));
if (!(array[i] in result)) {
result.push(array[i]);
} else {}
}
return result;
};
uniq([1, 2, 3, 3, 4, 4]);
Output is:
1 ' is in ' [] false
2 ' is in ' [ 1 ] false
3 ' is in ' [ 1, 2 ] false
3 ' is in ' [ 1, 2, 3 ] false <--- HUH?
4 ' is in ' [ 1, 2, 3, 3 ] false
4 ' is in ' [ 1, 2, 3, 3, 4 ] true <--- now it works correctly
Could someone clue me in please? Why is (!(array[i] in result)) not evaluating to true the first time but is the second time?