简体   繁体   中英

Return True or False Without the OR operator

I need to write a function called "or".

The instruction was:

Given 2 boolean expressions, "or" returns true or false, corresponding to the || operator.

Notes:
* Do not use the || operator.
* Use ! and && operators instead.

Here's my function:

function or(expression1, expression2) {
  if(expression1 && !expression2) {
    return true;
  } else {
    return false;
  }
}

var output = or(true, false);
console.log(output); // --> IT MUST RETURN true;

Any idea what am I doing wrong?

try this:

function or(a, b) {
  return !(!a && !b)
}

Update your code to following

function or(expression1, expression2) {
  if(!expression1 && !expression2) {
    return false;
  } else {
    return true;
  }
}

 function or(expression1, expression2) { return !(!expression1 && !expression2); } console.log(or(true, false)); // --> IT MUST RETURN true; console.log(or(true, true)); console.log(or(false, false)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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