JavaScript Boolean Type

Operators

We quite often come in situations where we want to figure out whether a list of conditions are truthy, whether at least one of them is truthy or just invert a Boolean value.

Negation (!)

The negation operator ! inverts a Boolean value to its opposite value.

const a = !true // false
const b = !false // true

Logical AND (&&)

The logical AND operator returns a truthy value only when all of its operands are truthy. We usually use this operator (logical conjunction) in control flow predicates when we want a list of conditions to be truthy before executing some code.

This operator returns the last evaluated value.

const a = true && true // true
const b = true && false // false
const c = true && 1 // 1 (truthy)

Short-circuit

The logical AND operator is evaluated from the left side to the right, and short-circuits as soon as one of the values are falsy. In the example below expensiveFunction is a function that we only want to execute when all other operands have been evaluated to truthy values. We use the operator's short-circuit functionality to optimize the execution time.

const expensiveFunction = arg => {
// heavy costly code here
console.log(arg)
return true
}
// this does not cause expensiveFunction to be called
// since the first value is falsy
const a = false && expensiveFunction("a")
// expensiveFunction is called since no falsy
// values short-circuited the evaluation before
// the function call operand
const b = true && expensiveFunction("a")

Logical OR (||)

The logical AND operator returns a truthy value when one of its operands are truthy. We usually use this operator (logical disjunction) in control flow predicates when we want at least one condition to be truthy before executing some code.

This operator returns the first truthy value or the last value.

const a = true || true // true
const b = true || false // true
const c = true || 1 // true

Short-circuit

The logical OR operator is evaluated from the left side to the right, and short-circuits as soon as one of the operand values are truthy.

const expensiveFunction = arg => {
// heavy costly code here
console.log(arg)
return true
}
// expensiveFunction is called since no truthy
// values short-circuited the evaluation before
// the function call operand
const a = false || expensiveFunction("a")
// this does not cause expensiveFunction to be called
// since the first value is truthy
const b = true || expensiveFunction("a")

Test yourself

Description

Define a new function with the name

calculatePrice

that takes two integer number input parameter: ticketPrice and age. Your function should calculate the final price of the train ticket by applying reductions in price if applicable. The price should be rounded up to the nearest integer.

Ticket prices by age:

  • 0-5: Tenth (10% of full ticket price)
  • 6-17: Half price (50% of full ticket price)
  • 18-66: Full price (100% of full ticket price)
  • 67-Infinity: Half price (50% of full ticket price)

Assumptions:

  • Full ticket price is a positive integer number larger than zero.
  • Age is a positive integer number larger than zero.

Tips:

  • Use short-circuiting aggressively.
  • Abuse the truthiness of non-zero numbers.
  • Final prices of full price percentages can be found by multiplying the full price with the percentage in decimal format.
  • 100% means that we are multiplying the full price with 1.0.
  • 50% means that we are multiplying the full price with 0.5.
  • 10% means that we are multiplying the full price with 0.1.
  • This challenge can be solved entirely without control flow statements; what've learnt so far is enough.

Tests

  1. 1 You should define a function named calculatePrice.
  2. 2calculatePrice(121, 0) should return 13.
  3. 3calculatePrice(161, 5) should return 17.
  4. 4calculatePrice(179, 6) should return 90.
  5. 5calculatePrice(200, 17) should return 100.
  6. 6calculatePrice(306, 18) should return 306.
  7. 7calculatePrice(158, 66) should return 158.
  8. 8calculatePrice(234, 67) should return 117.
  9. 9calculatePrice(451, 117) should return 226.
Editor