Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中布尔逻辑的顺序是什么?_Javascript - Fatal编程技术网

Javascript中布尔逻辑的顺序是什么?

Javascript中布尔逻辑的顺序是什么?,javascript,Javascript,我想用两个Not和一个and in boolean来测试变量既不是大写也不是小写 到目前为止,我使用了此代码,但它没有按要求工作: else if ((x[i]) !== (x[i].toUpperCase()) && (x[i]!== x[i].toLowerCase()) ){ x.splice(x[i], 1); } 这段代码用于对输入的字符串进行排序的函数,但先对大写字母进行排序 这是完整的代码,除了布尔逻辑和我使用的数组方法之外,我还可以理解创建此函数的更好方

我想用两个Not和一个and in boolean来测试变量既不是大写也不是小写

到目前为止,我使用了此代码,但它没有按要求工作:

else if ((x[i]) !== (x[i].toUpperCase()) && (x[i]!== x[i].toLowerCase()) ){
    x.splice(x[i], 1);
}
这段代码用于对输入的字符串进行排序的函数,但先对大写字母进行排序

这是完整的代码,除了布尔逻辑和我使用的数组方法之外,我还可以理解创建此函数的更好方法

function alpha(str){ // United States

    var x = str.split(""); // [U,n,i,t,e,d,S,t,a,t,e,s]
    var cap = [];
    var small = [];


    for (var i = 0; i<x.length; i++){

        if (x[i] == x[i].toUpperCase()){
            cap.push(x[i]);
        }

        else if ((x[i]) !== (x[i].toUpperCase()) && (x[i]!== x[i].toUpperCase()) )  {
            x.splice(x[i], 1);
        }
        else {small.push(x[i]);}
}


    var z = cap.sort();
    var y = small.sort();
    return z.concat(y).join("");

}
函数alpha(str){//美国
var x=str.split(“”;//[U,n,i,t,e,d,S,t,a,t,e,S]
var上限=[];
小风险值=[];

对于ASCII表格中的(var i=0;i,大写字母排在第一位。这就是按字母顺序排序时它们排在第一位的原因。这里有一个指向页面的链接,该页面显示了表格,大写字母排在第一位及其数字等价物。甚至可以打印

此外,我还冒昧地简化了您的代码。似乎没有必要使用.splice()

function alpha( str ) {

    var x = str.split(""); // [U,n,i,t,e,d,S,t,a,t,e,s]
    var cap = [];
    var small = [];
    var length = x.length;

    for (var i = 0; i < length; i++) {
        if (x[i] === x[i].toUpperCase()) {
            cap.push(x[i]);
        } else if (x[i] === x[i].toLowerCase()) {
            small.push(x[i]);
        }           

    }

    return cap.sort().concat(small.sort()).join("");

}
函数alpha(str){
var x=str.split(“”;//[U,n,i,t,e,d,S,t,a,t,e,S]
var上限=[];
小风险值=[];
变量长度=x.length;
对于(变量i=0;i

也许可以解释一下你想做什么?这很可能是以前以某种形式做过的,你肯定找到了正确的答案。

在所有编程语言中(据我所知),布尔表达式总是用括号从左到右求值

因此,在下面的示例中,首先调用my_func(),然后如果完整表达式有可能变为true,则调用my_other_func()

if (my_func() && my_other_func()) {
    // I only get here if my_func() AND my_other_func() return true
    // If my_func() returns false, my_other_func() is never called
}
下面示例中的“or”运算符也是如此

if (my_func() || my_other_func()) {
    // I only get here if my_func() OR my_other_func() return true
    // If my_func() returns true, my_other_func() is not called
}
因此,回到您的代码,在本部分中有详细说明(为了更好的可读性,我对其进行了一些重新格式化):


我不知道你想做什么,但我希望这些评论有助于理解你的代码。

这就是你想做的吗

var str=“美国”;
功能α(str){
返回str.split(“”).sort().join(“”);
}

警报(alpha(str))
为什么你会有一个条件,而
&&
是两个完全相同的条件?为什么你只测试大写字母,却要测试非大写字母?你想用
拼接
做什么?
拼接
采用索引,而不是数组元素或字母。对不起,这是为了小写字母,只是一个打字错误我正在测试非上限和非下限以及是否要删除,因为出于某种原因,当我输入“United Stated”时,我会得到“SUadeeinsttt”,因此这是为了拼接/删除此空间,所以拼接的索引应该是indexOf(x[I])?我知道对于这样一个简单的函数来说,这是一个复杂的方法,但作为一个学习者,这是我所能想到的。请给出几个输入和期望输出的案例。只有一个测试输入是“美国”,应该是“SUadeeinsttt”。非常感谢你的回答Patrick。我想。sort()是不区分大小写的,所以我使用了整个.toUpper/LowerCase以使大写字母排在第一位,但是我使用.splice()的原因是在加入数组之前从数组中删除这个“”空格字符串,但是,我刚刚了解了.trim()是的,这正是我要做的减号.trim()这就是为什么我使用了.toUpper/小写,非常感谢你的回答torazaburo,我真的很感激人们对像我这样的新手有耐心:)非常感谢你answe chickahoona。第二个.toUpperCase()应该是一个打字错误。toLowerCase(),你能解释一下,在我纠正了输入错误的情况下,代码是如何工作的吗?
    if (x[i] == x[i].toUpperCase()){
        // only uppercase here
        cap.push(x[i]);
    } else if (x[i] !== x[i].toUpperCase() && x[i] !== x[i].toUpperCase()) {
        // tested twice the same thing, so Im really sure that its not uppercase :D
        // only lowercase here
        x.splice(x[i], 1);
    } else {
        // I will never reach this
        small.push(x[i]);
    }