Promises - Exercise 1

Question 1: Resolve a Number

Return a promise resolved with the supplied number. The example waits for the promise before printing.

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

javascript

function resolveGivenNumber(value) {
  // Write your solution here
}

console.log(await resolveGivenNumber(7));

Question 2: Resolve a Calculated Total

Return a promise resolved with the sum of the two supplied numbers.

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

javascript

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

console.log(await resolveNumberSum(3, 5));

Question 3: Double a Resolved Value

Resolve the supplied number and use then to return twice its value.

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

javascript

function doubleResolvedValue(value) {
  // Write your solution here
}

console.log(await doubleResolvedValue(4));

Question 4: Format a Resolved Name

Resolve the supplied name and use then to return "Hello, " followed by the name.

Expected output: "Hello, Ada" (print this with console.log)

javascript

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

console.log(await formatResolvedGreeting("Ada"));

Question 5: Chain Two Arithmetic Steps

Resolve the number, add 3 in one then callback, and multiply the result by 2 in a second then callback.

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

javascript

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

console.log(await chainTwoPromiseSteps(4));

Question 6: Construct a Resolved Promise

Return a new Promise that resolves with the supplied text.

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

javascript

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

console.log(await constructResolvedText("ready"));

Question 7: Resolve an Array Length

Resolve the supplied array and use then to return its length.

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

javascript

function resolvedArrayLength(items) {
  // Write your solution here
}

console.log(await resolvedArrayLength(["a","b"]));

Question 8: Resolve a Trimmed String

Resolve the supplied string and use then to return it without outer whitespace.

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

javascript

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

console.log(await trimResolvedText("  hello  "));

Question 9: Resolve a Record Field

Resolve the supplied object and use then to return its score property.

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

javascript

function resolvedRecordScore(record) {
  // Write your solution here
}

console.log(await resolvedRecordScore({"score":9}));

Question 10: Return a Promise from Then

Resolve the supplied number, then return a second resolved promise containing the number plus 5.

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

javascript

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

console.log(await returnPromiseFromThen(6));