Functions - Exercise 1
Question 1: Call a Formatting Helper Completed
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)
function formatLabelWithHelper(label) {
// Write your solution here
}
console.log(formatLabelWithHelper("draft"));function formatLabelWithHelper(label) {
function wrap(value) { return "(" + value + ")"; }
return wrap(label);
}
console.log(formatLabelWithHelper("draft"));Question 2: Call a Helper Twice Completed
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)
function adjustTwoValuesWithHelper(first, second, adjustment) {
// Write your solution here
}
console.log(adjustTwoValuesWithHelper(2, 5, 3));function adjustTwoValuesWithHelper(first, second, adjustment) {
function adjust(value) { return value + adjustment; }
return [adjust(first), adjust(second)];
}
console.log(adjustTwoValuesWithHelper(2, 5, 3));Question 3: Use a Default Multiplier Completed
Return the value multiplied by multiplier, which defaults to 2 when omitted.
Expected output: 14 (print this with console.log)
function multiplyWithDefault(value, multiplier = 2) {
// Write your solution here
}
console.log(multiplyWithDefault(7));function multiplyWithDefault(value, multiplier = 2) {
return value * multiplier;
}
console.log(multiplyWithDefault(7));Question 4: Use a Default Separator Completed
Return the two words joined by separator, which defaults to a space when omitted.
Expected output: "red blue" (print this with console.log)
function joinWordsDefaultSeparator(first, second, separator = " ") {
// Write your solution here
}
console.log(joinWordsDefaultSeparator("red", "blue"));function joinWordsDefaultSeparator(first, second, separator = " ") {
return first + separator + second;
}
console.log(joinWordsDefaultSeparator("red", "blue"));Question 5: Use a Function Expression Completed
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)
function subtractWithExpression(first, second) {
// Write your solution here
}
console.log(subtractWithExpression(9, 4));function subtractWithExpression(first, second) {
const subtract = function (a, b) { return a - b; };
return subtract(first, second);
}
console.log(subtractWithExpression(9, 4));Question 6: Use an Arrow Function Completed
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)
function squareWithArrow(number) {
// Write your solution here
}
console.log(squareWithArrow(4));function squareWithArrow(number) {
const square = (value) => value * value;
return square(number);
}
console.log(squareWithArrow(4));Question 7: Call a Declared Helper Early Completed
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)
function callWrapperBeforeDeclaration(text) {
// Write your solution here
}
console.log(callWrapperBeforeDeclaration("note"));function callWrapperBeforeDeclaration(text) {
return wrap(text);
function wrap(value) { return "<" + value + ">"; }
}
console.log(callWrapperBeforeDeclaration("note"));Question 8: Return the First Nonempty Argument Completed
Return the first argument if it is a nonempty string, otherwise return the second argument.
Expected output: "backup" (print this with console.log)
function firstNonemptyArgument(first, second) {
// Write your solution here
}
console.log(firstNonemptyArgument("", "backup"));function firstNonemptyArgument(first, second) {
if (first !== "") return first;
return second;
}
console.log(firstNonemptyArgument("", "backup"));Question 9: Return an Object from an Arrow Completed
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)
function makeNamedObjectWithArrow(name) {
// Write your solution here
}
console.log(makeNamedObjectWithArrow("Ada"));function makeNamedObjectWithArrow(name) {
const make = (value) => ({ name: value });
return make(name);
}
console.log(makeNamedObjectWithArrow("Ada"));Question 10: Calculate a Rectangle Summary Completed
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)
function rectangleSummaryWithHelper(width, height) {
// Write your solution here
}
console.log(rectangleSummaryWithHelper(3, 4));function rectangleSummaryWithHelper(width, height) {
function area(w, h) { return w * h; }
return { area: area(width, height), perimeter: 2 * (width + height) };
}
console.log(rectangleSummaryWithHelper(3, 4));