简体   繁体   English

JavaScript变量声明/初始化可以使用条件类型的布尔值吗?

[英]Can a JavaScript variable declaration/initialization use a boolean type conditional?

JavaScript novice here. JavaScript新手在这里。 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? 如果条件语句解析为false,是否仅将其分配为“ 0”,如果条件解析为true,则将其分配为“ 1”?

More context, this is for the exercise almostIncreasingSequence from the CodeSignal website. 更多的上下文,这是针对CodeSignal网站上几乎增加序列的练习。 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. 注意:如果a0 <a1 <... <an,则序列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. 对于序列= [1、3、2、1],输出应几乎为IncreasingSequence(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. 对于序列= [1、3、2],输出应几乎为IncreasingSequence(sequence)= true。

You can remove 3 from the array to get the strictly increasing sequence [1, 2]. 您可以从数组中删除3以获得严格递增的序列[1、2]。 Alternately, you can remove 2 to get the strictly increasing sequence [1, 3]. 或者,您可以删除2以得到严格递增的序列[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;
}

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]; 例如,对于noStepBack变量,它将被计算为该条件的布尔结果: sequence[i-1] && sequence[i-1] >= sequence[i+1];

If that is true, noStepBack = true and noStepBack = false otherwise. 如果为true,则noStepBack = true ,否则为noStepBack = false

Note that true and false are primitive Boolean values of Javascript. 请注意, truefalse是Javascript的原始布尔值。

For instance, if we have a situation where noStepBack = true and noStepFoward = false 例如,如果存在noStepBack = truenoStepFoward = false

The statement 该声明

(i > 0 && noStepBack && noStepFoward)

will be equivalent to: 将等同于:

(i > 0 && true && false)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM