forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex3-rollAnAce.js
More file actions
40 lines (36 loc) · 1.4 KB
/
ex3-rollAnAce.js
File metadata and controls
40 lines (36 loc) · 1.4 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
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/blob/main/3-UsingAPIs/Week2/README.md#exercise-3-roll-an-ace
1. Run the unmodified exercise and observe that it works as advertised. Observe
that the die must be thrown an indeterminate number of times until we get an
ACE or until it rolls off the table.
2. Now, rewrite the body of the `rollDieUntil()` function using async/await and
without using recursion. Hint: a `while` loop may come handy.
3. Refactor the function `main()` to use async/await and try/catch.
------------------------------------------------------------------------------*/
// ! Do not change or remove the next two lines
import { rollDie } from '../../helpers/pokerDiceRoller.js';
/** @import {DieFace} from "../../helpers/pokerDiceRoller.js" */
/**
* Rolls a die until the desired value is rolled.
* @param {DieFace} desiredValue
* @returns {Promise<DieFace>}
*/
export async function rollDieUntil(desiredValue) {
let value;
while (value !== desiredValue) {
value = await rollDie();
}
return value;
}
async function main() {
try {
const result = await rollDieUntil('ACE');
console.log('Resolved!', result);
} catch (error) {
console.log('Rejected!', error.message);
}
}
// ! Do not change or remove the code below
if (process.env.NODE_ENV !== 'test') {
main();
}