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?
> 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.
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?
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.
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.
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.
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)
}
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!
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