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

shirshah sahel
shirshah sahel
10,035 Points

Using the filter and map methods on the years array, create an array of display strings for each year in the 21st centur

Wasn't able to pass this challenge. will appreciate the help. https://teamtreehouse.com/library/javascript-array-iteration-methods/combining-array-methods/method-chaining

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

const displayYear= years
.filter(year=>year===2001);
.map(year=>year +1);
console.log(incrementedYear);

2 Answers

Matthew Long
Matthew Long
28,407 Points

You're not super far from the solution, but you have a couple errors, and you're not doing exactly what the challenge is asking.

First, you have a semicolon interrupting the method chain. Remove the semicolon before the map() method. Next, you are only checking for the year to be strictly equal to 2001. Whereas there is no year equal to 2001. The challenge wants you to check for years greater than 2000. Lastly, you aren't mapping the new array the way the challenge is wanting. For example, each element in the new array should be "<year> A.D.".

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

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

Good luck and happy coding! :smile:

Owa Aquino
Owa Aquino
19,277 Points

The problem I got here is how would I convert the 'displayYears' into an array... because it was not initialized as an array by default.... 😅

I figure it out when I have read this answer. I just have to assign the 'years' directly to the 'displayYears' so it'll become an array.. hahaha 🤦‍♂️

Thanks!

shirshah sahel
shirshah sahel
10,035 Points

Thanks alot for providing the answer. it helps me to learn better.