forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathex2-checkDoubleDigits.js
53 lines (46 loc) · 1.76 KB
/
ex2-checkDoubleDigits.js
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
46
47
48
49
50
51
52
53
/*------------------------------------------------------------------------------
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 (is2Digit(number)) {
resolve("This is a double digit number!");
} else {
reject(new Error(`Expected a double digit number but got ${number}`));
}
});
}
const is2Digit = (number) => {
let count = 0;
while (number) {
number = Math.floor((number /= 10));
count++;
}
return count === 2;
};
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();
}