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 // falseconst 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 // trueconst b = true && false // falseconst 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 hereconsole.log(arg)return true}// this does not cause expensiveFunction to be called// since the first value is falsyconst a = false && expensiveFunction("a")// expensiveFunction is called since no falsy// values short-circuited the evaluation before// the function call operandconst 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 // trueconst b = true || false // trueconst 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 hereconsole.log(arg)return true}// expensiveFunction is called since no truthy// values short-circuited the evaluation before// the function call operandconst a = false || expensiveFunction("a")// this does not cause expensiveFunction to be called// since the first value is truthyconst 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
You should define a function named
calculatePrice. - 2
calculatePrice(121, 0)should return13. - 3
calculatePrice(161, 5)should return17. - 4
calculatePrice(179, 6)should return90. - 5
calculatePrice(200, 17)should return100. - 6
calculatePrice(306, 18)should return306. - 7
calculatePrice(158, 66)should return158. - 8
calculatePrice(234, 67)should return117. - 9
calculatePrice(451, 117)should return226.