Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Array Iteration Methods Combining Array Methods Chaining Array Methods

Further explantion about the code in the teacher notes

Hi.

const numbers = [1, 1, 2, 3, 4, 3, 5, 5, 6, 7, 3, 8, 9, 10];

const unique = numbers.filter(function(number, index, array) {
  return index === array.indexOf(number);
});

console.log(unique); // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I'm a abit confused with this example so I'll be happy if someone can tell me if im right about this: I understand and tested the beahvir of "indexOf" which returns the first element that he encounter, in the first iteration "index" is 0 and the return statament return 0 === 0 and that's true so that number gets added to the unique numbers array.

in the second iteration, "index" is now 1 and the return statment evulate to 0, so we basically have 1===0 which is not true so the duplicate 1 is not added.

I added a console.log to see exactly whats happening in each iteration:

const numbers = [1, 1, 2, 3, 4, 3, 5, 5, 6, 7, 3, 8, 9, 10];

const unique = numbers.filter(function(number, index, array) {
  console.log(`the index is now ${index} and  result of the indexOf method is :${array.indexOf(number)}`);
  return index === array.indexOf(number)

});

console.log(unique); // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

THE OUTPUT:

the index is now 0 and  result of the indexOf method is :0                       
the index is now 1 and  result of the indexOf method is :0                       
the index is now 2 and  result of the indexOf method is :2                       
the index is now 3 and  result of the indexOf method is :3                       
the index is now 4 and  result of the indexOf method is :4                       
the index is now 5 and  result of the indexOf method is :3                       
the index is now 6 and  result of the indexOf method is :6                       
the index is now 7 and  result of the indexOf method is :6                       
the index is now 8 and  result of the indexOf method is :8                       
the index is now 9 and  result of the indexOf method is :9                       
the index is now 10 and  result of the indexOf method is :3                      
the index is now 11 and  result of the indexOf method is :11                     
the index is now 12 and  result of the indexOf method is :12                     
the index is now 13 and  result of the indexOf method is :13          

i hope this will help others to understand..