Mini Linter Step #4 (Words of Story)?

Hi there,
I am new to the community. Just started my first course (JavaScript Beginners) 2 weeks ago.
I have no prior coding experience or knowledge.

After trying around I finally got some help from the walk through video.
But there is one thing he didn’t explain and maybe I missed it it somewhere. In the following code block I don’t understand the line “for (word of storyWords) {”
In particular “of”. I know what it does,but I haven’t come across this particular syntax yet or just forgot. What did I miss?
Thanks!
Tilo

const storyWords = story.split(’ ');
let betterWords = storyWords.filter(function(notNeeded) {
return !unnecessaryWords.includes(notNeeded);
})
let wordCounter1 = 0;
let wordCounter2 = 0;
let wordCounter3 = 0;

for (word of storyWords) {
if (word === ‘really’) {
wordCounter1 += 1;
} else if (word === ‘very’) {
wordCounter2 += 1;
} else if (word === ‘basically’) {
wordCounter3 += 1;
}
}

1 Like

Welcome to the forums!

I totally forgot about this too! I’ve noticed sometimes Codecademy will brush over something quick and then later it comes back to confuse the heck out of you haha - but I kind of like that because it gives an opportunity to check the documentation or find other sources to learn from… or get involved in the forums!

Seems like of is another way to get every element in an array, or any iterable object according to the MDN doc.

We can write the same thing multiple ways:

const array1 = [1, 2, 3, 4];

// using a traditional for loop and bracket notation:
for(let i = 0; i < array1.length; i++) {
	console.log(array1[i]);
}
// Prints
// 1
// 2
// 3
// 4

Since you’re in the iterators lessons, this might look familiar:

// Using the forEach() iterator function.
array1.forEach(element => { // We are telling each indexed value of array1 to take on the name element
	console.log(element);
});
// Prints
// 1
// 2
// 3
// 4

And then there’s of

// Using of keyword
for(element of array1) { // We are telling each indexed value of array1 to take on the name element
	console.log(element);
}
// Prints:
// 1
// 2
// 3
// 4

let myName = 'RyanSUP'; // Strings are itterable objects too!

for(letter of myName) { // We are telling each indexed value of myName to take on the name letter
	console.log(letter);
}
// Prints
// R
// y
// a
// n
// S
// U
// P

The difference between iterating with of and the forEach() function is forEach() will always return a value of undefined. I suppose it’s up to you as the programmer to decide which use is appropriate for whatever you’re writing.

Hope that clears things up and thanks for reminding me about this!

Here’s the MDN link:

1 Like

Fabulous! Thank you!
Greatly appreciated!
Tilo

1 Like