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 Challenge 4 Solution

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

Dont I need to use character.value instead ?

In order to get the value of an array or a string, I thought that the syntax was always:

if(character.value !== "L) {

noel.push(character);
}

How come I don't need to pass the ".value" and when do I need to use the ".value"?

2 Answers

Blake Larson
Blake Larson
13,014 Points

character is the value in this instance. He takes the array of characters and uses the forEach() array method. It takes the placeholder variable of character (singular) which is the current iteration of the loop through all the characters. So in human words it is for each character in characters ... do this (code) and the character variable is available each time.

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Hamzah Iqbal in this specific case

character refers to each individual letter in the array

//characters array
['A','B','C',....]
//array[0] = 1 character 'A'

so your first iteration of characters.forEach loops over the entire characters array. And each iteration it checks the character variable. So loop one character will be 'A' loop 2 it will be 'B' and so on. So it keeps running until and adds each letter to the new array except as per the condition letter 'L'

To make this less confusing you can change the word character to letter

characters.forEach(letter => {
   if(letter !== 'L'){
      noel.push(letter);
   }
});