-
Notifications
You must be signed in to change notification settings - Fork 4
Week1 UsingAPIs: solve all 5 exercises #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| ## Test Summary | ||
|
|
||
| **Mentors**: For more information on how to review homework assignments, please refer to the [Review Guide](https://github.com/HackYourFuture/mentors/blob/main/assignment-support/review-guide.md). | ||
|
|
||
| ### 3-UsingAPIs - Week1 | ||
|
|
||
| | Exercise | Passed | Failed | ESLint | | ||
| |-----------------------|--------|--------|--------| | ||
| | ex1-johnWho | 9 | - | ✓ | | ||
| | ex2-checkDoubleDigits | 11 | - | ✓ | | ||
| | ex3-rollDie | 7 | - | ✓ | | ||
| | ex4-pokerDiceAll | 7 | - | ✓ | | ||
| | ex5-pokerDiceChain | 5 | - | ✓ | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,10 +11,16 @@ Complete the function called `checkDoubleDigits` such that: | |
| "Expected a double digit number but got `number`", where `number` is the | ||
| number that was passed as an argument. | ||
| ------------------------------------------------------------------------------*/ | ||
| export function checkDoubleDigits(/* TODO add parameter(s) here */) { | ||
| // TODO complete this function | ||
| } | ||
|
|
||
| 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}`)); | ||
| } | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
| function main() { | ||
| checkDoubleDigits(9) // should reject | ||
| .then((message) => console.log(message)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -10,53 +10,45 @@ Full description at: https://github.com/HackYourFuture/Assignments/tree/main/3-U | |||
| explanation? Add your answer as a comment to be bottom of the file. | ||||
| ------------------------------------------------------------------------------*/ | ||||
|
|
||||
| // TODO Remove callback and return a promise | ||||
| export function rollDie(callback) { | ||||
| // Compute a random number of rolls (3-10) that the die MUST complete | ||||
| const randomRollsToDo = Math.floor(Math.random() * 8) + 3; | ||||
| console.log(`Die scheduled for ${randomRollsToDo} rolls...`); | ||||
|
|
||||
| const rollOnce = (roll) => { | ||||
| // Compute a random die value for the current roll | ||||
| const value = Math.floor(Math.random() * 6) + 1; | ||||
| console.log(`Die value is now: ${value}`); | ||||
|
|
||||
| // Use callback to notify that the die rolled off the table after 6 rolls | ||||
| if (roll > 6) { | ||||
| // TODO replace "error" callback | ||||
| callback(new Error('Oops... Die rolled off the table.')); | ||||
| } | ||||
|
|
||||
| // Use callback to communicate the final die value once finished rolling | ||||
| if (roll === randomRollsToDo) { | ||||
| // TODO replace "success" callback | ||||
| callback(null, value); | ||||
| } | ||||
|
|
||||
| // Schedule the next roll todo until no more rolls to do | ||||
| if (roll < randomRollsToDo) { | ||||
| setTimeout(() => rollOnce(roll + 1), 500); | ||||
| } | ||||
| }; | ||||
|
|
||||
| // Start the initial roll | ||||
| rollOnce(1); | ||||
| export function rollDie() { | ||||
| return new Promise((resolve, reject) => { | ||||
| const randomRollsToDo = Math.floor(Math.random() * 8) + 3; | ||||
| console.log(`Die scheduled for ${randomRollsToDo} rolls...`); | ||||
| const rollOnce = (roll) => { | ||||
| const value = Math.floor(Math.random() * 6) + 1; | ||||
| console.log(`Die value is now: ${value}`); | ||||
| if (roll > 6) { | ||||
| reject(new Error('Oops... Die rolled off the table.')); | ||||
| } | ||||
| if (roll === randomRollsToDo) { | ||||
| resolve(value); | ||||
| return; | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove:
Suggested change
|
||||
| } | ||||
| if (roll < randomRollsToDo) { | ||||
| setTimeout(() => rollOnce(roll + 1), 500); | ||||
| } | ||||
| }; | ||||
| rollOnce(1); | ||||
| }); | ||||
| } | ||||
|
|
||||
| function main() { | ||||
| // TODO Refactor to use promise | ||||
| rollDie((error, value) => { | ||||
| if (error !== null) { | ||||
| console.log(error.message); | ||||
| } else { | ||||
| rollDie() | ||||
| .then((value) => { | ||||
| console.log(`Success! Die settled on ${value}.`); | ||||
| } | ||||
| }); | ||||
| }) | ||||
| .catch((error) => { | ||||
| console.log(error.message); | ||||
| }); | ||||
| } | ||||
|
|
||||
| // ! Do not change or remove the code below | ||||
| if (process.env.NODE_ENV !== 'test') { | ||||
| main(); | ||||
| } | ||||
|
|
||||
| // TODO Replace this comment by your explanation that was asked for in the assignment description. | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is your explanation that was asked here? |
||||
| /* | ||||
| Explanation: | ||||
| Removing the `return` after `reject(...)` ensures that `rollOnce` continues to | ||||
| schedule and log the remaining rolls (up to the scheduled count), even after the | ||||
| promise has been rejected because the die "rolled off the table". This matches | ||||
| the exercise requirement: all scheduled rolls should complete, while the promise | ||||
| still settles as a rejection at the moment of the "off the table" event. | ||||
| */ | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The actual question was to explain how the promise version solves the problem observed with the callback version. (You added the |
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,26 +21,31 @@ yet finished their roll continue to do so. | |
| Can you explain why? Please add your answer as a comment to the end of the | ||
| exercise file. | ||
| ------------------------------------------------------------------------------*/ | ||
| /*------------------------------------------------------------------------------ | ||
| Full description at: https://github.com/HackYourFuture/Assignments/tree/main/3-UsingAPIs/Week1#exercise-4-throw-the-dice-for-a-poker-dice-game | ||
| ------------------------------------------------------------------------------*/ | ||
|
|
||
| // The line below makes the rollDie() function available to this file. | ||
| // Do not change or remove it. | ||
| import { rollDie } from '../../helpers/pokerDiceRoller.js'; | ||
|
|
||
| export function rollDice() { | ||
| // TODO Refactor this function | ||
| const dice = [1, 2, 3, 4, 5]; | ||
| return rollDie(1); | ||
| const dicePromises = dice.map((die) => rollDie(die)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return Promise.all(dicePromises); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unwanted extra blank lines. |
||
| } | ||
|
|
||
| function main() { | ||
| rollDice() | ||
| .then((results) => console.log('Resolved!', results)) | ||
| .catch((error) => console.log('Rejected!', error.message)); | ||
| } | ||
|
|
||
| // ! Do not change or remove the code below | ||
| if (process.env.NODE_ENV !== 'test') { | ||
| main(); | ||
| } | ||
|
|
||
| // TODO Replace this comment by your explanation that was asked for in the assignment description. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I miss your explanation. |
||
| /* | ||
| Explanation: | ||
| `Promise.all()` runs all five dice rolls at the same time. | ||
| It returns a single Promise that resolves when all the dice finish rolling | ||
| and provides an array with all their results. | ||
| If any one die rejects (for example, rolls off the table), | ||
| the entire `Promise.all()` rejects immediately with that error. | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A rejected |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,19 +10,28 @@ To throw the dice sequentially we will be using a _promise chain_. Your job is | |
| to expand the given promise chain to include five dice. | ||
| ------------------------------------------------------------------------------*/ | ||
|
|
||
| // The line below makes the rollDie() function available to this file. | ||
| // Do not change or remove it. | ||
| import { rollDie } from '../../helpers/pokerDiceRoller.js'; | ||
|
|
||
| export function rollDice() { | ||
| const results = []; | ||
|
|
||
| // TODO: expand the chain to include five dice | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A chain was asked for, not a |
||
| return rollDie(1) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return rollDie(2); | ||
| }) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return rollDie(3); | ||
| }) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return rollDie(4); | ||
| }) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return rollDie(5); | ||
| }) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return results; | ||
|
|
@@ -39,3 +48,12 @@ function main() { | |
| if (process.env.NODE_ENV !== 'test') { | ||
| main(); | ||
| } | ||
|
|
||
| /* | ||
| Explanation: | ||
| In this exercise, we use a Promise **chain** instead of `Promise.all()`. | ||
| Each `.then()` waits for the previous die to finish rolling before starting the next one. | ||
| This ensures the dice are rolled sequentially, not in parallel. | ||
| If any die rejects (for example, it falls off the table), | ||
| the chain stops immediately and the final Promise rejects with that error. | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| *** Unit Test Error Report *** | ||
|
|
||
| PASS .dist/3-UsingAPIs/Week1/unit-tests/ex1-johnWho.test.js | ||
| api-wk1-ex1-johnWho | ||
| ✅ should exist and be executable (2 ms) | ||
| ✅ should have all TODO comments removed (1 ms) | ||
| ✅ `getAnonName` should not contain unneeded console.log calls | ||
| ✅ should call `new Promise()` (1 ms) | ||
| ✅ should take a single argument | ||
| ✅ `resolve()` should be called with a one argument | ||
| ✅ `reject()` should be called with a one argument | ||
| ✅ should resolve when called with a string argument (1 ms) | ||
| ✅ should reject with an Error object when called without an argument (1 ms) | ||
|
|
||
| Test Suites: 1 passed, 1 total | ||
| Tests: 9 passed, 9 total | ||
| Snapshots: 0 total | ||
| Time: 0.772 s, estimated 1 s | ||
| Ran all test suites matching /C:\\Users\\Rimha\\Assignments-Cohort54\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex1-johnWho.test.js/i. | ||
| No linting errors detected. | ||
| No spelling errors detected. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| *** Unit Test Error Report *** | ||
|
|
||
| PASS .dist/3-UsingAPIs/Week1/unit-tests/ex2-checkDoubleDigits.test.js | ||
| api-wk1-ex2-checkDoubleDigits | ||
| ✅ should exist and be executable (2 ms) | ||
| ✅ should have all TODO comments removed (1 ms) | ||
| ✅ `checkDoubleDigits` should not contain unneeded console.log calls | ||
| ✅ should call new Promise() | ||
| ✅ `resolve()` should be called with a one argument | ||
| ✅ `reject()` should be called with a one argument (1 ms) | ||
| ✅ should be a function that takes a single argument | ||
| ✅ (9) should return a rejected promise with an Error object (1 ms) | ||
| ✅ (10) should return a promise that resolves to "This is a double digit number!" (1 ms) | ||
| ✅ (99) should return a promise that resolves to "This is a double digit number!" | ||
| ✅ (100) should return a rejected promise with an Error object | ||
|
|
||
| Test Suites: 1 passed, 1 total | ||
| Tests: 11 passed, 11 total | ||
| Snapshots: 0 total | ||
| Time: 0.785 s, estimated 1 s | ||
| Ran all test suites matching /C:\\Users\\Rimha\\Assignments-Cohort54\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex2-checkDoubleDigits.test.js/i. | ||
| No linting errors detected. | ||
| No spelling errors detected. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| *** Unit Test Error Report *** | ||
|
|
||
| PASS .dist/3-UsingAPIs/Week1/unit-tests/ex3-rollDie.test.js | ||
| api-wk1-ex3-rollDie | ||
| ✅ should exist and be executable (2 ms) | ||
| ✅ should have all TODO comments removed (1 ms) | ||
| ✅ should call `new Promise()` | ||
| ✅ `resolve()` should be called with a one argument (1 ms) | ||
| ✅ `reject()` should be called with a one argument | ||
| ✅ should resolve when the die settles successfully (1 ms) | ||
| ✅ should reject with an Error when the die rolls off the table (1 ms) | ||
|
|
||
| Test Suites: 1 passed, 1 total | ||
| Tests: 7 passed, 7 total | ||
| Snapshots: 0 total | ||
| Time: 0.789 s, estimated 1 s | ||
| Ran all test suites matching /C:\\Users\\Rimha\\Assignments-Cohort54\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex3-rollDie.test.js/i. | ||
| No linting errors detected. | ||
| No spelling errors detected. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| *** Unit Test Error Report *** | ||
|
|
||
| PASS .dist/3-UsingAPIs/Week1/unit-tests/ex4-pokerDiceAll.test.js | ||
| api-wk1-ex4-pokerDiceAll | ||
| ✅ should exist and be executable (2 ms) | ||
| ✅ should have all TODO comments removed (1 ms) | ||
| ✅ `rollDice` should not contain unneeded console.log calls | ||
| ✅ should use `dice.map()` (1 ms) | ||
| ✅ should use `Promise.all()` | ||
| ✅ should resolve when all dice settle successfully (11 ms) | ||
| ✅ should reject with an Error when a die rolls off the table (68 ms) | ||
|
|
||
| Test Suites: 1 passed, 1 total | ||
| Tests: 7 passed, 7 total | ||
| Snapshots: 0 total | ||
| Time: 0.874 s, estimated 1 s | ||
| Ran all test suites matching /C:\\Users\\Rimha\\Assignments-Cohort54\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex4-pokerDiceAll.test.js/i. | ||
| No linting errors detected. | ||
| No spelling errors detected. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| *** Unit Test Error Report *** | ||
|
|
||
| PASS .dist/3-UsingAPIs/Week1/unit-tests/ex5-pokerDiceChain.test.js | ||
| api-wk1-ex5-pokerDiceChain | ||
| ✅ should exist and be executable (1 ms) | ||
| ✅ should have all TODO comments removed | ||
| ✅ `rollDice` should not contain unneeded console.log calls (1 ms) | ||
| ✅ should resolve when all dice settle successfully (101 ms) | ||
| ✅ should reject with an Error when a die rolls off the table (92 ms) | ||
|
|
||
| Test Suites: 1 passed, 1 total | ||
| Tests: 5 passed, 5 total | ||
| Snapshots: 0 total | ||
| Time: 1.05 s | ||
| Ran all test suites matching /C:\\Users\\Rimha\\Assignments-Cohort54\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex5-pokerDiceChain.test.js/i. | ||
| No linting errors detected. | ||
| No spelling errors detected. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