Posts

Showing posts with the label boolean

Why my code doesn't work correctly by sometimes returning true instead of false?

Why my code doesn't work correctly by sometimes returning true instead of false? This is my code: function almostIncreasingSequence(sequence) { var counter = 0; for (var i = 1; i < sequence.length - 1; i++) { if (sequence[i] <= sequence[i - 1]) { counter += 1; } else if (sequence[i + 1] <= sequence[i - 1]) { counter += 1; } } if (counter <= 1) { return true; } return false; } console.log(almostIncreasingSequence([1, 3, 2, 1])); console.log(almostIncreasingSequence([1, 2, 5, 5, 5])); console.log(almostIncreasingSequence([1, 2, 3, 4, 3, 6])); This code's job is to: Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. Example For sequence = [1, 3, 2, 1] , the output should be sequence = [1, 3, 2, 1] almostIncreasingSequence(sequence) = false; There is no one element in this array that can be removed in order to get ...