Functions - Exercise 1

Question 1: Call a Formatting Helper

Define a helper that wraps its input in parentheses. Call it for the supplied label and return the result.

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

javascript

function formatLabelWithHelper(label) {
  // Write your solution here
}

console.log(formatLabelWithHelper("draft"));

Question 2: Call a Helper Twice

Define a helper that adds the supplied adjustment to a number. Return its results for the two inputs in an array.

Expected output: [5,8] (print this with console.log)

javascript

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

console.log(adjustTwoValuesWithHelper(2, 5, 3));

Question 3: Use a Default Multiplier

Return the value multiplied by multiplier, which defaults to 2 when omitted.

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

javascript

function multiplyWithDefault(value, multiplier = 2) {
  // Write your solution here
}

console.log(multiplyWithDefault(7));

Question 4: Use a Default Separator

Return the two words joined by separator, which defaults to a space when omitted.

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

javascript

function joinWordsDefaultSeparator(first, second, separator = " ") {
  // Write your solution here
}

console.log(joinWordsDefaultSeparator("red", "blue"));

Question 5: Use a Function Expression

Define a function expression that subtracts the second number from the first. Call it with the supplied values and return the result.

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

javascript

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

console.log(subtractWithExpression(9, 4));

Question 6: Use an Arrow Function

Define an arrow function that returns the square of a number. Call it with the supplied number and return the result.

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

javascript

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

console.log(squareWithArrow(4));

Question 7: Call a Declared Helper Early

Call a function declaration before its declaration. The helper should return the input wrapped in angle brackets.

Expected output: "<note>" (print this with console.log)

javascript

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

console.log(callWrapperBeforeDeclaration("note"));

Question 8: Return the First Nonempty Argument

Return the first argument if it is a nonempty string, otherwise return the second argument.

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

javascript

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

console.log(firstNonemptyArgument("", "backup"));

Question 9: Return an Object from an Arrow

Use an arrow function to create an object with a name property set to its argument. Return the resulting object.

Expected output: {"name":"Ada"} (print this with console.log)

javascript

function makeNamedObjectWithArrow(name) {
  // Write your solution here
}

console.log(makeNamedObjectWithArrow("Ada"));

Question 10: Calculate a Rectangle Summary

Define a helper for rectangle area. Return an object containing area and perimeter for the supplied width and height.

Expected output: {"area":12,"perimeter":14} (print this with console.log)

javascript

function rectangleSummaryWithHelper(width, height) {
  // Write your solution here
}

console.log(rectangleSummaryWithHelper(3, 4));