I'm challenging myself by doing the different problems in code signal. In the almostIncreasingSequence problem I coded this:
function almostIncreasingSequence(sequence) {
var count = 0;
sequence.forEach((element, i) => {
if(sequence[i] <= sequence[i-1]){
count++;
if(sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]){
return false;
}
}
});
return count <= 1;
};
14/17 Answers were correct. But searching for solutions I found that doing the same with a normal for loop it works and I do not know why
function almostIncreasingSequence(sequence) {
var count = 0;
for(i=0; i < sequence.length; i++){
if(sequence[i] <= sequence[i-1]){
count++;
if(sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]){
return false;
}
}
}
return count <= 1;
};