diff --git a/.test-summary/TEST_SUMMARY.md b/.test-summary/TEST_SUMMARY.md new file mode 100644 index 000000000..d3a57c403 --- /dev/null +++ b/.test-summary/TEST_SUMMARY.md @@ -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 | - | ✓ | diff --git a/3-UsingAPIs/Week1/assignment/ex1-johnWho.js b/3-UsingAPIs/Week1/assignment/ex1-johnWho.js index 5851c8c4f..a28adf68e 100644 --- a/3-UsingAPIs/Week1/assignment/ex1-johnWho.js +++ b/3-UsingAPIs/Week1/assignment/ex1-johnWho.js @@ -7,22 +7,20 @@ 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(() => { + +export const getAnonName = firstName => { + return new Promise((resolve, reject) => { if (!firstName) { - callback(new Error("You didn't pass in a first name!")); - return; + reject(new Error("You didn't pass in a first name!")); } const fullName = `${firstName} Doe`; - - callback(fullName); - }, 1000); + resolve(fullName); + }); }; function main() { - getAnonName('John', console.log); + getAnonName('John').then(name => console.log(name)).catch(err => console.log(err.message)); } // ! Do not change or remove the code below diff --git a/3-UsingAPIs/Week1/assignment/ex2-checkDoubleDigits.js b/3-UsingAPIs/Week1/assignment/ex2-checkDoubleDigits.js index 4b31d1927..efc5ac5c3 100644 --- a/3-UsingAPIs/Week1/assignment/ex2-checkDoubleDigits.js +++ b/3-UsingAPIs/Week1/assignment/ex2-checkDoubleDigits.js @@ -11,8 +11,15 @@ 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() { diff --git a/3-UsingAPIs/Week1/assignment/ex3-rollDie.js b/3-UsingAPIs/Week1/assignment/ex3-rollDie.js index 7e4b77888..97d7d50a5 100644 --- a/3-UsingAPIs/Week1/assignment/ex3-rollDie.js +++ b/3-UsingAPIs/Week1/assignment/ex3-rollDie.js @@ -10,48 +10,36 @@ 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) { + 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 @@ -59,4 +47,7 @@ if (process.env.NODE_ENV !== 'test') { main(); } -// TODO Replace this comment by your explanation that was asked for in the assignment description. +/* + The issue does not occur anymore, we don't get a success message when we catch the error because as soon as the Promise is rejected we can't get resolve value + (I didn't get why rollOnce() had no return statements to stop it from continuing computing (I added them just to test)) +*/ diff --git a/3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js b/3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js index d88cd71d2..b91d3da1b 100644 --- a/3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js +++ b/3-UsingAPIs/Week1/assignment/ex4-pokerDiceAll.js @@ -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); + const promises = dice.map(num => { + return new Promise((resolve, reject) => { + rollDie(num).then(resolve).catch(reject); + }) + }) + return Promise.all(promises); } function main() { @@ -43,4 +47,7 @@ if (process.env.NODE_ENV !== 'test') { main(); } -// TODO Replace this comment by your explanation that was asked for in the assignment description. +/* + I think that the rolls are happening inside the API and multiple Promises are running which causes the outputs to mix up when they go to + in the Event Queue and then end up in the Call Stack +*/ diff --git a/3-UsingAPIs/Week1/assignment/ex5-pokerDiceChain.js b/3-UsingAPIs/Week1/assignment/ex5-pokerDiceChain.js index 5b1394b84..c4bee61e6 100644 --- a/3-UsingAPIs/Week1/assignment/ex5-pokerDiceChain.js +++ b/3-UsingAPIs/Week1/assignment/ex5-pokerDiceChain.js @@ -17,16 +17,27 @@ 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; - }); + }) } function main() { diff --git a/3-UsingAPIs/Week1/test-reports/ex1-johnWho.report.txt b/3-UsingAPIs/Week1/test-reports/ex1-johnWho.report.txt new file mode 100644 index 000000000..bd29956cb --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex1-johnWho.report.txt @@ -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 + ✅ `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.832 s +Ran all test suites matching /C:\\Users\\BidonLoverXXX\\WebstormProjects\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex1-johnWho.test.js/i. +No linting errors detected. +No spelling errors detected. diff --git a/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.report.txt b/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.report.txt new file mode 100644 index 000000000..d8e0fc516 --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.report.txt @@ -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 + ✅ (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!" (1 ms) + ✅ (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.689 s +Ran all test suites matching /C:\\Users\\BidonLoverXXX\\WebstormProjects\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex2-checkDoubleDigits.test.js/i. +No linting errors detected. +No spelling errors detected. diff --git a/3-UsingAPIs/Week1/test-reports/ex3-rollDie.report.txt b/3-UsingAPIs/Week1/test-reports/ex3-rollDie.report.txt new file mode 100644 index 000000000..2dbe9fd03 --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex3-rollDie.report.txt @@ -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 (1 ms) + ✅ should have all TODO comments removed + ✅ should call `new Promise()` (1 ms) + ✅ `resolve()` should be called with a one argument + ✅ `reject()` should be called with a one argument + ✅ should resolve when the die settles successfully (1 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: 0.814 s, estimated 1 s +Ran all test suites matching /C:\\Users\\BidonLoverXXX\\WebstormProjects\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex3-rollDie.test.js/i. +No linting errors detected. +No spelling errors detected. diff --git a/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.report.txt b/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.report.txt new file mode 100644 index 000000000..a497ddae0 --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.report.txt @@ -0,0 +1,22 @@ +*** Unit Test Error Report *** + +(node:72180) TimeoutNegativeWarning: -5 is a negative number. +Timeout duration was set to 1. +(Use `node --trace-warnings ...` to show where the warning was created) + PASS .dist/3-UsingAPIs/Week1/unit-tests/ex4-pokerDiceAll.test.js + api-wk1-ex4-pokerDiceAll + ✅ should exist and be executable (2 ms) + ✅ should have all TODO comments removed (1 ms) + ✅ `rollDice` should not contain unneeded console.log calls + ✅ should use `dice.map()` + ✅ should use `Promise.all()` + ✅ should resolve when all dice settle successfully (9 ms) + ✅ should reject with an Error when a die rolls off the table (86 ms) + +Test Suites: 1 passed, 1 total +Tests: 7 passed, 7 total +Snapshots: 0 total +Time: 1.004 s +Ran all test suites matching /C:\\Users\\BidonLoverXXX\\WebstormProjects\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex4-pokerDiceAll.test.js/i. +No linting errors detected. +No spelling errors detected. diff --git a/3-UsingAPIs/Week1/test-reports/ex5-pokerDiceChain.report.txt b/3-UsingAPIs/Week1/test-reports/ex5-pokerDiceChain.report.txt new file mode 100644 index 000000000..cfcdd5fd4 --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex5-pokerDiceChain.report.txt @@ -0,0 +1,20 @@ +*** Unit Test Error Report *** + +(node:66272) TimeoutNegativeWarning: -5 is a negative number. +Timeout duration was set to 1. +(Use `node --trace-warnings ...` to show where the warning was created) + PASS .dist/3-UsingAPIs/Week1/unit-tests/ex5-pokerDiceChain.test.js + api-wk1-ex5-pokerDiceChain + ✅ should exist and be executable (1 ms) + ✅ should have all TODO comments removed + ✅ `rollDice` should not contain unneeded console.log calls (1 ms) + ✅ should resolve when all dice settle successfully (23 ms) + ✅ should reject with an Error when a die rolls off the table (95 ms) + +Test Suites: 1 passed, 1 total +Tests: 5 passed, 5 total +Snapshots: 0 total +Time: 0.821 s +Ran all test suites matching /C:\\Users\\BidonLoverXXX\\WebstormProjects\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex5-pokerDiceChain.test.js/i. +No linting errors detected. +No spelling errors detected. diff --git a/package-lock.json b/package-lock.json index db6997f5f..fd830347c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -438,26 +438,27 @@ } }, "node_modules/@babel/helpers": { - "version": "7.25.0", - "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.25.0.tgz", - "integrity": "sha512-MjgLZ42aCm0oGjJj8CtSM3DB8NOOf8h2l7DCTePJs29u+v7yO/RBX9nShlKMgFnRks/Q4tBAe7Hxnov9VkGwLw==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.27.0.tgz", + "integrity": "sha512-U5eyP/CTFPuNE3qk+WZMxFkp/4zUzdceQlfzf7DdGdhp+Fezd7HD+i8Y24ZuTMKX3wQBld449jijbGq6OdGNQg==", "dev": true, + "license": "MIT", "dependencies": { - "@babel/template": "^7.25.0", - "@babel/types": "^7.25.0" + "@babel/template": "^7.27.0", + "@babel/types": "^7.27.0" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/parser": { - "version": "7.26.9", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.26.9.tgz", - "integrity": "sha512-81NWa1njQblgZbQHxWHpxxCzNsa3ZwvFqpUg7P+NNUU6f3UU2jBEg4OlF/J6rl8+PQGh1q6/zWScd001YwcA5A==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.27.0.tgz", + "integrity": "sha512-iaepho73/2Pz7w2eMS0Q5f83+0RKI7i4xmiYeBmDzfRVbQtTOG7Ts0S4HzJVsTMGI9keU8rNfuZr8DKfSt7Yyg==", "dev": true, "license": "MIT", "dependencies": { - "@babel/types": "^7.26.9" + "@babel/types": "^7.27.0" }, "bin": { "parser": "bin/babel-parser.js" @@ -1733,9 +1734,9 @@ } }, "node_modules/@babel/runtime": { - "version": "7.26.9", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.26.9.tgz", - "integrity": "sha512-aA63XwOkcl4xxQa3HjPMqOP6LiK0ZDv3mUPYEFXkpHbaFjtGggE1A61FjFzJnB+p7/oy2gA8E+rcBNl/zC1tMg==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.27.0.tgz", + "integrity": "sha512-VtPOkrdPHZsKc/clNqyi9WUA8TINkZ4cGk63UUE3u4pmB2k+ZMQRDuIOagv8UVd6j7k0T3+RRIb7beKTebNbcw==", "dev": true, "license": "MIT", "dependencies": { @@ -1746,15 +1747,15 @@ } }, "node_modules/@babel/template": { - "version": "7.26.9", - "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.26.9.tgz", - "integrity": "sha512-qyRplbeIpNZhmzOysF/wFMuP9sctmh2cFzRAZOn1YapxBsE1i9bJIY586R/WBLfLcmcBlM8ROBiQURnnNy+zfA==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.27.0.tgz", + "integrity": "sha512-2ncevenBqXI6qRMukPlXwHKHchC7RyMuu4xv5JBXRfOGVcTy1mXCD12qrp7Jsoxll1EV3+9sE4GugBVRjT2jFA==", "dev": true, "license": "MIT", "dependencies": { "@babel/code-frame": "^7.26.2", - "@babel/parser": "^7.26.9", - "@babel/types": "^7.26.9" + "@babel/parser": "^7.27.0", + "@babel/types": "^7.27.0" }, "engines": { "node": ">=6.9.0" @@ -1789,9 +1790,9 @@ } }, "node_modules/@babel/types": { - "version": "7.26.9", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.26.9.tgz", - "integrity": "sha512-Y3IR1cRnOxOCDvMmNiym7XpXQ93iGDDPHx+Zj+NM+rg0fBaShfQLkg+hKPaZCEvg5N/LeCo4+Rj/i3FuJsIQaw==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.27.0.tgz", + "integrity": "sha512-H45s8fVLYjbhFH62dIJ3WtmJ6RSPt/3DRO0ZcT2SUiYiQyz3BLVb9ADEnLl91m74aQPS3AzzeajZHYOalWe3bg==", "dev": true, "license": "MIT", "dependencies": { @@ -9787,22 +9788,22 @@ } }, "@babel/helpers": { - "version": "7.25.0", - "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.25.0.tgz", - "integrity": "sha512-MjgLZ42aCm0oGjJj8CtSM3DB8NOOf8h2l7DCTePJs29u+v7yO/RBX9nShlKMgFnRks/Q4tBAe7Hxnov9VkGwLw==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.27.0.tgz", + "integrity": "sha512-U5eyP/CTFPuNE3qk+WZMxFkp/4zUzdceQlfzf7DdGdhp+Fezd7HD+i8Y24ZuTMKX3wQBld449jijbGq6OdGNQg==", "dev": true, "requires": { - "@babel/template": "^7.25.0", - "@babel/types": "^7.25.0" + "@babel/template": "^7.27.0", + "@babel/types": "^7.27.0" } }, "@babel/parser": { - "version": "7.26.9", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.26.9.tgz", - "integrity": "sha512-81NWa1njQblgZbQHxWHpxxCzNsa3ZwvFqpUg7P+NNUU6f3UU2jBEg4OlF/J6rl8+PQGh1q6/zWScd001YwcA5A==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.27.0.tgz", + "integrity": "sha512-iaepho73/2Pz7w2eMS0Q5f83+0RKI7i4xmiYeBmDzfRVbQtTOG7Ts0S4HzJVsTMGI9keU8rNfuZr8DKfSt7Yyg==", "dev": true, "requires": { - "@babel/types": "^7.26.9" + "@babel/types": "^7.27.0" } }, "@babel/plugin-bugfix-firefox-class-in-computed-class-key": { @@ -10599,23 +10600,23 @@ } }, "@babel/runtime": { - "version": "7.26.9", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.26.9.tgz", - "integrity": "sha512-aA63XwOkcl4xxQa3HjPMqOP6LiK0ZDv3mUPYEFXkpHbaFjtGggE1A61FjFzJnB+p7/oy2gA8E+rcBNl/zC1tMg==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.27.0.tgz", + "integrity": "sha512-VtPOkrdPHZsKc/clNqyi9WUA8TINkZ4cGk63UUE3u4pmB2k+ZMQRDuIOagv8UVd6j7k0T3+RRIb7beKTebNbcw==", "dev": true, "requires": { "regenerator-runtime": "^0.14.0" } }, "@babel/template": { - "version": "7.26.9", - "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.26.9.tgz", - "integrity": "sha512-qyRplbeIpNZhmzOysF/wFMuP9sctmh2cFzRAZOn1YapxBsE1i9bJIY586R/WBLfLcmcBlM8ROBiQURnnNy+zfA==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.27.0.tgz", + "integrity": "sha512-2ncevenBqXI6qRMukPlXwHKHchC7RyMuu4xv5JBXRfOGVcTy1mXCD12qrp7Jsoxll1EV3+9sE4GugBVRjT2jFA==", "dev": true, "requires": { "@babel/code-frame": "^7.26.2", - "@babel/parser": "^7.26.9", - "@babel/types": "^7.26.9" + "@babel/parser": "^7.27.0", + "@babel/types": "^7.27.0" } }, "@babel/traverse": { @@ -10642,9 +10643,9 @@ } }, "@babel/types": { - "version": "7.26.9", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.26.9.tgz", - "integrity": "sha512-Y3IR1cRnOxOCDvMmNiym7XpXQ93iGDDPHx+Zj+NM+rg0fBaShfQLkg+hKPaZCEvg5N/LeCo4+Rj/i3FuJsIQaw==", + "version": "7.27.0", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.27.0.tgz", + "integrity": "sha512-H45s8fVLYjbhFH62dIJ3WtmJ6RSPt/3DRO0ZcT2SUiYiQyz3BLVb9ADEnLl91m74aQPS3AzzeajZHYOalWe3bg==", "dev": true, "requires": { "@babel/helper-string-parser": "^7.25.9",