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;
}