yes I know there are a few similar questions about this problem that's why I will skip the intro about it. All of them have different codes but nothing identical to mine and I couldn't understand and fix the problem with my code. So the problem with my code is that it can pass 35 of the 38 tests but it can't pass the performance tests. I tried to stop the iterations to make it faster but it still fails. What can I do to make it faster?
function solution(sequence) {
const min = 2
let countFalse = 0
let dblCheck = false
const dblFind = sequence => sequence.filter((item, index) => sequence.indexOf(item) !== index)
const dblFalse = dblFind(sequence)
if (sequence.length === min) {
return true
} else if (dblFalse.length > 1){
return false
} else {
let start = sequence[0]
for (var i=0; i < sequence.length-1; i++) {
if (start < sequence[i+1]){
start = sequence[i+1]
} else {
countFalse++
start = sequence[i+1]
if (countFalse >=2){
return false
}
if (sequence[i+1] < sequence[i+2] && sequence[i-1] > sequence[i+2]) {
dblCheck = true
return false
}
}
}
}
return true
}
console.log(solution([1,3,2,1])) //false
console.log(solution([1,3,2])) //true
console.log(solution([40, 50, 60, 10, 20, 30])) //false