Why is the behavior of the for...of loop different from the traditional for loop within this function? It appears that the 'el' in the for...of loop does not behave the same as iterable[i] in the regular for loop?
var uniqueInOrder = function(iterable){
let unique = [];
for(let el of iterable){
let cur = el;
let next = el + 1;
if(cur !== next){
unique.push(cur)
}
}
return unique;
}
uniqueInOrder('AAAABBBCCDAABBB') // unexpected output
// Returns expected output
var uniqueInOrder = function(iterable){
let unique = [];
for(var i = 0; i < iterable.length; i++){
let cur = iterable[i];
let next = iterable[i + 1];
if(cur !== next){
unique.push(cur)
}
}
return unique;
}
uniqueInOrder('AAAABBBCCDAABBB') // ----> ["A", "B", "C", "D", "A", "B"]