1

JavaScript novice here. I ran across the following code in the solution to an exercise. I'm not sure what it is doing:

var noStepBack = sequence[i-1] && sequence[i-1] >= sequence[i+1];
var noStepFoward = sequence[i+2] && sequence[i] >= sequence[i+2];

It appears that it is declaring variables and initializing them, but the code to the right of the assignment operator is a conditional statement. Does this simply assign "0" if the conditional statements resolve to false and "1" if they resolve to true?

More context, this is for the exercise almostIncreasingSequence from the CodeSignal website. The exercise description and full solution are as follows:

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.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

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].

function almostIncreasingSequence(sequence) {    
    if(sequence.length == 2) return true;

    var error = 0;

    for(var i = 0; i < sequence.length - 1; i++){
        // if current value is greater than next value
        if(sequence[i] >= sequence[i+1]){
            // Test whether stepping back or forwards can bridge the hump or pothole
            var noStepBack = sequence[i-1] && sequence[i-1] >= sequence[i+1];
            var noStepFoward = sequence[i+2] && sequence[i] >= sequence[i+2];
            // We only test for bridge gaps when i > 0
            if(i > 0 && noStepBack && noStepFoward) {
                // Cannot step back over gap forwards or backwards
                // Counts as two errors ONLY WHEN BOTH PRESENT
                error+=2;
            }else{
                // Typical error
                error++;
            }
        }
        // Early dropout cause if you ever get more than one error, then its game over anyway
        if(error > 1) return false;
    }
    return true;
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Saint Razz
  • 53
  • 1
  • 7
  • 1
    `Does this simply assign "0" if the conditional statements resolve to false and "1" if they resolve to true?` No. It assigns false when the statements resolve to false and true when they resolve to true. – tkausl Feb 20 '19 at 18:50

1 Answers1

0

Yes and they actually get assigned boolean values.

For example, for the noStepBack variable, it will be computed as the boolean result of this condition: sequence[i-1] && sequence[i-1] >= sequence[i+1];

If that is true, noStepBack = true and noStepBack = false otherwise.

Note that true and false are primitive Boolean values of Javascript.

For instance, if we have a situation where noStepBack = true and noStepFoward = false

The statement

(i > 0 && noStepBack && noStepFoward)

will be equivalent to:

(i > 0 && true && false)

Hope this helps.

Luanf
  • 172
  • 1
  • 10