Created
January 25, 2023 19:08
-
-
Save jsantanders/7d390faea7e8913ebbd70711a1e83273 to your computer and use it in GitHub Desktop.
Curebase - technical interview- solution - Jesus Santander.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function determines if a given array input is a valid 'Montain number' | |
* @param {number[]} arr the input array | |
* @returns {boolean} True if is valid | |
*/ | |
const isValidMountainArray = (arr) => { | |
let isIncreasing = true; | |
let isDecreasing = false; | |
if(arr.length < 3) return false; | |
if(arr[0] > arr[1]) return false; | |
for(let i = 0; i < arr.length - 1; i++){ | |
if(typeof arr[i] !== "number") return false; | |
if(arr[i] < 0) return false; | |
if(arr[i] == arr[i+1]) return false; | |
if(arr[i] > Math.pow(10,4)) return false; | |
if(isIncreasing && arr[i] > arr[i+1]){ | |
isIncreasing = false; | |
isDecreasing= true; | |
continue; | |
} | |
if(isDecreasing && arr[i] < arr[i+1]){ | |
return false; | |
} | |
} | |
if(!isDecreasing) return false; | |
return true; | |
} | |
const isValidMountInArray_InputIsString_ShouldReturnFalse = () => { | |
const caseScenario = ["a", 5, "b"]; | |
if(!isValidMountainArray(caseScenario)) throw Error(`It should return false on test case -> ${caseScenario}`) | |
} | |
const isValidMountainArray_InputGratherThan10pow4_ShouldReturnFalse = () => { | |
const caseScenario = [0, 1, 100000, 1, 0]; | |
if (!isValidMountainArray(caseScenario)) throw Error(`It should return false on test case -> ${caseScenario}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment