Logical operator or

2

What am I doing wrong? In Javascript I am using the logical operator or in:

  

Use the logical operator or in variables 'foo' and 'bar' and assign it the   result to the variable disjunction.

But it's not working for me; The code I use is the following:

foo = true;
bar = false;

if (foo || bar) {

  disjunction = foo;
} else {
  disjunction = bar;
}
    
asked by rafael herrera 02.02.2018 в 04:51
source

4 answers

3

You can save the whole if block as follows:

let disjunction = (foo || bar);

By the way, if it's an exercise to practice, I advise you to understand well what that operator does:

let foo = "hola";
let bar = "mundo"

console.log(foo || bar);
console.log(bar || foo);
console.log(!!(bar || foo));

console.log(0 || foo);
console.log(foo || 0);
    
answered by 02.02.2018 в 09:49
1

Enter the if for the variable foo and that's why the variable disjunction has value true

let foo = true,
  bar = false,
  disjunction = undefined;

if (foo || bar) {

  disjunction = foo;
} else {
  disjunction = bar;
}
console.log(disjunction);
    
answered by 02.02.2018 в 04:55
-1
var disjunction=0;
var foo = true;
var bar = false;


Boolean(disjunction); //retorna false por que disjunction vale 0

if (foo==true || bar==false) {

  disjunction = foo;
} else {
  disjunction = bar;
}

alert(disjunction):
    
answered by 02.02.2018 в 05:18
-2
If(foo=='true'  || bar=='false') {}
    
answered by 02.02.2018 в 04:56