FAQ: Code Challenges: Intermediate JavaScript - justCoolStuff()

Because there is no return in the callback function.

1 Like

Thank you, I saw an earlier post that you had answered the same question for too :slight_smile:

1 Like

Does the essentially mean undefined then?

No. It means an empty array. An array is defined by its structure, which is a type of object. It’s contents are undefined, though.

Been taking the course and every time I have trouble all I need to do is find your explanations. You’re awesome, thank you very much!

1 Like

A post was split to a new topic: How does item fit in exactly in the code

Item refers to whatever is passed into the “isInSecondArray” function.

this means, the “item” parameter has implicit reference to the filter() methods element parameter?

Hi everyone,

first time poster!

I know that using the forEach method may not be the most elegant solution, but I’m wondering why this return undefined.

function justCoolStuff(arr1, arr2) { function compareElements() { arr1.forEach(element => arr2.includes(element)) } arr1.filter(compareElements); };

Thanks in advance for the help!

As often asked, what is the first thing we learn about .forEach()?

I’m guessing that it doesn’t mutate the original array?

Not by default, but it can if we specify the third positional argument in the callback function.

The first thing we learn is that there is no return value. Consider how that effects your code above.

Oh that makes sense, because if I’m calling filter on a function that does not return anything, than nothing is going to happen. Is that right?

1 Like

Pretty much. Just remember to not expect a return from .forEach and study ways that it can be useful, one such way being to mutate its own array, which may or may not ever come up unless you explore it.

Also remember that an arrow function with braces is just a standard function expression that requires a return (but lacks the this and arguments properties).

Your code above will work if you simple return the filter. Your comparison function can written into the filter.

arr1.fillter(x => arr2.includes(x))
const justCoolStuff = (arr1, arr2) => {
  let inBothArrays = []
  arr1.filter(e => arr2.includes(e) ? inBothArrays.push(e) : false)
  return inBothArrays
}

I used false to represent any value that is not in both arrays. Assumed that nothing would be added to inBothArrays and happened to be right :sweat_smile:

Isn’t that a little bit of overkill?

    return arr1.filter(e => arr2.includes(e))

does the same thing.

Yes, it certainly is overkill but I’m sure my solutions will get neater and neater with time. The lazier I can get the better (as long as others can understand what I’m doing).

What’s to understand? the incorrect use of filter and ternary expressions?

To be fair, one should explain that there are cross-purposes in your code; in other words, the desired result can be obtained in either way.

const share = [];
for (let e of arr1) {
    if (arr2.includes(e)) {
        share.push(e)
    }
}

That is a filtering action.

I’m not in total agreement with the execution of statements in a ternary, so haven’t gone down that road, as you have. Personally, whether it works or not, I do not favor ever issuing statements in a ternary, period. It should be returnable, or capable of being expressed further. It is an expression. Expressions do not contain statements. This is the main incorrectness that I was harboring on in my initial reaction, without offense or derision of code. I’m only focused upon usage that makes sense and is portable.

1 Like

I’m able to get output like [ ‘fruit-by-the-foot’, ‘skateboards’, ‘my room’ ] but what is problem …can you please explain this

function justCoolStuff(a,b){
const sameWord=[ ];
for(let i=0;i<a.length;i++){
for(let j=0;j<b.length;j++){
if(a[i]===b[j]){
sameWord.push(a[i])
}
}
}
return sameWord;
}

What kind of message are you getting?