Javascript if if else (not otherwise if)

I have a simple bit of code that checks some conditions, and I want each condition to produce a unique output, but if the conditions were not met, to cause another unique output.

Is there a way to create elseone that only starts if all previous instructions iffail? I know that the code below does the same thing, but it seems cumbersome, and it would be better to have a faster way to check all the variables than copy and paste all of them into another if statement.

var boolean1 = true,
  boolean2 = true,
  boolean3 = false;

if (boolean1) {
  alert("boolean1");
}
if (boolean2) {
  alert("boolean2");
}
if (boolean3) {
  alert("boolean3");
}
/* and so on */
if (!boolean1 && !boolean2 && !boolean3 /* etc */ ) {
  alert("none");
}
Run codeHide result
+4
source share
9 answers

, , . , , . , , . checkResults , , true. , - true .

function checkResults(resultMap) {
    var any = false;
    Object.keys(resultMap).forEach(function(key) {
        if (resultMap[key] === true) {
          any = true;
          alert(key);
        }
    });
    if (!any)
      alert('none');
}

checkResults({
  'boolean1': false,
  'boolean2': true,
  'boolean3': false
});
Hide result

, :

var results = {};
results['boolean1'] = true;
results['boolean2'] = false;
...
checkResults(results);

checkResults .

+1

, , - , .

var boolean1 = true,
    boolean2 = true,
    boolean3 = false,
    any = false;


if (boolean1) {
  alert("boolean1");
  any = true;
}
if (boolean2) {
  alert("boolean2");
  any = true;
}
if (boolean3) {
  alert("boolean3");
  any = true;
}
/* and so on */
if (!any) {
  alert("none")
}

, , .

+1

, :

var booleans = [false, false, false];
var i = 0;
while (i < booleans.length) {
    if (booleans[i] === true) {
        alert("boolean"+i);
    }
    i++;
}
i = 0;
while (booleans[i] === false && i < booleans.length) {
    i++;
}
if (i == booleans.length) {
    alert('none');
}

// Filter would work like this:
var bools = booleans.filter(function (val, ind) { return !val; });
if (bools.length  > 0) alert('none');
Hide result

filter() : https://msdn.microsoft.com/de-de/library/ff679973(v=vs.94).aspx

.every(), @Bergi.

+1
var accumulated = false;

acumulated = acumulated || boolean1;
if (boolean1) {
  alert("boolean1");
}

acumulated = acumulated || boolean2;
if (boolean2) {
  alert("boolean2");
}

acumulated = acumulated || boolean3;
if (boolean3) {
  alert("boolean3");
}

if(!acumulated) {
  alert("none");
}
0

, :

var boolean1 = true,
    boolean2 = true,
    boolean3 = false;

[(boolean1 && !alert("boolean1")),
    (boolean2 && !alert("boolean2")),
      (boolean3 && !alert("boolean3"))]
      .some(function(passed) {
        return passed;
      }) || alert("none");
Hide result
0

, none

var boolean1 = true;
var boolean2 = true;
var boolean3 = false;
var allConditionFailed = true;

if (boolean1) {
  alert("boolean1");
  allConditionFailed = false;
}
if (boolean2) {
  alert("boolean2");
  allConditionFailed = false;
}
if (boolean3) {
  alert("boolean3");
  allConditionFailed = false;
}

if(allConditionFailed) {
   alert("none");
}
0

, . , ( ) boolean .

( , ),

if (boolean1) {
  alert("boolean1");
  if (boolean2) {
    alert("boolean2");
  }
  if (boolean3) {
    alert("boolean3");
  }
} else {
  if (boolean2) {
    alert("boolean2");
    if (boolean3) {
      alert("boolean3");
    }
  } else {
    if (boolean3) {
      alert("boolean3");
    } else {
      alert("none")
    }
  }
}

, , , .

, , :

var booleans = [true, true, false];
var alerts = ["boolean1", "boolean2", "boolean3"];
for (var i=0; i<booleans.length; i++)
  if (booleans[i])
    alert(alerts[i]);
if (booleans.every(b => !b))
  alert("none");
0

for, .

var boolean1 = true,
  boolean2 = true,
  boolean3 = false;
  none=true;
for(i=1;i<4;i++){
    var variable=window["boolean"+i];
    if (variable) {
      alert("boolean"+i);
      none=false;
    }
}

if (none) {
  alert("none");
}
0

If you can save them as an array, you can use array.indexOf()to make sure that true does not exist in the array (all tests do not work), and if it loops through the array using array.forEach()to do something with each of them.

So your code will look something like this:

// The array of conditions.
var bools = [true, true, false];

// Check if none of them are true (all fail).
if (bools.indexOf(true) === -1) {
    alert("none");
} else {
// Loop over them and do something based if the current one is true.
bools.forEach(function(b, i) {
    b ? alert("boolean" + (i + 1)) : '';
});
};
0
source

Source: https://habr.com/ru/post/1667172/


All Articles