-
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 1 commit
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 |
|---|---|---|
|
|
@@ -11,28 +11,19 @@ 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 | ||
| } | ||
|
|
||
| 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)); | ||
|
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. Why did you remove |
||
| 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. 👍 |
||
| } | ||
|
|
||
|
|
||
| // ! Do not change or remove the code below | ||
| if (process.env.NODE_ENV !== 'test') { | ||
| main(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -10,53 +10,51 @@ 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.')); | ||||
| return; | ||||
|
||||
| } | ||||
|
|
||||
| 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); | ||||
| }); | ||||
| } | ||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
||||
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.
Where is your explanation that was asked here?
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,15 +21,21 @@ 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() { | ||
|
|
@@ -38,9 +44,14 @@ function main() { | |
| .catch((error) => console.log('Rejected!', error.message)); | ||
| } | ||
|
|
||
| if (process.env.NODE_ENV !== 'test') { | ||
| main(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // ! 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. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,23 +10,15 @@ 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 results; | ||
| }); | ||
| const dice = [1, 2, 3, 4, 5]; | ||
|
|
||
| const dicePromises = dice.map((die) => rollDie(die)); | ||
|
|
||
|
|
||
| return Promise.all(dicePromises); | ||
| } | ||
|
|
||
| function main() { | ||
|
|
@@ -35,6 +27,12 @@ function main() { | |
| .catch((error) => console.log('Rejected!', error.message)); | ||
| } | ||
|
|
||
| if (process.env.NODE_ENV !== 'test') { | ||
| main(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // ! Do not change or remove the code below | ||
| if (process.env.NODE_ENV !== 'test') { | ||
| main(); | ||
|
|
||
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.
👍