Basic Knowledge - Exercise 4
Question 1: Read a Book Title Completed
Return the title property of the supplied book object.
Expected output: "Dune" (print this with console.log)
function readBookTitle(book) {
// Write your solution here
}
console.log(readBookTitle({"title":"Dune","pages":400}));function readBookTitle(book) {
return book.title;
}
console.log(readBookTitle({"title":"Dune","pages":400}));Question 2: Create a Point Completed
Return an object with x and y properties from the two supplied coordinates.
Expected output: {"x":3,"y":5} (print this with console.log)
function createCoordinatePoint(x, y) {
// Write your solution here
}
console.log(createCoordinatePoint(3, 5));function createCoordinatePoint(x, y) {
return { x, y };
}
console.log(createCoordinatePoint(3, 5));Question 3: Return an Updated Price Completed
Return a copy of the product with its price replaced by the supplied price. Do not modify the product.
Expected output: {"name":"Pen","price":3} (print this with console.log)
function copyWithNewPrice(product, price) {
// Write your solution here
}
console.log(copyWithNewPrice({"name":"Pen","price":2}, 3));function copyWithNewPrice(product, price) {
return { ...product, price };
}
console.log(copyWithNewPrice({"name":"Pen","price":2}, 3));Question 4: Check Whether a Score Passes Completed
Return true when the score is at least the supplied pass mark.
Expected output: true (print this with console.log)
function scoreMeetsPassMark(score, passMark) {
// Write your solution here
}
console.log(scoreMeetsPassMark(70, 60));function scoreMeetsPassMark(score, passMark) {
return score >= passMark;
}
console.log(scoreMeetsPassMark(70, 60));Question 5: Choose a Stock Message Completed
Return "In stock" for a quantity greater than zero and "Out of stock" otherwise.
Expected output: "In stock" (print this with console.log)
function stockAvailabilityMessage(quantity) {
// Write your solution here
}
console.log(stockAvailabilityMessage(3));function stockAvailabilityMessage(quantity) {
return quantity > 0 ? "In stock" : "Out of stock";
}
console.log(stockAvailabilityMessage(3));Question 6: Classify a Number's Sign Completed
Return "positive", "negative", or "zero" according to the supplied number.
Expected output: "negative" (print this with console.log)
function classifyNumberSign(number) {
// Write your solution here
}
console.log(classifyNumberSign(-4));function classifyNumberSign(number) {
if (number > 0) return "positive";
if (number < 0) return "negative";
return "zero";
}
console.log(classifyNumberSign(-4));Question 7: Check an Even Number Completed
Return true if the supplied integer is even.
Expected output: true (print this with console.log)
function integerIsEven(number) {
// Write your solution here
}
console.log(integerIsEven(8));function integerIsEven(number) {
return number % 2 === 0;
}
console.log(integerIsEven(8));Question 8: Check a User's Access Completed
Return true only when the user is active and has a ticket. Both arguments are booleans.
Expected output: false (print this with console.log)
function userHasTicketAccess(active, hasTicket) {
// Write your solution here
}
console.log(userHasTicketAccess(true, false));function userHasTicketAccess(active, hasTicket) {
return active && hasTicket;
}
console.log(userHasTicketAccess(true, false));Question 9: Choose a Shorter Word Completed
Return the shorter string. When lengths are equal, return the first string.
Expected output: "sun" (print this with console.log)
function chooseShorterWord(first, second) {
// Write your solution here
}
console.log(chooseShorterWord("sun", "moon"));function chooseShorterWord(first, second) {
return first.length <= second.length ? first : second;
}
console.log(chooseShorterWord("sun", "moon"));Question 10: Check a Required Field Completed
Return true if the string contains at least one non-whitespace character.
Expected output: true (print this with console.log)
function hasRequiredText(text) {
// Write your solution here
}
console.log(hasRequiredText(" hello "));function hasRequiredText(text) {
return text.trim().length > 0;
}
console.log(hasRequiredText(" hello "));