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 trialMichael Caveney
50,144 PointsPractice 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
56,414 PointsThese 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 = //...
Matt Brock
28,330 PointsCrikey! That's a persnickety little bugger.. Thanks Seth!
Jasmine Martin
14,307 PointsI 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 )
Keith McGill
Full Stack JavaScript Techdegree Student 6,852 PointsAbsolute simplest solution for this particular array:
century20 = years.filter(year => year < 2001)
Matt Brock
28,330 PointsI'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
50,144 PointsYup, 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.
shahid manzoor
5,384 Pointsconst years = [1989, 2015, 2000, 1999, 2013, 1973, 2012]; let century20; century20 = years.filter(year => year === 2000 || year > 2000); console.log(century20);
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Pointssimplest solution is to use the && operator.
century20 = years.filter(year => year>= 1900 && year<=2000)
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 PointsDoron Geyer
Full Stack JavaScript Techdegree Student 13,897 Pointsthe expected answer is years less than 2000. Your solution is correct, but you are searching years greater than 2000