Skip to content

Instantly share code, notes, and snippets.

@jsantanders
Created January 25, 2023 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsantanders/7d390faea7e8913ebbd70711a1e83273 to your computer and use it in GitHub Desktop.
Save jsantanders/7d390faea7e8913ebbd70711a1e83273 to your computer and use it in GitHub Desktop.
Curebase - technical interview- solution - Jesus Santander.
/**
* 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