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 Practice forEach in JavaScript Practice forEach forEach Practice - 2

Will Hunting
Will Hunting
13,726 Points

Why is my code not being accepted ?

My code produces Su Mo Tu We Th Fr Sa which was pushed to dayAbbreviations from days ? I have tested code on my personal work space and all is working fine?

app.js
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
let dayAbbreviations = [];

// dayAbbreviations should be: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
// Write your code below

days.forEach(day => dayAbbreviations = dayAbbreviations += day.slice(0,2) + ' ');

4 Answers

In your original code you created a 21 character string. You can verify this with:

console.log(dayAbbreviations.length) //logs 21

console.log(typeof dayAbbreviations) //logs string

The desired result is a 7 element array. Based on what you have above one way to do this is as follows:

days.forEach(day => dayAbbreviations.push(day.slice(0,2)));

Check the length of dayAbbreviations. It should be 7.

Will Hunting
Will Hunting
13,726 Points

Hi Kris, when using the console.log(dayAbbreviations) for my code mentioned above it displays: Su Mo Tu We Th Fr Sa which is not the correct format requested. This was achieved by using:

days.forEach(day => dayAbbreviations = dayAbbreviations += day.slice(0,2) + ' '); console.log(dayAbbreviations)

This challenge has requested that the format should be ["Su ", "Mo ", "Tu ", "We ", "Th ", "Fr ", "Sa "] which is in the format of an array and is achieved by using the below.

days.forEach(day => { const dayAbbreviation = day.slice(0, 2) + ' ' dayAbbreviations.push(dayAbbreviation) }); console.log(dayAbbreviations)

Is there a difference between the result from my format and the requested format?

Will Hunting
Will Hunting
13,726 Points

Thanks for explaining the above Kris.