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

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

Code challenge doesn't accept my code

Hi guys, I wrote this code:

days.forEach(day => {

  let character2 = `${day.charAt(0)}${day.charAt(1)}`;
  dayAbbreviations.push(character2);

});

however, the program says illegal character. What should I do?

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 => {

  let character2 = `${day.charAt(0)}${day.charAt(1)}`;
  dayAbbreviations.push(character2);

});

4 Answers

Eitan Peles
Eitan Peles
4,453 Points

Where is the error? It worked just fine here in Visual Code. [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ]

PS: Another way you could do it is using map, like the following: dayAbbreviations = days.map(day => day.slice(0,2));

Try using "substring" instead. That is: let character2 = day.substring(0, 2). Substring extracts the characters from a string between two specified indices.

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

Many thanks for your answers. I used split method and it works, but my code is correct. My only question is why the first version is wrong in code challenge and good in workspace

Ivan Villarroel
Ivan Villarroel
4,010 Points

Hi, i think the problem is that you use Jquery. i think the courses and practice is mostly in vanilla js try to rewrite you code into vanilla js and i think is should work fine.

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

Thanks Ivan, but I don't think it's Jquery. I use template literals and the code runs in workspace but here in the code challenge part doesn't work.