Search code examples
javascriptarrayssequences

JavaScript sequence using for loop or switch statement


I am having trouble with a problem and how to address it. Mind you, I am relatively new to JavaScript and the problem feels like I am over complicating it.

The problem:

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 almostIncreasingSequence(sequence) = false;

There is no one element in this array that can be removed in order to get a strictly increasing sequence.

For sequence = [1, 3, 2], the output should be almostIncreasingSequence(sequence) = true.

You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

Thanks for all your comments! I wanted to update this to be a better question, and also inform you I have found a solution if someone would like to check it and see if there is a cleaner way to put it. :)

function almostIncreasingSequence(sequence) {    
  if(sequence.length == 2) return true;
  var error = 0;
  for(var i = 0; i < sequence.length - 1; i++){
    if(sequence[i] >= sequence[i+1]){
      var noStepBack = sequence[i-1] && sequence[i-1] >= sequence[i+1];
      var noStepFoward = sequence[i+2] && sequence[i] >= sequence[i+2];
      if(i > 0 && noStepBack && noStepFoward) {
        error+=2;
      }else{
        error++;
      }
    }
    if(error > 1){
      return false;
    }
  }
  return true;
}

Solution

  • Think about what your code:

    sequence[i+1] - sequence[i] !== 1, variable++;
    

    would do for the following array:[1,2,3,8,8].

    From the problem description, it is not clear weather the program must remove one character. But if that is the case, the code below should do it.

    function canGetStrictlyIncreasingSeq(numbers) {
      var counter = 0;
      var lastGreatestNumber = numbers[0];
      for (var i = 1; i < numbers.length; i++) {
        if (lastGreatestNumber >= numbers[i]) {
          counter++;
          lastGreatestNumber = numbers[i];
        } else {
          lastGreatestNumber = numbers[i];
        }
      }
      if (counter <= 1)
        return true;
      return false;
    }
    
    var nums1 = [1, 2, 3, 4, 5]; //true
    var nums2 = [1, 2, 2, 3, 4]; //true
    var nums3 = [1, 3, 8, 1, 9]; //true
    var nums4 = [3, 2, 5, 6, 9]; //true
    var nums5 = [3, 2, 1, 0, 5]; //false
    var nums6 = [1, 2, 2, 2, 3]; //false
    var nums7 = [1, 1, 1, 1, 1]; //false
    var nums8 = [1, 2]; //true
    var nums9 = [1, 2, 2]; //true
    var nums10 = [1, 1, 2, 3, 4, 5, 5]; //false
    var nums11 = [10, 1, 2, 3, 4, 5]; //true
    var nums12 = [1, 2, 3, 4, 99, 5, 6]; //true
    
    
    console.log(canGetStrictlyIncreasingSeq(nums1));
    console.log(canGetStrictlyIncreasingSeq(nums2));
    console.log(canGetStrictlyIncreasingSeq(nums3));
    console.log(canGetStrictlyIncreasingSeq(nums4));
    console.log(canGetStrictlyIncreasingSeq(nums5));
    console.log(canGetStrictlyIncreasingSeq(nums6));
    console.log(canGetStrictlyIncreasingSeq(nums7));
    console.log(canGetStrictlyIncreasingSeq(nums8));
    console.log(canGetStrictlyIncreasingSeq(nums9));
    console.log(canGetStrictlyIncreasingSeq(nums10));
    console.log(canGetStrictlyIncreasingSeq(nums11));
    console.log(canGetStrictlyIncreasingSeq(nums12));