forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex2-checkDoubleDigits.js
More file actions
45 lines (38 loc) · 1.63 KB
/
ex2-checkDoubleDigits.js
File metadata and controls
45 lines (38 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/3-UsingAPIs/Week1#exercise-2-is-it-a-double-digit-number
Complete the function called `checkDoubleDigits` such that:
- It takes one argument: a number
- It returns a `new Promise`.
- If the number between 10 and 99 it should resolve to the string
"This is a double digit number!".
- For any other number it should reject with an an Error object containing:
"Expected a double digit number but got `number`", where `number` is the
number that was passed as an argument.
------------------------------------------------------------------------------*/
export function checkDoubleDigits(number) {
return new Promise((resolve, reject) => {
if (number >= 10 && number <= 99) {
resolve("This is a double digit number!");
} else {
reject(new Error(`Expected a double digit number but got ${number}`));
}
});
}
function main() {
checkDoubleDigits(9) // should reject
.then((message) => console.log(message))
.catch((error) => console.log(error.message));
checkDoubleDigits(10) // should resolve
.then((message) => console.log(message))
.catch((error) => console.log(error.message));
checkDoubleDigits(99) // should resolve
.then((message) => console.log(message))
.catch((error) => console.log(error.message));
checkDoubleDigits(100) // should reject
.then((message) => console.log(message))
.catch((error) => console.log(error.message));
}
// ! Do not change or remove the code below
if (process.env.NODE_ENV !== 'test') {
main();
}