Skip to content
Advertisement

How to check strictly increasing and decreasing array in javascript

I solved an exercise in javascript but I’m not very happy with my solution and I couldn’t find a better one.

Problem: check if an array have a starting subarray strictly increasing and an ending subarray strictly decreasing. Each array has at least 3 elements.

Examples:

JavaScript

My solution:

JavaScript

Is there a better way to do it? Maybe using reduce?

Thanks.

Advertisement

Answer

You can create difference graph and check for the specific pattern.

Here you can find two patterns first return condition check whether array elements follows increment and then decrement pattern.

Second return condition check whether array elements follows and decrement then increment pattern.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement