Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Array Iteration Methods Array Manipulation Remove Array Items with filter()

Michael Caveney
Michael Caveney
50,144 Points

Practice filter()

This has to be a bug, right? The following code isn't passing the challenge:

const years = [1989, 2015, 2000, 1999, 2013, 1973, 2012];
let century20 = years.filter(year => year >= 2000); 

7 Answers

Seth Kroger
Seth Kroger
56,414 Points

These challenges are a little extra picky because you're required to put your solution below the "Write your code below" comment. (Hence why the variable is declared with let instead of const.) Otherwise Matt Brock's answer would pass.

const years = [1989, 2015, 2000, 1999, 2013, 1973, 2012];
let century20;

// century20 should be: [1989, 2000, 1999, 1973]
// Write your code below
century20 = //...

Crikey! That's a persnickety little bugger.. Thanks Seth!

Jasmine Martin
Jasmine Martin
14,307 Points

I like the above answer .. but if the array had 1800s etc. it wouldn't work. Does anyone else have a way they would have solved this.

My answer:

const years = [1989, 2015, 2000, 1999, 2013, 1973, 2012];
let century20;
century20 = years.filter( year => year.toString().slice(0, 2) == '19'|| year == 2000 )

I'm getting the same error. Strange. Your code is actually filtering for years in the 21st Century (year >= 2000), but it still doesn't work when I changed it to filter for 20th Century years. This still breaks and returns the "" error:

const years = [1989, 2015, 2000, 1999, 2013, 1973, 2012];
let century20 = years.filter(year => year <= 2000);
Michael Caveney
Michael Caveney
50,144 Points

Yup, doing so passes the challenge. It's just personally annoying to me to not modify the variable declaration to include the filter since it's a bit more succinct.

const years = [1989, 2015, 2000, 1999, 2013, 1973, 2012]; let century20; century20 = years.filter(year => year === 2000 || year > 2000); console.log(century20);