Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions 3-UsingAPIs/Week1/assignment/ex1-johnWho.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/3-UsingAPIs/Week1#exercise-1-john-who
Full description at:
https://github.com/HackYourFuture/Assignments/tree/main/3-UsingAPIs/Week1#exercise-1-john-who

Rewrite this function, but replace the callback syntax with the Promise syntax:
- Have the `getAnonName` function return a `new Promise`.
- If the Promise `resolves`, pass the full name as an argument to resolve with.
- If the Promise `rejects`, pass an error as the argument to reject with: "You
didn't pass in a first name!"
Rewrite this function using Promise syntax:
- Return a new Promise from getAnonName.
- resolve with the full name.
- reject with: "You didn't pass in a first name!"
------------------------------------------------------------------------------*/
// TODO see above
export const getAnonName = (firstName, callback) => {
setTimeout(() => {
if (!firstName) {
callback(new Error("You didn't pass in a first name!"));
return;
}

const fullName = `${firstName} Doe`;

callback(fullName);
}, 1000);
export const getAnonName = (firstName) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (!firstName) {
reject(new Error("You didn't pass in a first name!"));
return;
}
const fullName = `${firstName} Doe`;
resolve(fullName);
}, 1000);
});
};

function main() {
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.

👍

}

// ! Do not change or remove the code below
Expand Down
27 changes: 9 additions & 18 deletions 3-UsingAPIs/Week1/assignment/ex2-checkDoubleDigits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
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()?

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}`));
}
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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();
Expand Down
76 changes: 37 additions & 39 deletions 3-UsingAPIs/Week1/assignment/ex3-rollDie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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;

}

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);
});
}





Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do not use multiple blank lines to separate code segments. A single blank line will do. Normally the Prettier VS Code extension removes extra blank lines. It looks like Prettier is not correctly set up in your VS Code.

Suggested change

// ! 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.
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?


21 changes: 16 additions & 5 deletions 3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍



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.

}

function main() {
Expand All @@ -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.
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.

26 changes: 12 additions & 14 deletions 3-UsingAPIs/Week1/assignment/ex5-pokerDiceChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.

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() {
Expand All @@ -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();
Expand Down