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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Questions about Practice 6 for forEach loops

First of all, please keep this practice courses coming. I've learned so much about forEach in a way that I didn't in the course this was based on and while I don't think it's practical to do do this for every concept I think in this case it's been very useful and worthwhile to do :)

Anyway, I've passed the challenge for Practice 6, however in the same way that James did, I put the code into separate files before copying them back to the code challenge. I used console.log to log output and check I was getting the strings I should have done.

So in this challenge, I did the same but although the challenge passes I'm not getting anything returned to the console....

Below is what I attempted.....

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

// filteredColors should be: ['#FFE0F4', '#FF2D2D', '#F7FFEC']
// Write your code below
colors.forEach( startsF => {

  if(startsF.startsWith("#F", 1) === true) {
     filteredColors.push(startsF);
  }

});

But even with the solution to the challenge I would thought it would return something to the log. I attempted to log both to the console.

/* omitting the solution.... */ 
console.log(filteredColors);

1 Answer

Steven Parker
Steven Parker
231,236 Points

You wouldn't get any matches with those arguments to "startsWith". But you could either change the first argument to just "F" or change the second argument to 0 (or leave it off) and you will.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

It works... but I should still be getting some output to the console, shouldn't I? I mean in a local file rarther than just the code challenge!

Steven Parker
Steven Parker
231,236 Points

You won't get console output from a challenge because the code doesn't run in your browser. To see the console output you'd need to run the code in the workspace or an IDE or sandbox tool.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

That's what I'm saying. I'm using node to try to pull up the output from console.log. Sorry, I should have been clearer... I'm trying to ascertain if there's anything to do with the startsWith() method that's causing the call to console.log to not return any output to the log. I can't see any reason it would, but it's not happening. :-)

Steven Parker
Steven Parker
231,236 Points

I tried your code (with fixes) in a node workspace and got this console output:

treehouse:~/workspace$ node foreach6                                                                
[ '#FFE0F4', '#FF2D2D', '#F7FFEC' ]      

Do you get something else? Or are you using something other than the workspace?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yea, it's Node, I'm using on my file system, a file called foreach6.js which I simply call with node foreach6

I should get what you're getting but I literally get nothing. I'll try in a workspace later. (It's getting late here ;) )