Basic Knowledge - Exercise 4

Question 1: Read a Book Title

Return the title property of the supplied book object.

Expected output: "Dune" (print this with console.log)

javascript

function readBookTitle(book) {
  // Write your solution here
}

console.log(readBookTitle({"title":"Dune","pages":400}));

Question 2: Create a Point

Return an object with x and y properties from the two supplied coordinates.

Expected output: {"x":3,"y":5} (print this with console.log)

javascript

function createCoordinatePoint(x, y) {
  // Write your solution here
}

console.log(createCoordinatePoint(3, 5));

Question 3: Return an Updated Price

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)

javascript

function copyWithNewPrice(product, price) {
  // Write your solution here
}

console.log(copyWithNewPrice({"name":"Pen","price":2}, 3));

Question 4: Check Whether a Score Passes

Return true when the score is at least the supplied pass mark.

Expected output: true (print this with console.log)

javascript

function scoreMeetsPassMark(score, passMark) {
  // Write your solution here
}

console.log(scoreMeetsPassMark(70, 60));

Question 5: Choose a Stock Message

Return "In stock" for a quantity greater than zero and "Out of stock" otherwise.

Expected output: "In stock" (print this with console.log)

javascript

function stockAvailabilityMessage(quantity) {
  // Write your solution here
}

console.log(stockAvailabilityMessage(3));

Question 6: Classify a Number's Sign

Return "positive", "negative", or "zero" according to the supplied number.

Expected output: "negative" (print this with console.log)

javascript

function classifyNumberSign(number) {
  // Write your solution here
}

console.log(classifyNumberSign(-4));

Question 7: Check an Even Number

Return true if the supplied integer is even.

Expected output: true (print this with console.log)

javascript

function integerIsEven(number) {
  // Write your solution here
}

console.log(integerIsEven(8));

Question 8: Check a User's Access

Return true only when the user is active and has a ticket. Both arguments are booleans.

Expected output: false (print this with console.log)

javascript

function userHasTicketAccess(active, hasTicket) {
  // Write your solution here
}

console.log(userHasTicketAccess(true, false));

Question 9: Choose a Shorter Word

Return the shorter string. When lengths are equal, return the first string.

Expected output: "sun" (print this with console.log)

javascript

function chooseShorterWord(first, second) {
  // Write your solution here
}

console.log(chooseShorterWord("sun", "moon"));

Question 10: Check a Required Field

Return true if the string contains at least one non-whitespace character.

Expected output: true (print this with console.log)

javascript

function hasRequiredText(text) {
  // Write your solution here
}

console.log(hasRequiredText("  hello  "));