FAQ: Code Challenges: Intermediate JavaScript - justCoolStuff()

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “justCoolStuff()” exercise from the lesson “Code Challenges: Intermediate JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise justCoolStuff()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi,
When i tried this ;
<const justCoolStuff = (arr1,arr2) =>
arr1.filter( item => arr2.includes(item))>
everthing goes well but When i added extra ‘{}’ ;
<const justCoolStuff = (arr1,arr2) =>
{arr1.filter( item => arr2.includes(item))}>
return undefined, Why something goes wrong? this is also arrow function?

4 Likes

When curly braces are used, the function must have an explicit return

const justCoolStuff = (arr1,arr2) => {
  return arr1.filter(item => arr2.includes(item));
};
14 Likes

I just don’t get where that “return false” came from! Can someone explain to me, please?

We don’t have enough context to answer your question. Perhaps it would be something like this?

const justCoolStuff = (arr1,arr2) => {
  let matches = arr1.filter(item => arr2.includes(item));
  return matches.length && matches || false; 
};

If there are no matches the length of the array will be zero, which is falsy so the last operand (false) is returned, else the array is returned.

2 Likes

Could someone please help me understand the actual logic behind this code:

const justCoolStuff = (firstArray, secondArray) => {
const isInSecondArray = item => {
for(let i = 0; i<secondArray.length; i++){
if (secondArray[i] === item) {
return true
}
}
return false
}
return firstArray.filter(isInSecondArray)
};

const coolStuff = [‘gameboys’, ‘skateboards’, ‘backwards hats’, ‘fruit-by-the-foot’, ‘pogs’, ‘my room’, ‘temporary tattoos’];

const myStuff = [ ‘rules’, ‘fruit-by-the-foot’, ‘wedgies’, ‘sweaters’, ‘skateboards’, ‘family-night’, ‘my room’, ‘braces’, ‘the information superhighway’];

console.log(justCoolStuff(myStuff, coolStuff))

This could be written in one line…

 > const y = (a, b) => a.filter(x => b.includes(x))
 > y(
['gameboys', 'skateboards', 'backwards hats', 'fruit-by-the-foot', 
'pogs', 'my room', 'temporary tattoos'],
['rules', 'fruit-by-the-foot', 'wedgies', 'sweaters', 'skateboards', 
'family-night', 'my room', 'braces', 'the information superhighway']
)
<-(3) ["skateboards", "fruit-by-the-foot", "my room"]

Granted, that is not what this lesson is focused upon. I’ve also leveraged a built-in method, includes which is what the exercise is intended to emulate, albeit as a callback function. Look closely at your code and see what it is doing. Now in retrospect you will see the includes aspect, and reason how this iterator proves very useful.

4 Likes

I’m wondering why the item .filter() is passing to the test function is not explicitly passed. In the following code I passed the thingsInArray2 test function to .filter() assuming that the item parameter would be implicitly passed and it works.

But the Array.prototype.filter syntax information says that the currentValue being passed to the test function is required.

function justCoolStuff(array1, array2) {
  function thingsInArray2(item) {
    for (i = 0; i < array2.length; i++) {
      if (array2[i] === item) {
        return true;
      }
    }
    return false;
  }
  return newArray = array1.filter(thingsInArray2);
}

Is there a way to explicitly pass the currentValue from .filter() to thingsInArray in the context of my code above?

Consider the following.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9];

const isOdd = x => !! (x % 2);

only_odd = array.filter(isOdd);

console.log(only_odd)    //  [1, 3, 5, 7, 9]

The above demonstrates a predicate function as would be useful to the filter method. isOdd is the callback. Notice there is no loop (iteration) in the function? That is because filter is the iterator. There is no need of a loop inside the callback.

function justCoolStuff(array1, array2) {
    isInArray2 = item => array2.includes(item);
    return array1.filter(isInArray2);
}

As we can see, there is no need to explicitly pass any argument. The filter iterator looks after that in the background.

2 Likes

Yes, I can see in your example that there is no need for a loop inside the callback. That’s because filter is iterating over array1 and includes is iterating over array2.

In was intentionally avoiding includes as an exercise. That is why I created my own iterator.

I could tell that there is no need to explicitly pass an argument to filter for the callback to use but I was wondering if the is a way to do it.

Try it and see. The only problem is to do that, the callback function will have to be inside the argument of filter so you can name the parameter. filter uses the parameter we specify in the callback function.

Hi!
For me a “return false” part became a little bit tricky.
This code works properly:

function justCoolStuff(firstArray, secondArray) {
      function isInSecondArray(item){
            for(let i = 0; i<secondArray.length; i++){
                  if (secondArray[i] === item){
                        return true
                  }
            }
            return false 
      }
      return firstArray.filter(isInSecondArray)
}

but if we change code like this:

function justCoolStuff(firstArray, secondArray) {
      function isInSecondArray(item){
            for(let i = 0; i<secondArray.length; i++){
                  if (secondArray[i] === item){
                        return true
                  } else return false;
            }
             
      }
      return firstArray.filter(isInSecondArray)
}

the outcome becomes an empty array.

Could you help me please to figure out why it happens? Shouldn’t this code work the same way?

1 Like

Inside a loop this will terminate the function immediately.
.
Aside

Have you read any other posts in this topic? Being as this is a lesson on iterators, it follows we would make every effort to use them. filter and includes are two that serve our needs very well in this problem.

1 Like

Yes, sure! This buzzing issue was just bothering me a little. Thank’s for help!

1 Like

Can somebody please help make me understand the function declaration below. I don’t understand the logic of item in function isInSecondArray. As the arguments for function justCoolStuff are firstArray and secondArray, where does the argument for item come from? If item automatically means each element of firstArray, how does that happen? I’m struggling to see that.

function justCoolStuff(firstArray, secondArray) {
function isInSecondArray(item){
for(let i = 0; i<secondArray.length; i++){
if (secondArray[i] === item){
return true
}
}
return false
}
return firstArray.filter(isInSecondArray)
}

3 Likes

Looking for the definition of a variable often means tracing it back to the parameter list of a function.

def foo (bar):
    pass

Above, bar is the parameter, a variable and hence, just a name that is locally meaningful because it points to an argument that corresponds to that position in the argument list.

>>> def foobar (a, f, b):
    return '{} {} {} = {}'.format(a, f, b, a + b)

>>> foobar(6, 'plus', 7)
'6 plus 7 = 13'
>>> 

a, f, and b are positional arguments by virtue of what they stand for. Three arguments in the call, in a specific sequence.

Hello! I just completed this exercise and was wondering if there is an argument in real-life practice to use .filter() and .includes() over a nested loop. Is there a preferred method? Is one better than the other? Or was this an exercise simply to get us familiar with using .filter()?

I am attaching a photo below of my code for context. Thank you!

2 Likes

Hi, I sed the following code and get an error that there is an unexpected token ‘===’ in the if loop. Can someone please help me out? I’m trying to solve this problem using loops

const justCoolStuff = (arr1,arr2) => {
const newArr = ;
for (let i=0; i < arr1.length; i++) {
for (let j=0; j < arr2.length; j++) {
if(arr1[i]) === arr2[j]){
newArr.push(arr1[i]);
}
}

}
return newArr;
}

stray ) before the operator.

1 Like

Thanks! I had the same question.