Operators - Exercise 2
Question 1: Check a Valid Temperature Completed
Return true when a temperature is at least the minimum and at most the maximum, inclusive.
Expected output: true (print this with console.log)
function isTemperatureInRange(temperature, min, max) {
// Write your solution here
}
console.log(isTemperatureInRange(18, 15, 25));function isTemperatureInRange(temperature, min, max) {
return temperature >= min && temperature <= max;
}
console.log(isTemperatureInRange(18, 15, 25));Question 2: Allow Either Payment Method Completed
Return true if either cash or card is accepted. Both arguments are booleans.
Expected output: true (print this with console.log)
function acceptsEitherPayment(cash, card) {
// Write your solution here
}
console.log(acceptsEitherPayment(false, true));function acceptsEitherPayment(cash, card) {
return cash || card;
}
console.log(acceptsEitherPayment(false, true));Question 3: Reverse a Door State Completed
Return true when the supplied isOpen boolean is false, and false otherwise.
Expected output: false (print this with console.log)
function doorIsClosed(isOpen) {
// Write your solution here
}
console.log(doorIsClosed(true));function doorIsClosed(isOpen) {
return !isOpen;
}
console.log(doorIsClosed(true));Question 4: Choose a Display Name Completed
Use || to return the supplied name, or "Guest" when the name is falsy.
Expected output: "Guest" (print this with console.log)
function chooseDisplayName(name) {
// Write your solution here
}
console.log(chooseDisplayName(""));function chooseDisplayName(name) {
return name || "Guest";
}
console.log(chooseDisplayName(""));Question 5: Keep a Zero Volume Completed
Use ?? to return the supplied volume or 50 only when it is null or undefined.
Expected output: 0 (print this with console.log)
function defaultAudioVolume(volume) {
// Write your solution here
}
console.log(defaultAudioVolume(0));function defaultAudioVolume(volume) {
return volume ?? 50;
}
console.log(defaultAudioVolume(0));Question 6: Choose a Shipping Label Completed
Return "free" when the total is at least 50 and "paid" otherwise, using a conditional operator.
Expected output: "free" (print this with console.log)
function shippingChargeLabel(total) {
// Write your solution here
}
console.log(shippingChargeLabel(55));function shippingChargeLabel(total) {
return total >= 50 ? "free" : "paid";
}
console.log(shippingChargeLabel(55));Question 7: Compare Numeric Text Loosely Completed
Use == to compare the supplied number and numeric string. Return the boolean result.
Expected output: true (print this with console.log)
function looselyCompareNumericText(number, text) {
// Write your solution here
}
console.log(looselyCompareNumericText(7, "7"));function looselyCompareNumericText(number, text) {
return number == text;
}
console.log(looselyCompareNumericText(7, "7"));Question 8: Detect a Changed Setting Completed
Return true when the two setting values are not strictly equal.
Expected output: true (print this with console.log)
function settingHasChanged(before, after) {
// Write your solution here
}
console.log(settingHasChanged(1, "1"));function settingHasChanged(before, after) {
return before !== after;
}
console.log(settingHasChanged(1, "1"));Question 9: Negate a Temperature Change Completed
Return the arithmetic negation of the supplied number using the unary minus operator.
Expected output: -8 (print this with console.log)
function negateTemperatureChange(change) {
// Write your solution here
}
console.log(negateTemperatureChange(8));function negateTemperatureChange(change) {
return -change;
}
console.log(negateTemperatureChange(8));Question 10: Convert a Numeric Input Completed
Use unary plus to convert a numeric string to a number.
Expected output: 3.5 (print this with console.log)
function convertNumericInput(text) {
// Write your solution here
}
console.log(convertNumericInput("3.5"));function convertNumericInput(text) {
return +text;
}
console.log(convertNumericInput("3.5"));