Hello! Need assessment for Arrays skill tests.
Congratulations @VladimirK. Everything is correct
Bonus question (if you like):
In task 4 instead of using a loop you could also filter the array. That’s an elegant way to create a new array based on a condition. Do you know how that could be achieved?
Happy coding,
Michael
Thank you @mikoMK!
I dont know but from the demo code i think that is something like this:
const eBirds = birds.filter(namesWithE => namesWithE.startsWith(‘E’));
or
const eBirds = birds.filter(function nameWithE(birds) {return birds.startsWith(‘E’)});
I will study it in more detail
Yes, both versions are correct.
You used namesWithE
and birds
as parameter names in the filter function. I would rename them to just bird
as we are checking a single bird for the condition inside the function. For example: bird => bird.startsWith('E')
.
In the second version the traditional function doesn’t need a name. We can just write: function (bird) {...}
Those functions that operate on every element of an array are often a simpler way to achieve the same as a loop does. Other similar functions are:
Have a nice day,
Michael