Promises - Exercise 1
Question 1: Resolve a Number Completed
Return a promise resolved with the supplied number. The example waits for the promise before printing.
Expected output: 7 (print this with console.log)
function resolveGivenNumber(value) {
// Write your solution here
}
console.log(await resolveGivenNumber(7));function resolveGivenNumber(value) {
return Promise.resolve(value);
}
console.log(await resolveGivenNumber(7));Question 2: Resolve a Calculated Total Completed
Return a promise resolved with the sum of the two supplied numbers.
Expected output: 8 (print this with console.log)
function resolveNumberSum(first, second) {
// Write your solution here
}
console.log(await resolveNumberSum(3, 5));function resolveNumberSum(first, second) {
return Promise.resolve(first + second);
}
console.log(await resolveNumberSum(3, 5));Question 3: Double a Resolved Value Completed
Resolve the supplied number and use then to return twice its value.
Expected output: 8 (print this with console.log)
function doubleResolvedValue(value) {
// Write your solution here
}
console.log(await doubleResolvedValue(4));function doubleResolvedValue(value) {
return Promise.resolve(value).then((number) => number * 2);
}
console.log(await doubleResolvedValue(4));Question 4: Format a Resolved Name Completed
Resolve the supplied name and use then to return "Hello, " followed by the name.
Expected output: "Hello, Ada" (print this with console.log)
function formatResolvedGreeting(name) {
// Write your solution here
}
console.log(await formatResolvedGreeting("Ada"));function formatResolvedGreeting(name) {
return Promise.resolve(name).then((value) => "Hello, " + value);
}
console.log(await formatResolvedGreeting("Ada"));Question 5: Chain Two Arithmetic Steps Completed
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)
function chainTwoPromiseSteps(number) {
// Write your solution here
}
console.log(await chainTwoPromiseSteps(4));function chainTwoPromiseSteps(number) {
return Promise.resolve(number).then((value) => value + 3).then((value) => value * 2);
}
console.log(await chainTwoPromiseSteps(4));Question 6: Construct a Resolved Promise Completed
Return a new Promise that resolves with the supplied text.
Expected output: "ready" (print this with console.log)
function constructResolvedText(text) {
// Write your solution here
}
console.log(await constructResolvedText("ready"));function constructResolvedText(text) {
return new Promise((resolve) => resolve(text));
}
console.log(await constructResolvedText("ready"));Question 7: Resolve an Array Length Completed
Resolve the supplied array and use then to return its length.
Expected output: 2 (print this with console.log)
function resolvedArrayLength(items) {
// Write your solution here
}
console.log(await resolvedArrayLength(["a","b"]));function resolvedArrayLength(items) {
return Promise.resolve(items).then((values) => values.length);
}
console.log(await resolvedArrayLength(["a","b"]));Question 8: Resolve a Trimmed String Completed
Resolve the supplied string and use then to return it without outer whitespace.
Expected output: "hello" (print this with console.log)
function trimResolvedText(text) {
// Write your solution here
}
console.log(await trimResolvedText(" hello "));function trimResolvedText(text) {
return Promise.resolve(text).then((value) => value.trim());
}
console.log(await trimResolvedText(" hello "));Question 9: Resolve a Record Field Completed
Resolve the supplied object and use then to return its score property.
Expected output: 9 (print this with console.log)
function resolvedRecordScore(record) {
// Write your solution here
}
console.log(await resolvedRecordScore({"score":9}));function resolvedRecordScore(record) {
return Promise.resolve(record).then((value) => value.score);
}
console.log(await resolvedRecordScore({"score":9}));Question 10: Return a Promise from Then Completed
Resolve the supplied number, then return a second resolved promise containing the number plus 5.
Expected output: 11 (print this with console.log)
function returnPromiseFromThen(number) {
// Write your solution here
}
console.log(await returnPromiseFromThen(6));function returnPromiseFromThen(number) {
return Promise.resolve(number).then((value) => Promise.resolve(value + 5));
}
console.log(await returnPromiseFromThen(6));