Why is this 2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
24th Aug 2017, 7:36 AM
// chris
// chris - avatar
2 Answers
+ 2
arrayName.filter(callbackFunction(){ //... return booleanResultTrue; } is a method for the array object that return the element only if that's a true condition for his callback. What means: the boolean false is equal to 0, boolean true is every number not equal to 0; said so, your callback is supposed to be a "filter" for your array so it will return a result only if a condition is true -> your var a = c = [1,2,3,4] so suppose to write var newArray = a%2-1; return newArray; inside the callback. what will newArray contain? newArray = [0, -1, 0, -1] because a%2 is [1,0,1,0] (the rest for each element of your array a). Now the method filter will pass to your var b only the elements of c that will return a true condition, so the elements that correspond to newArray[1] and newArray[3], so b = [2, 4], so b[0] = 2. if you try to write only return a%2 your var b will be equals to [1,3]
24th Aug 2017, 8:46 AM
Michele Virgilio
Michele Virgilio - avatar
+ 1
thank you for your response
24th Aug 2017, 10:05 AM
// chris
// chris - avatar