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!

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 Practice map()

when I try my code out in my work space it works, but on the challenge task editor it fails, and I am sure my code works

Here is my code for this exercise

daysOfWeek.map(day => { console.log(abbreviatedDays = day.slice(0, 3)); })

const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let abbreviatedDays;

// abbreviatedDays should be: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
// Write your code below

daysOfWeek.map(day => {
  abbreviatedDays = day.slice(0, 3);

1 Answer

Owen Bell
Owen Bell
8,059 Points

The .map method returns a new array, consisting of transformations of values in the array on which it has been called. As such, the return value needs to be stored in the variable abbreviatedDays:

abbreviatedDays = daysOfWeek.map(day => func()); // Where func() is the transformation function

The function responsible for applying the transformation to your OG array's values should be a simple function returning a value corresponding to each array item with the transformation applied. In this case, each day needs to be sliced for 3 characters starting from the first character (0):

abbreviatedDays = daysOfWeek.map(day => day.slice(3, 0));

Because this is a very simple function, we are able to drop the braces denoting a code block and use implicit return to put this all on one line.

I'm not sure how you concluded that your original code is correct in Workspaces, as I copy-pasted it into a workspace myself and logged abbreviatedDays to the console, which returned "Sat" as the code challenge identified. This is also how I would expect your code to behave; your new array is not actually being stored in the abbreviatedDays variable, which is instead being updated for each iteration of .map to store the result of slicing the current array value - therefore storing the value "Sat" by the end of the method call.