Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions .test-summary/TEST_SUMMARY.md
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 | - | ✓ |
26 changes: 14 additions & 12 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

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!"
------------------------------------------------------------------------------*/
// TODO see above
export const getAnonName = (firstName, callback) => {
setTimeout(() => {
if (!firstName) {
callback(new Error("You didn't pass in a first name!"));
return;
}
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`;
const fullName = `${firstName} Doe`;

callback(fullName);
}, 1000);
resolve(fullName);
}, 1000);
});
};

function main() {
getAnonName('John', console.log);
getAnonName('John')
.then((response) => console.log(response))
.catch((err) => console.log(err.message));
}

// ! Do not change or remove the code below
Expand Down
10 changes: 8 additions & 2 deletions 3-UsingAPIs/Week1/assignment/ex2-checkDoubleDigits.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ 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(num) {
return new Promise((resolve, reject) => {
if (num >= 10 && num <= 99) {
resolve('This is a double digit number!');
} else {
reject(new Error(`Expected a double digit number but got ${num}`));
}
});
}

function main() {
Expand Down
69 changes: 33 additions & 36 deletions 3-UsingAPIs/Week1/assignment/ex3-rollDie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.'));
return; //stop the func from continuing. code below won't run
}

// Use callback to communicate the final die value once finished rolling
if (roll === randomRollsToDo) {
//when it finishes resolve
resolve(value);
return;
}

// 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 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.
//Yes, I see a problem using callbacks. In the callback version, if randomRollsToDo is greater than 6, the function prints both the error and the success messages. This happens because the callback can be called multiple times, while, in the promise version, this problem no longer occurs. Once a promise is either fulfilled or rejected, it cannot be settled again.
10 changes: 7 additions & 3 deletions 3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ 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);

return Promise.all(
dice.map((ele) => {
return rollDie(ele); // rollDie() returns a promise
})
);
}

function main() {
Expand All @@ -43,4 +47,4 @@ if (process.env.NODE_ENV !== 'test') {
main();
}

// TODO Replace this comment by your explanation that was asked for in the assignment description.
// The remaining dice continue rolling because reject will only mark the promise as rejected but does not stop the execution of the code inside the function rolldie().To solve that we can use the return keyword right after reject(). In addition, after rejection, the function still schedules the next roll using setTimeout, so the die keeps rolling and logging updates even though its promise has already been rejected. This happens because promises in javaScript are not cancelable by default - once async operations like setTimeout are started they continue running unless explicitely stopped. Therefore each die runs independently, even if one promise is rejected, the other dice will continue their own rolls.

Choose a reason for hiding this comment

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

👍

13 changes: 12 additions & 1 deletion 3-UsingAPIs/Week1/assignment/ex5-pokerDiceChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ import { rollDie } from '../../helpers/pokerDiceRoller.js';
export function rollDice() {
const results = [];

// TODO: 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);
})
.then((value) => {
results.push(value);
return results;
Expand Down
21 changes: 21 additions & 0 deletions 3-UsingAPIs/Week1/test-reports/ex1-johnWho.report.txt
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 (3 ms)
✅ should have all TODO comments removed (1 ms)
✅ `getAnonName` should not contain unneeded console.log calls (1 ms)
✅ should call `new Promise()`
✅ 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 (2 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: 1.118 s
Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-Cohort54\/.dist\/3-UsingAPIs\/Week1\/unit-tests\/ex1-johnWho.test.js/i.
No linting errors detected.
No spelling errors detected.
23 changes: 23 additions & 0 deletions 3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.report.txt
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 (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 be a function that takes a single argument (1 ms)
✅ (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.598 s, estimated 1 s
Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-Cohort54\/.dist\/3-UsingAPIs\/Week1\/unit-tests\/ex2-checkDoubleDigits.test.js/i.
No linting errors detected.
No spelling errors detected.
19 changes: 19 additions & 0 deletions 3-UsingAPIs/Week1/test-reports/ex3-rollDie.report.txt
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()` (1 ms)
✅ `resolve()` should be called with a one argument
✅ `reject()` should be called with a one argument (1 ms)
✅ should resolve when the die settles successfully (2 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: 1.031 s
Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-Cohort54\/.dist\/3-UsingAPIs\/Week1\/unit-tests\/ex3-rollDie.test.js/i.
No linting errors detected.
No spelling errors detected.
24 changes: 24 additions & 0 deletions 3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*** Unit Test Error Report ***

PASS .dist/3-UsingAPIs/Week1/unit-tests/ex4-pokerDiceAll.test.js
api-wk1-ex4-pokerDiceAll
✅ should exist and be executable (4 ms)
✅ should have all TODO comments removed
✅ `rollDice` should not contain unneeded console.log calls (1 ms)
✅ 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 (29 ms)

Test Suites: 1 passed, 1 total
Tests: 7 passed, 7 total
Snapshots: 0 total
Time: 1.14 s
Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-Cohort54\/.dist\/3-UsingAPIs\/Week1\/unit-tests\/ex4-pokerDiceAll.test.js/i.
No linting errors detected.


*** Spell Checker Report ***

3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js:50:158 - Unknown word (rolldie)
3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js:50:576 - Unknown word (explicitely)
17 changes: 17 additions & 0 deletions 3-UsingAPIs/Week1/test-reports/ex5-pokerDiceChain.report.txt
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 (2 ms)
✅ should have all TODO comments removed
✅ `rollDice` should not contain unneeded console.log calls (1 ms)
✅ should resolve when all dice settle successfully (21 ms)
✅ should reject with an Error when a die rolls off the table (29 ms)

Test Suites: 1 passed, 1 total
Tests: 5 passed, 5 total
Snapshots: 0 total
Time: 0.493 s, estimated 1 s
Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-Cohort54\/.dist\/3-UsingAPIs\/Week1\/unit-tests\/ex5-pokerDiceChain.test.js/i.
No linting errors detected.
No spelling errors detected.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ We highly recommend that you go through this README in detail before starting to
>
> This will ensure that the line endings for text files are compatible with those used on MacOS and Linux-based computers.

1. Fork the `HackYourAssignment/Assignments-CohortXX` repository to your own GitHub account.
1. Fork the `HackYourAssignment/JavaScript-CohortXX` repository to your own GitHub account.
2. Clone the fork to your local computer.
3. Open the root folder of the repository in VSCode.
4. When invited to do so, please install the recommended VSCode extensions.
Expand All @@ -24,7 +24,7 @@ We highly recommend that you go through this README in detail before starting to
10. Fix any reported issues and rerun the test. Repeat until all issues are fixed.
11. When all assignments are done, commit all changed files. This includes the modified exercises, the generated test summary (`TEST_SUMMARY.md`) and test reports (`EXERCISE_NAME.report.txt`).
12. Push the changes to your fork.
13. Create a pull request against the `main` branch of the `HackYourAssignment/Assignments-CohortXX` repository. For the title of your pull request use the same format as the branch name, e.g.: `YOUR_NAME-w2-JavaScript`.
13. Create a pull request against the `main` branch of the `HackYourAssignment/JavaScript-CohortXX` repository. For the title of your pull request use the same format as the branch name, e.g.: `YOUR_NAME-w2-JavaScript`.

Repeat steps 6-13 for each week. For subsequent weeks the mandated branch names are:

Expand All @@ -39,7 +39,7 @@ For more information how to hand in your weekly assignments please refer to the

Throughout your [HYF journey](https://github.com/HackYourFuture/curriculum) you will be asked to do certain exercises. This repository contains all of these exercises for the JavaScript modules (JavaScript, Browsers, UsingAPIs). The module repositories will tell you how to hand in the assignment, the curriculum will indicate what week you will need to do.

> Note that a fork of this repository will be created on the [HackYourAssignment](https://github.com/HackYourAssignment) GitHub account specifically for your cohort. The name of the repository will have the format `Assignments-cohortXX` where `XX` is your cohort number,
> Note that a fork of this repository will be created on the [HackYourAssignment](https://github.com/HackYourAssignment) GitHub account specifically for your cohort. The name of the repository will have the format `JavaScript-cohortXX` where `XX` is your cohort number,

## Installation

Expand All @@ -64,9 +64,9 @@ From the command line, while inside the `Assignments-cohortXX` folder, you can u
code .
```

> When working on your assignments it is strongly recommended to always open the `Assignments-cohortXX` folder in VSCode rather than one of its sub-folders. This gives VSCode and all its extensions the full view on the repository for the best overall developer experience.
> When working on your assignments it is strongly recommended to always open the `JavaScript-cohortXX` folder in VSCode rather than one of its sub-folders. This gives VSCode and all its extensions the full view on the repository for the best overall developer experience.
>
> Note that the name of the folder opened in VSCode can always be found in the `EXPLORER` panel ( `ASSIGNMENTS_COHORT49` in the picture below):
> Note that the name of the folder opened in VSCode can always be found in the `EXPLORER` panel ( `ASSIGNMENT_COHORT49` in the picture below):
>
> ![folder-name](./assets/folder-name.png)

Expand Down