Skip to content

Week1 UsingAPIs: solve all 5 exercises#15

Open
Hadidreem17 wants to merge 3 commits intoHackYourAssignment:mainfrom
Hadidreem17:Hadidreem17-w1-UsingAPIs
Open

Week1 UsingAPIs: solve all 5 exercises#15
Hadidreem17 wants to merge 3 commits intoHackYourAssignment:mainfrom
Hadidreem17:Hadidreem17-w1-UsingAPIs

Conversation

@Hadidreem17
Copy link
Copy Markdown

No description provided.

@remarcmij remarcmij self-assigned this Oct 9, 2025
Copy link
Copy Markdown

@remarcmij remarcmij left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Hadidreem17, there are some issues that I would like you to have a look at. Please let me know on Slack if you need help with fixing them.

getAnonName('John', console.log);
getAnonName('John')
.then(console.log)
.catch(err => console.error(err.message));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


checkDoubleDigits(100) // should reject
.then((message) => console.log(message))
.catch((error) => console.log(error.message));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove function main()?

} else {
reject(new Error(`Expected a double digit number but got ${number}`));
}
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


if (roll > 6) {
reject(new Error('Oops... Die rolled off the table.'));
return;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exercise description states that the die should complete its scheduled rolls, even if the die rolls off the table. The return statement that you added here breaks off the scheduled rolls. So, for this exercise, remove the return statement and explain what happens.

Current output example (only 7 rolls of the scheduled 10 rolls):

Die scheduled for 10 rolls...
Die value is now: 2
Die value is now: 3
Die value is now: 1
Die value is now: 4
Die value is now: 4
Die value is now: 4
Die value is now: 1
Oops... Die rolled off the table.

Expected output example (all 10 rolls completed):

Die scheduled for 10 rolls...
Die value is now: 2
Die value is now: 3
Die value is now: 1
Die value is now: 4
Die value is now: 4
Die value is now: 4
Die value is now: 1
Oops... Die rolled off the table.
Die value is now: ...
Die value is now: ...
Die value is now: ...


if (roll === randomRollsToDo) {
resolve(value);
return;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove:

Suggested change
return;

main();
}

// TODO Replace this comment by your explanation that was asked for in the assignment description.
Copy link
Copy Markdown

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?

const dicePromises = dice.map((die) => rollDie(die));


return Promise.all(dicePromises);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwanted extra blank lines.

return rollDie(1);


const dicePromises = dice.map((die) => rollDie(die));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

main();
}

// TODO Replace this comment by your explanation that was asked for in the assignment description.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss your explanation.

export function rollDice() {
const results = [];

// TODO: expand the chain to include five dice
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A chain was asked for, not a Promise.all(). Please fix this.

Copy link
Copy Markdown

@remarcmij remarcmij left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Hadidreem17, thanks for the updates. I will now approve your PR.

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.
*/
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 return statement, it was not in the original code and it was not intended for you to add it.) The promise version solves it because once a promise is settled its outcome can no longer be changed. So calling resolve() after reject() does not change a rejected promise to a resolved promise.

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.
*/
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A rejected Promsise.all() does not stop the other still running promises.

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.
*/
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants