Skip to content
Advertisement

What am I missing in my function (return an array of element that is greater than the element to its right)?

I need to create a function that finds all elements in the given array, such that each element is greater than all elements to the right of it.

Examples

JavaScript

Here is my function :

JavaScript

The test that makes me fail is when this array is called:

JavaScript

Advertisement

Answer

Just finding the index of the max element isn’t enough, because the elements that come after the max element may not be in decreasing order. Use .filter instead.

JavaScript
Advertisement