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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Struggling with method chaining

I'm really stuck with this one.

I think I've got filter right but even after looking at the documentation I'm still at a loss for how to use the correct map method.

Help! :)

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

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

 displayYears = years
   .filter( century => century < 2001)
   .map((years){return years;});

console.log(displayYears + " A.D.");

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

You are very close:

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

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

 let displayYears = years
    .filter(x => x >= 2001)
    .map(x => x + " A.D.");

console.log(displayYears);
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Thanks... this worked.

So what's happening I think is I got the wrong condition but I think it sank in with the map method yet :)

So with map is it...

Parameter.... callback.... string. But the last part goes in the method rather than the final console.log? Although I've tested it and that seems to work too.

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

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

displayYears = years
    .filter(x => x >= 2001)
    .map(x => x);

console.log(displayYears + " A.D.");
Ari Misha
Ari Misha
19,323 Points

hiya Chris! Well its pretty straightforward imo. We need to use map and filter in order to display the array in this format: displayYears = ["2001 A.D.", "2013 A.D.", "2012 A.D."]. Here is how your code should look like ( i dunno how to explain it):

...

displayYears = years.filter ( (year) => year >= "2001").map ((year) => `${year} A.D.`)