## Valid Mountain Array Solution in JavaScript

**Harsh Vats** on **August 05, 2021**

There's a very good problem on leetcode, about valid mountain array.

The steps to tackle it should be:

- If length of array is less than 3, then return false.
- If the array never started increasing, then return false.
- Then initialize a variable called
`maxFound`

to false and run a for loop till`arr.length - 2`

(because we will try to access`arr[i++]`

and we don't want to go beyond array length). - If element is equal to next element, return false.
- If the next element is greater than current element, set maxFound to true.
- If max is found and still the next element is greater than current, then return false.
- Now that you are out of for loop, if max is not found, that means array never started decreasing, then return false, otherwise return true.

Here's the solution of that problem in javascript.

var validMountainArray = function (arr) {if (arr.length < 3) return falseif (arr[1] < arr[0]) return falselet maxFound = falsefor (let i = 0; i < arr.length - 1; i++) {if (arr[i + 1] === arr[i]) return falseif (maxFound && arr[i + 1] > arr[i]) return falseif (arr[i] > arr[i + 1]) {maxFound = true}}// return maxFound ? true : falsereturn maxFound}