-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Changes from all commits
95635f5
e8c823b
2072884
36096f6
6f07ca0
65de351
1cdf6c3
6caa398
88eab6b
c6e2554
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 |
---|---|---|
@@ -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 | - | ✓ | |
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. |
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) |
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) |
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. |
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}`)) | ||
} | ||
}) | ||
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. 👍 |
||
} | ||
|
||
function main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
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. It looks like you got the idea. But to be more precise, a promise settles on the first call on either In this example, in the case that a die rolls off the table the |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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);; | ||
}; | ||
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. 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 You can shorter the code further by passing the 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() | ||
|
@@ -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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
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. 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; | ||
}); | ||
} | ||
|
||
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. 👍 |
||
} | ||
|
||
|
||
function main() { | ||
rollDice() | ||
|
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. |
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. |
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. |
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.
Excellent!
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.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.