I cannot answer "Filter Arrays with .filter" question

I cannot answer "Filter Arrays with .filter" question
0

#1

I think I am on track with my code for this exercise, but get told array is not defined

var oldArray = [1,2,3,4,5,6,7,8,9,10];

// Only change code below this line.

var newArray = oldArray;
newArray = array.filter(function(val) {
return val<6;
});

To some extent I am completing this exercise a bit blind. In the previous exercises I have been able to complete them but have not really understood. I think it is the same with this exercise.
Any assitance welcome


#2

hi @bushcoder

If you check this line

newArray = array.filter(function(val) {

you can see you are trying to filter “array” which is not defined.

If you change it to

newArray = newArray.filter(function(val) {

it will work.


#3

With the newArray = oldArray isn’t necessary as that is just a pointer reference to the oldArray, so it’s actually the same array. You just need to have var newArray = oldArray.filter()=>

You can check this by entering your oldarray in console and setting your newArray to it like you did here. Try changing newArray and you will see it changes your oldArray too!


#4

Hi everybody.

I see that the initial question is answered, but I would like to ask another question related to this challenge, or how it can be solved on FCC. How come on MDN the callback is not needed for this method to run, or I am missing something?

I tried to simplify the solution on FCC, but it throws back an error that “val” is not defined.

var newArray = oldArray.filter( val < 6 );

#5

Actually, it dawned on me :slight_smile: It’s an arrow function, that’s why… Well, then another question would be, why can’t we use ES2015 functionality in FCC solutions? That kinda sets us back on our learning curve…