Skip to content

Ilias_khugaev_W1UsingAPIs #39

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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 | - | ✓ |
20 changes: 20 additions & 0 deletions 2-Browsers/Week1/test-reports/ex1-bookList.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
*** Unit Test Error Report ***

(node:18820) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
PASS .dist/2-Browsers/Week1/unit-tests/ex1-bookList.test.js (11.027 s)
br-wk1-ex1-bookList
✅ HTML should be syntactically valid (116 ms)
✅ should have all TODO comments removed
✅ should contain a <ul> that is a child of <div id="bookList"> (1 ms)
✅ should contain a <ul> with 3 <li> elements (1 ms)
✅ should contain an <li> with title and author for each book of the `myBooks` array (2 ms)
✅ should contain an <img> element for each book

Test Suites: 1 passed, 1 total
Tests: 6 passed, 6 total
Snapshots: 0 total
Time: 11.688 s
Ran all test suites matching /C:\\Users\\Shnek\\hackyourfuture\\Assignments-cohort52\\.dist\\2-Browsers\\Week1\\unit-tests\\ex1-bookList.test.js/i.
No linting errors detected.
No spelling errors detected.
26 changes: 26 additions & 0 deletions 2-Browsers/Week1/test-reports/ex2-aboutMe.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
*** Unit Test Error Report ***

(node:16988) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
PASS .dist/2-Browsers/Week1/unit-tests/ex2-aboutMe.test.js
br-wk1-ex2-aboutMe
✅ should be syntactically valid (116 ms)
✅ should have all TODO comments removed (1 ms)
✅ each <li> should have the CSS class `list-item`
✅ each <li> should rendered red (= rgb(255, 0, 0)) (16 ms)

Test Suites: 1 passed, 1 total
Tests: 4 passed, 4 total
Snapshots: 0 total
Time: 4.391 s
Ran all test suites matching /C:\\Users\\Shnek\\hackyourfuture\\Assignments-cohort52\\.dist\\2-Browsers\\Week1\\unit-tests\\ex2-aboutMe.test.js/i.
No linting errors detected.


*** Spell Checker Report ***

2-Browsers/Week1/assignment/ex2-aboutMe/index.js:12:25 - Unknown word (Ilias)
2-Browsers/Week1/assignment/ex2-aboutMe/index.js:12:31 - Unknown word (Khugaev)
2-Browsers/Week1/assignment/ex2-aboutMe/index.js:13:7 - Unknown word (favfood)
2-Browsers/Week1/assignment/ex2-aboutMe/index.js:14:1 - Unknown word (favfood)
2-Browsers/Week1/assignment/ex2-aboutMe/index.js:16:25 - Unknown word (Geldagan)
19 changes: 19 additions & 0 deletions 2-Browsers/Week1/test-reports/ex3-hijackLogo.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*** Unit Test Error Report ***

PASS .dist/2-Browsers/Week1/unit-tests/ex3-hijackLogo.test.js
br-wk1-ex3-hijackLogo
✅ should have all TODO comments removed (2 ms)
✅ should set the `.src` property (1 ms)
✅ should set the `.srcset` property

Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 0.59 s, estimated 1 s
Ran all test suites matching /C:\\Users\\Shnek\\hackyourfuture\\Assignments-cohort52\\.dist\\2-Browsers\\Week1\\unit-tests\\ex3-hijackLogo.test.js/i.
No linting errors detected.


*** Spell Checker Report ***

2-Browsers/Week1/assignment/ex3-hijackLogo.js:12:30 - Unknown word (Xdpd)
20 changes: 20 additions & 0 deletions 2-Browsers/Week1/test-reports/ex4-whatsTheTime.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
*** Unit Test Error Report ***

(node:17104) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
PASS .dist/2-Browsers/Week1/unit-tests/ex4-whatsTheTime.test.js (5.654 s)
br-wk1-ex4-whatsTheTime
✅ HTML should be syntactically valid (111 ms)
✅ should have all TODO comments removed (1 ms)
✅ should use `setInterval()`
✅ should not call `setInterval()` more than once (2011 ms)
✅ should use `window.onload` or `window.addEventListener()` for the `load` or `DOMContentLoaded` event
✅ `window.onload` or `window.addEventListener` should not call its event handler function

Test Suites: 1 passed, 1 total
Tests: 6 passed, 6 total
Snapshots: 0 total
Time: 6.189 s
Ran all test suites matching /C:\\Users\\Shnek\\hackyourfuture\\Assignments-cohort52\\.dist\\2-Browsers\\Week1\\unit-tests\\ex4-whatsTheTime.test.js/i.
No linting errors detected.
No spelling errors detected.
19 changes: 19 additions & 0 deletions 2-Browsers/Week1/test-reports/ex5-catWalk.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*** Unit Test Error Report ***

(node:21288) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
PASS .dist/2-Browsers/Week1/unit-tests/ex5-catWalk.test.js
br-wk1-ex5-catWalk
✅ HTML should be syntactically valid (114 ms)
✅ should have all TODO comments removed (1 ms)
✅ should use `setInterval()` and/or `setTimeout()`
✅ should use `window.onload` or `window.addEventListener()` for the `load` or `DOMContentLoaded` event
✅ `window.onload` or `window.addEventListener` should not call its event handler function

