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 - 6

Using forEach, iterate over the colors array and store the hex colors that start with the letter F (ex. #FF0000) in the

How come this doesn't work and why is the solution method different from the initial challenge?

app.js
const colors = ['#87E2D0', '#559F4D', '#FFE0F4', '#7E7E7E', '#FF2D2D', '#F7FFEC'];
let filteredColors = [];

// filteredColors should be: ['#FFE0F4', '#FF2D2D', '#F7FFEC']
// Write your code below
colors.forEach(color => {
  if(color === 'F' ) {
    filteredColors.push(color);
  }
});

2 Answers

You are comparing the entire hex color to F. None match therefore none are added. You could compare the second character of color to F by using color[1].

Henry Blandon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Henry Blandon
Full Stack JavaScript Techdegree Graduate 21,521 Points

Hello Dionte Barnes. Here is my code. You are really close. when you compare color===F, the loop is going to compare #87E2D0 ===F, then #559F4D=== F and so on; therefore, it will never be equal to F. You need to use charAt(index). Then index in this case will be the index of the letter you want to compare, which is the second letter or index 1. For example: in the color #87E2D0, index 0 is '#' index 1 is' 8'. in the color #FFE0F4 index 0 is '#' index 1 is 'F'.
I know is late to answer this question, but someone else might come in here looking some information.

colors.forEach(color=> (color.charAt(1)=== 'F') && filteredColors.push(color))