-
Notifications
You must be signed in to change notification settings - Fork 17
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
rizan_ibrahim-UsingAPIs-w1-assingment #57
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,10 @@ | ||
## 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 | - | ✕ | |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,53 +10,50 @@ 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) { | ||
export function rollDie() { | ||
// 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); | ||
return new Promise((resolve, reject) => { | ||
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) { | ||
reject(new Error('Oops... Die rolled off the table.')); | ||
} | ||
|
||
// Use callback to communicate the final die value once finished rolling | ||
if (roll === randomRollsToDo) { | ||
resolve(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); | ||
}); | ||
} | ||
|
||
function main() { | ||
// TODO Refactor to use promise | ||
rollDie((error, value) => { | ||
if (error !== null) { | ||
console.log(error.message); | ||
} else { | ||
console.log(`Success! Die settled on ${value}.`); | ||
} | ||
}); | ||
rollDie() | ||
.then((value) => console.log(`success! Die value is: ${value}`)) | ||
.catch((error) => console.log(error.message)); | ||
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. 👍 |
||
} | ||
|
||
// ! Do not change or remove the code below | ||
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. There is still a TODO left to be done. |
||
if (process.env.NODE_ENV !== 'test') { | ||
main(); | ||
} | ||
|
||
// TODO Replace this comment by your explanation that was asked for in the assignment description. | ||
/* | ||
Explanation: | ||
The `rollDie` function simulates rolling a die until it lands on a value of 6. It uses a recursive function `rollOnce` to keep rolling the die. The function returns a promise that resolves when the die lands on 6 and rejects if an error occurs. The `rollOnce` function logs each attempt and rolls the die again after a 1-second delay if the value is not 6. | ||
*/ | ||
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. This explains what the promise version does, not why the behaviour is different from the callback version of the code. The key point here is that when a die rolls off the table if calls |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,11 @@ exercise file. | |
import { rollDie } from '../../helpers/pokerDiceRoller.js'; | ||
|
||
export function rollDice() { | ||
// TODO Refactor this function | ||
|
||
const dice = [1, 2, 3, 4, 5]; | ||
return rollDie(1); | ||
const promises = dice.map(rollDie); | ||
return Promise.all(promises); | ||
|
||
} | ||
|
||
function main() { | ||
|
@@ -43,4 +45,7 @@ if (process.env.NODE_ENV !== 'test') { | |
main(); | ||
} | ||
|
||
// TODO Replace this comment by your explanation that was asked for in the assignment description. | ||
/* | ||
Explanation: | ||
When using `Promise.all`, if one of the promises is rejected, the entire `Promise.all` is rejected immediately with that reason. However, the other promises that have not yet finished their roll will continue to execute. This is because `Promise.all` does not cancel the remaining promises; it simply stops waiting for them once one promise is rejected. | ||
*/ | ||
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. Excellent explanation. This is exactly right. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,24 @@ import { rollDie } from '../../helpers/pokerDiceRoller.js'; | |
export function rollDice() { | ||
const results = []; | ||
|
||
// TODO: expand the chain to include five dice | ||
// Expand the chain to include five dice | ||
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); | ||
}) | ||
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. 👍 |
||
.then((value) => { | ||
results.push(value); | ||
return results; | ||
|
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 | ||
|
||
Test Suites: 1 passed, 1 total | ||
Tests: 9 passed, 9 total | ||
Snapshots: 0 total | ||
Time: 0.497 s, estimated 1 s | ||
Ran all test suites matching /C:\\Users\\rizan\\Assignments-cohort51\\.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 | ||
✅ should be a function that takes a single argument (1 ms) | ||
✅ (9) should return a rejected promise with an Error object | ||
✅ (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!" (1 ms) | ||
✅ (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.629 s | ||
Ran all test suites matching /C:\\Users\\rizan\\Assignments-cohort51\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex2-checkDoubleDigits.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.
👍