CC-02: Conditionals 2

with Carmen Salas • 2024/10/03

slidesarrow-up-right


Logical Operators

and and or are the ones that we use for JavaScript. Unlike equality operators, logical operators don't necessarily evaluate into boolean.

Short Circuiting

if (1 > 3 && 13 > 5) {
   return true;
} else { return false; }

On operations concerning logical operators, most languages (JavaScript obviously included), evaluates the operands from left to right. This creates an interesting behavior called short-circuiting. If any of the operands that come first is enough to satisfy either && or ||, the second operand will not be run at all.

circle-info

This is important in testing because short-circuiting may prevent you from catching any syntax or runtime errors in the second operand.

Code Challenge

  1. Write a function named greatestOfThree that takes in three integer arguments and returns the largest of all arguments. Solve this problem without using any sorting JS methods. You can assume all integers will be different values.

  1. Write a function named distinstThree that takes in three integer arguments and returns a boolean if all the three arguments are distinct. Distinct integers are integers that are not equal to each-other.

  1. Write a function named isEvenAndPositive that checks if a number is both even and positive. This function should return a boolean.

  1. Write a function named leastOfFour that takes in four integer arguments and returns the least of all arguments. Solve this problem without using any sorting JavaScript methods. You can assume all integers will be different values.

Last updated