Please help me with this

You are confusing me with this.
First thought was to try using x instead of string inside your for-loop.
That would make sense only, if string was an array, though.
There seems to be a misunderstanding of the “for(let… of …)” structure.
You take the “let … of …” when you want to run through an array of various elements. each cycle of the loop takes the next item until done with the array.
In your example “let x of string” would say there is an arry called “string” and for each cycle you take one of its elements and assign it to “x”.
You do, however, call the function with only one “String” - element so that doesn’t make any sense here.
Take out the line “for…” and after “return string;” change to " } else {"
and you should be fine with what you get as result.
(I suppose without having tested it).

1 Like

Hi Andreas,
All I wanted to do is…

•Create a function that goes through a string and looks for a specific word, if it does find it, it will push that word into an empty array.
• And after that the function will print the length of that array

I read your reply and understood where I went wrong

Can you please look at my plan and guide me?

Hey there, looks great!

The formatting is a little hard to read in your screenshot, so I’ve rewritten your code snippet below to make it a bit easier. I’ve also renamed the arr parameter to sentence.

var txt = 'Hi bad';
var bads = 'bad';

function lb(sentence, badWord) {
    let badWrds = [];
    if (sentence.includes(badWord) {
    } else {
        return sentence;

console.log(lb(txt, bads));

Two questions you can ask yourself before you work on it some more:

  1. If you’re only interested in the number of bad words, why not just store a number and add to it each time you find one, rather than push 'bad' to an array each time and then get the array’s length?

  2. Currently, your function will only ever find one bad word. How can you make it find more than one?

Good luck! Looking forward to seeing where you take it from here.

1 Like

Yes I’m only interested in the no of ‘bad’ words and I actually didn’t think of just adding +1 to the array instead of pushing a word and then printing the array length. Thank you for your guidance there.

Your second point has me really wondered and I will try to write a code for that.

Also, I had to write the above code in a browser because some buttons are not available in code playground. Please add them.

Great! If you just want the number of bad words, you probably don’t need an array at all. You can just have something like this:

let badWrds = 0;
if (sentence.includes(badWord) {

Good luck!