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 Combining Array Methods Method Chaining

filter and map in JS

Could someone help me on this Challenge. The question is: Using the filter and map methods on the years array, create an array of display strings for each year in the 21st century (remember: the 21st century starts with the year "2001"). Each display string should be the year followed by "A.D." See the comments below to see the correct result. Store the new array in the variable displayYears. I don't know, what I made wrong. Thanks everyone for help, regards, Jan.

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

const displayYears = years
    .filter(year => year > 2000)
    .map(year => year + 'A.D.');

// displayYears should be: ["2015 A.D.", "2013 A.D.", "2012 A.D."]
// Write your code below

I just managed it myself by having a closer look at the error message, first I declared "displayYears" twice ... and the 'A.D.' ... I forgot a space when concatenating it ... so by dropping the "const" in front of "displayYears" and by writing ' A.D.' ... instead of 'A.D.' ... everything works out fine ... but if I wouldn't have posted my question, I guess I would not have tried harder, but I didn't want to look like an idiot ... so I can just encourage anyone to ask questions ...

6 Answers

Cooper Runstein
Cooper Runstein
11,850 Points

You declared the same variable "displayYears" twice, once with const, once with let. Also, you'll likely need a space to separate year and "A.D.".

thx Cooper ...

Kate C
Kate C
13,589 Points

Why can't I use displayYears = years .filter(year => year > 2000) .map(year => year.join("A.D."); console says no such function, why?

displayYears = years.filter(yr => yr > 2000).map(yr => ${yr} A.D);

I did it like this, unfortunately, I have found that code challenges tend to give errors when using backticks.

Henry Blandon
.a{fill-rule:evenodd;}techdegree seal-36
Henry Blandon
Full Stack JavaScript Techdegree Graduate 21,521 Points

Lewis Sheeran the expression I wrote is using back ticks which it converts the output to a string. It is called Template Literals. If you want to see the type it is returning, just change the code like this.

displayYears = years
    .filter(year => year > 2000)
    .map(year => typeof (`${year} A.D.`));


The return should look like:

[ 'string', 'string', 'string']

Can someone please explain to me why we aren't calling .toString within the map method? How do the years get converted to a string format when the filter initially places the values in the new array as integers?

The solutions posted above work (thanks) but I still don't understand how the year values themselves are automatically becoming strings.