Test Suites: 1 passed, 1 total
Tests: 5 passed, 5 total
Snapshots: 0 total
Time: 3.427 s
Ran all test suites matching /C:\\Users\\Shnek\\hackyourfuture\\Assignments-cohort52\\.dist\\2-Browsers\\Week1\\unit-tests\\ex5-catWalk.test.js/i.
No linting errors detected.
No spelling errors detected.
25 changes: 14 additions & 11 deletions 3-UsingAPIs/Week1/assignment/ex1-johnWho.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ Rewrite this function, but replace the callback syntax with the Promise syntax:
- 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) => {

const fullName = `${firstName} Doe`;
return new Promise((resolve, reject) => {
if (!firstName) {
reject(new Error("You didn't pass in a first name!"))
} else {
const fullName = `${firstName} Doe`;
resolve(fullName)
};
Comment on lines +13 to +18

Choose a reason for hiding this comment

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

Excellent!

  1. You can also "exit early" when rejecting the promise. The idea is to check for errors at the start of the function body and return "early" in case of errors. This eliminates the need for an else clause and also requires one less indentation level on the remainder of the code. For a short function like the one here it does not make much of a difference but when you function becomes larger it will improve readability.

  2. Your VSCode Prettier setup is not working correctly as the code is not automatically formatted according to the Prettier rules. Send me a DM on Slack so we can fix that for you.

Suggested change
if (!firstName) {
reject(new Error("You didn't pass in a first name!"))
} else {
const fullName = `${firstName} Doe`;
resolve(fullName)
};
if (!firstName) {
reject(new Error("You didn't pass in a first name!"));
return;
}
const fullName = `${firstName} Doe`;
resolve(fullName);
});


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

function main() {
getAnonName('John', console.log);
getAnonName('John')
.then(name => console.log(name))
.catch(error => console.log(error.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(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}`))
}
})

Choose a reason for hiding this comment

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

👍

}

function main() {
Expand Down
73 changes: 32 additions & 41 deletions 3-UsingAPIs/Week1/assignment/ex3-rollDie.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,47 @@ Full description at: https://github.com/HackYourFuture/Assignments/tree/main/3-U
- Change the calls to `callback()` to calls to `resolve()` and `reject()`.
- Refactor the code that call `rollDie()` to use the promise it returns.
- Does the problem described above still occur? If not, what would be your
explanation? Add your answer as a comment to be bottom of the file.
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) => {
// Compute a random number of rolls (3-10) that the die MUST complete
const randomRollsToDo = Math.floor(Math.random() * 8) + 3; 6
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) {
return reject(new Error('Oops... Die rolled off the table.'))
}

if (roll === randomRollsToDo) {
return resolve(value)
}

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 {
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.
// In a promise, if there is a reject, it cannot be resolved, and vice versa. If there is a resolve, there cannot be a reject.
// There is no such limitation with callbacks.

Choose a reason for hiding this comment

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

It looks like you got the idea. But to be more precise, a promise settles on the first call on either resolve() or reject(). Subsequent calls to resolve() or reject() do not change the already settled outcome of the promise.

In this example, in the case that a die rolls off the table the reject() function is called. Then when that same die completes its scheduled rolls the resolve() function is called, but that doesn't change the promise from rejected to resolved. As you rightly point out, this is different from the callback version.

11 changes: 7 additions & 4 deletions 3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ 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 diceArray = dice.map((dice) => {
return rollDie(dice);
});
return Promise.all(diceArray);;
};

Choose a reason for hiding this comment

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

Correct!

Note that you can also use the shorter notation for an arrow function if the function body consist of one statement only, like this:

export function rollDice() {
  const dice = [1, 2, 3, 4, 5];
  const diceArray = dice.map((die) => rollDie(die));
  return Promise.all(diceArray);
}

(BTW: the singular form for "dice" is "die". Since the callback function of map() takes one element of the array at a time you should use the singular form for the argument name as I did in the example above).

You can shorter the code further by passing the rollDie function itself as the map() parameter:

export function rollDice() {
  const dice = [1, 2, 3, 4, 5];
  const diceArray = dice.map(rollDie);
  return Promise.all(diceArray);
}

Finally, you might as well get rid of the intermediate array like this:

export function rollDice() {
  const dice = [1, 2, 3, 4, 5];
  return Promise.all(dice.map(rollDie));
}


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

// TODO Replace this comment by your explanation that was asked for in the assignment description.
// Because Promise.all waits until all the "promises" are fulfilled and only then will it produce a result.
// Here's a lesson: don't make promises that you can't keep.
21 changes: 17 additions & 4 deletions 3-UsingAPIs/Week1/assignment/ex5-pokerDiceChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,31 @@ import { rollDie } from '../../helpers/pokerDiceRoller.js';

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

// TODO: expand the chain to include five dice
return rollDie(1)
let index = 1;

Choose a reason for hiding this comment

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

This variable seems to be unused.

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

Choose a reason for hiding this comment

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

👍

}


function main() {
rollDice()
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 (1 ms)
✅ should have all TODO comments removed
✅ `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 (1 ms)
✅ `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 (1 ms)

Test Suites: 1 passed, 1 total
Tests: 9 passed, 9 total
Snapshots: 0 total
Time: 0.598 s, estimated 1 s
Ran all test suites matching /C:\\Users\\Shnek\\hackyourfuture\\Assignments-cohort52\\.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
✅ `checkDoubleDigits` should not contain unneeded console.log calls (1 ms)
✅ 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
✅ (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.601 s, estimated 1 s
Ran all test suites matching /C:\\Users\\Shnek\\hackyourfuture\\Assignments-cohort52\\.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
✅ should call `new Promise()`
✅ `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 (1 ms)
✅ should reject with an Error when the die rolls off the table

Test Suites: 1 passed, 1 total
Tests: 7 passed, 7 total
Snapshots: 0 total
Time: 0.812 s, estimated 1 s
Ran all test suites matching /C:\\Users\\Shnek\\hackyourfuture\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex3-rollDie.test.js/i.
No linting errors detected.
No spelling errors detected.
Loading