diff --git a/3-UsingAPIs/Week1/homework/ex1-johnWho.js b/3-UsingAPIs/Week1/homework/ex1-johnWho.js index 2c3e82bc..e2e16f58 100644 --- a/3-UsingAPIs/Week1/homework/ex1-johnWho.js +++ b/3-UsingAPIs/Week1/homework/ex1-johnWho.js @@ -6,21 +6,28 @@ 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 -const getAnonName = (firstName, callback) => { - setTimeout(() => { - if (!firstName) { - callback(new Error("You didn't pass in a first name!")); - return; - } - const fullName = `${firstName} Doe`; +const getAnonName = (firstName) => { + const anonName = new Promise((resolve, reject) => { + setTimeout(() => { + if (firstName) { + resolve(`${firstName} Doe`); + } else { + reject(new Error("You didn't pass in a first name!")); + } + }, 1000); + }); + return anonName; +}; - callback(fullName); - }, 1000); +const handleSuccess = (resolvedValue) => { + console.log(resolvedValue); +}; +const handleFailure = (rejectValue) => { + console.log(rejectValue); }; -getAnonName('John', console.log); +getAnonName('John').then(handleSuccess).catch(handleFailure); // ! Do not change or remove the code below module.exports = getAnonName; diff --git a/3-UsingAPIs/Week1/homework/ex2-checkDoubleDigits.js b/3-UsingAPIs/Week1/homework/ex2-checkDoubleDigits.js index a5274aa1..29d11a4e 100644 --- a/3-UsingAPIs/Week1/homework/ex2-checkDoubleDigits.js +++ b/3-UsingAPIs/Week1/homework/ex2-checkDoubleDigits.js @@ -10,8 +10,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. ------------------------------------------------------------------------------*/ -function checkDoubleDigits(/* TODO add parameter(s) here */) { - // TODO complete this function +function checkDoubleDigits(number) { + const checkNumber = new Promise((resolve, reject) => { + if (number >= 10 && number <= 99) { + resolve('This is double digit number!'); + } else { + reject(new Error(`Expected a double digit number but got ${number}`)); + } + }); + return checkNumber; } checkDoubleDigits(11) // should resolve diff --git a/3-UsingAPIs/Week1/homework/ex3-rollDice.js b/3-UsingAPIs/Week1/homework/ex3-rollDice.js index 0c5f0f6b..6b4ba90c 100644 --- a/3-UsingAPIs/Week1/homework/ex3-rollDice.js +++ b/3-UsingAPIs/Week1/homework/ex3-rollDice.js @@ -9,47 +9,38 @@ explanation? Add your answer as a comment to be bottom of the file. ------------------------------------------------------------------------------*/ -// TODO Remove callback and return a promise -function rollDice(callback) { - // Compute a random number of rolls (3-10) that the dice MUST complete - const randomRollsToDo = Math.floor(Math.random() * 8) + 3; - console.log(`Dice scheduled for ${randomRollsToDo} rolls...`); - - const rollOnce = (roll) => { - // Compute a random dice value for the current roll - const value = Math.floor(Math.random() * 6) + 1; - console.log(`Dice value is now: ${value}`); - - // Use callback to notify that the dice rolled off the table after 6 rolls - if (roll > 6) { - // TODO replace "error" callback - callback(new Error('Oops... Dice rolled off the table.')); - } - - // Use callback to communicate the final dice 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); +function rollDice() { + const rollPromise = new Promise((resolve, reject) => { + // Compute a random number of rolls (3-10) that the dice MUST complete + const randomRollsToDo = Math.floor(Math.random() * 8) + 3; + + console.log(`Dice scheduled for ${randomRollsToDo} rolls...`); + const rollOnce = (roll) => { + // Compute a random dice value for the current roll + const value = Math.floor(Math.random() * 6) + 1; + console.log(`Dice value is now: ${value}`); + + if (roll > 6) { + reject(new Error('Oops... Dice rolled off the table.')); + } + if (roll === randomRollsToDo) { + resolve(`Success! Dice settled on ${value}.`); + } + if (roll < randomRollsToDo) { + setTimeout(() => rollOnce(roll + 1), 500); + } + }; + // Start the initial roll + rollOnce(1); + }); + return rollPromise; } -// TODO Refactor to use promise -rollDice((error, value) => { - if (error !== null) { - console.log(error.message); - } else { - console.log(`Success! Dice settled on ${value}.`); - } -}); +rollDice() + .then((resolveValue) => console.log(resolveValue)) + .catch((rejectValue) => console.log(rejectValue.message)); +// It looks like the promise gives the value (reject or resolve) immediately after +// the first case (rejection or resolving) happen and doesn't change this value anymore. // ! Do not change or remove the code below module.exports = rollDice; diff --git a/3-UsingAPIs/Week1/homework/ex4-pokerDiceAll.js b/3-UsingAPIs/Week1/homework/ex4-pokerDiceAll.js index 2602f22e..d26dd706 100644 --- a/3-UsingAPIs/Week1/homework/ex4-pokerDiceAll.js +++ b/3-UsingAPIs/Week1/homework/ex4-pokerDiceAll.js @@ -18,14 +18,19 @@ Can you explain why? Please add your answer as a comment to the end of the exerc const rollDice = require('../../helpers/pokerDiceRoller'); function rollTheDices() { - // TODO Refactor this function const dices = [1, 2, 3, 4, 5]; - return rollDice(1); + const resultPromises = dices.map((dice) => rollDice(dice)); + return Promise.all(resultPromises); } rollTheDices() .then((results) => console.log('Resolved!', results)) .catch((error) => console.log('Rejected!', error.message)); +//1. Dices that have not yet finished their roll continue to do so because reject() doesn't mean 'return'. +//2. There only a single call to either .then() or .catch() despite resolve() and/or reject() being called more than once +//because .then() or .catch() can be called only ones. But I'm not sure :) +//3. We get "Rejected! Dice ... rolled off the table." only once even if several dices rolled off the table +//because if one of the promises is rejected the promise returned by Promise.all() is rejected. // ! Do not change or remove the code below module.export = rollTheDices; diff --git a/3-UsingAPIs/Week1/homework/ex5-rollDiceChain.js b/3-UsingAPIs/Week1/homework/ex5-rollDiceChain.js new file mode 100644 index 00000000..456d9a9d --- /dev/null +++ b/3-UsingAPIs/Week1/homework/ex5-rollDiceChain.js @@ -0,0 +1,25 @@ +// Challenge: throw five dices in sequence +const rollDice = require('../../helpers/pokerDiceRoller'); +function rollTheDices() { + const results = []; + + const pushAndRoll = (dice) => (value) => { + results.push(value); + return rollDice(dice); + }; + + rollDice(1) + .then(pushAndRoll(2)) + .then(pushAndRoll(3)) + .then(pushAndRoll(4)) + .then(pushAndRoll(5)) + .then((value) => { + results.push(value); + console.log('Resolved!', results); + }) + .catch((error) => console.log('Rejected!', error.message)); +} + +rollTheDices(); +// ! Do not change or remove the code below +module.exports = rollTheDices; diff --git a/3-UsingAPIs/Week1/test-reports/ex1-johnWho.pass.txt b/3-UsingAPIs/Week1/test-reports/ex1-johnWho.pass.txt new file mode 100644 index 00000000..4952cede --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex1-johnWho.pass.txt @@ -0,0 +1 @@ +All tests passed \ No newline at end of file diff --git a/3-UsingAPIs/Week1/test-reports/ex1-johnWho.todo.txt b/3-UsingAPIs/Week1/test-reports/ex1-johnWho.todo.txt deleted file mode 100644 index d8d86897..00000000 --- a/3-UsingAPIs/Week1/test-reports/ex1-johnWho.todo.txt +++ /dev/null @@ -1 +0,0 @@ -This test has not been run. \ No newline at end of file diff --git a/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.pass.txt b/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.pass.txt new file mode 100644 index 00000000..4952cede --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.pass.txt @@ -0,0 +1 @@ +All tests passed \ No newline at end of file diff --git a/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.todo.txt b/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.todo.txt deleted file mode 100644 index d8d86897..00000000 --- a/3-UsingAPIs/Week1/test-reports/ex2-checkDoubleDigits.todo.txt +++ /dev/null @@ -1 +0,0 @@ -This test has not been run. \ No newline at end of file diff --git a/3-UsingAPIs/Week1/test-reports/ex3-rollDice.pass.txt b/3-UsingAPIs/Week1/test-reports/ex3-rollDice.pass.txt new file mode 100644 index 00000000..4952cede --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex3-rollDice.pass.txt @@ -0,0 +1 @@ +All tests passed \ No newline at end of file diff --git a/3-UsingAPIs/Week1/test-reports/ex3-rollDice.todo.txt b/3-UsingAPIs/Week1/test-reports/ex3-rollDice.todo.txt deleted file mode 100644 index d8d86897..00000000 --- a/3-UsingAPIs/Week1/test-reports/ex3-rollDice.todo.txt +++ /dev/null @@ -1 +0,0 @@ -This test has not been run. \ No newline at end of file diff --git a/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.pass.txt b/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.pass.txt new file mode 100644 index 00000000..4952cede --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.pass.txt @@ -0,0 +1 @@ +All tests passed \ No newline at end of file diff --git a/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.todo.txt b/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.todo.txt deleted file mode 100644 index d8d86897..00000000 --- a/3-UsingAPIs/Week1/test-reports/ex4-pokerDiceAll.todo.txt +++ /dev/null @@ -1 +0,0 @@ -This test has not been run. \ No newline at end of file diff --git a/3-UsingAPIs/Week1/test-reports/ex5-rollDiceChain.pass.txt b/3-UsingAPIs/Week1/test-reports/ex5-rollDiceChain.pass.txt new file mode 100644 index 00000000..4952cede --- /dev/null +++ b/3-UsingAPIs/Week1/test-reports/ex5-rollDiceChain.pass.txt @@ -0,0 +1 @@ +All tests passed \ No newline at end of file diff --git a/sysinfo.json b/sysinfo.json new file mode 100644 index 00000000..a30ce8fe --- /dev/null +++ b/sysinfo.json @@ -0,0 +1,71 @@ +{ + "node": "v14.15.4", + "system": { + "manufacturer": "Acer", + "model": "Aspire ES1-331" + }, + "cpu": { + "manufacturer": "Intel®", + "brand": "Pentium® N3700", + "speed": 1.6 + }, + "memory": "8 GB", + "osInfo": { + "platform": "win32", + "distro": "�������� Windows 10 ������� ��� ������ �몠", + "release": "10.0.19042" + }, + "blockDevices": [ + { + "name": "C:", + "size": "499 GB", + "fsType": "ntfs", + "physical": "Local" + } + ], + "vscodeInfo": { + "version": "1.54.3", + "extensions": [ + "aaron-bond.better-comments", + "bierner.github-markdown-preview", + "bierner.markdown-checkbox", + "bierner.markdown-emoji", + "bierner.markdown-preview-github-styles", + "bierner.markdown-yaml-preamble", + "CoenraadS.bracket-pair-colorizer", + "CoenraadS.bracket-pair-colorizer-2", + "DavidAnson.vscode-markdownlint", + "dbaeumer.vscode-eslint", + "eamodio.gitlens", + "esbenp.prettier-vscode", + "evgeniypeshkov.syntax-highlighter", + "formulahendry.code-runner", + "hdg.live-html-previewer", + "ritwickdey.LiveServer", + "streetsidesoftware.code-spell-checker", + "techer.open-in-browser", + "vsls-contrib.codetour" + ], + "userSettings": { + "editor.detectIndentation": false, + "editor.formatOnSave": true, + "editor.minimap.enabled": false, + "editor.renderIndentGuides": true, + "editor.tabSize": 2, + "editor.codeActionsOnSave": { + "source.fixAll": true + }, + "eslint.autoFixOnSave": true, + "files.autoSave": "onFocusChange", + "prettier.singleQuote": true, + "prettier.trailingComma": "all", + "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe", + "workbench.colorTheme": "Visual Studio Light", + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "liveServer.settings.donotShowInfoMsg": true, + "window.zoomLevel": 1 + } + } +} \ No newline at end of file diff --git a/veroniqe@rambler.ru.log b/veroniqe@rambler.ru.log new file mode 100644 index 00000000..0dd193d7 --- /dev/null +++ b/veroniqe@rambler.ru.log @@ -0,0 +1,66 @@ +2021-03-19 01:33:45 info: --------------------------------------- +2021-03-19 01:33:45 info: >>> Running Unit Test `ex1-johnWho` <<< +2021-03-19 01:33:45 info: --------------------------------------- +2021-03-19 01:34:42 error: *** Unit Test Error Report *** + +- getAnonName should return a resolved promise when called with a string argument + Expected: John Doe + Received: John + +2021-03-19 01:36:22 info: --------------------------------------- +2021-03-19 01:36:22 info: >>> Running Unit Test `ex1-johnWho` <<< +2021-03-19 01:36:22 info: --------------------------------------- +2021-03-19 01:36:40 info: All unit tests passed. +2021-03-19 01:36:52 info: All steps were completed successfully +2021-03-19 02:47:53 info: ------------------------------------------------- +2021-03-19 02:47:53 info: >>> Running Unit Test `ex2-checkDoubleDigits` <<< +2021-03-19 02:47:53 info: ------------------------------------------------- +2021-03-19 02:48:58 info: All unit tests passed. +2021-03-19 02:49:18 info: All steps were completed successfully +2021-03-19 06:36:05 info: ---------------------------------------- +2021-03-19 06:36:05 info: >>> Running Unit Test `ex3-rollDice` <<< +2021-03-19 06:36:05 info: ---------------------------------------- +2021-03-19 06:36:23 info: All unit tests passed. +2021-03-19 06:36:38 info: All steps were completed successfully +2021-03-19 07:37:15 info: ---------------------------------------- +2021-03-19 07:37:15 info: >>> Running Unit Test `ex3-rollDice` <<< +2021-03-19 07:37:15 info: ---------------------------------------- +2021-03-19 07:37:31 info: All unit tests passed. +2021-03-19 07:37:44 info: All steps were completed successfully +2021-03-20 02:01:16 info: ---------------------------------------- +2021-03-20 02:01:16 info: >>> Running Unit Test `ex3-rollDice` <<< +2021-03-20 02:01:16 info: ---------------------------------------- +2021-03-20 02:01:51 info: All unit tests passed. +2021-03-20 02:01:57 error: *** ESLint Report *** + +ex3-rollDice.js + 15:5 warning Remove commented-out code hyf/no-commented-out-code + +✖ 1 problem (0 errors, 1 warning) + + +2021-03-20 02:03:47 info: ---------------------------------------- +2021-03-20 02:03:47 info: >>> Running Unit Test `ex3-rollDice` <<< +2021-03-20 02:03:47 info: ---------------------------------------- +2021-03-20 02:04:03 info: All unit tests passed. +2021-03-20 02:04:17 info: All steps were completed successfully +2021-03-20 02:06:17 info: ---------------------------------------- +2021-03-20 02:06:17 info: >>> Running Unit Test `ex3-rollDice` <<< +2021-03-20 02:06:17 info: ---------------------------------------- +2021-03-20 02:06:33 info: All unit tests passed. +2021-03-20 02:06:54 info: All steps were completed successfully +2021-03-20 03:55:45 info: -------------------------------------------- +2021-03-20 03:55:45 info: >>> Running Unit Test `ex4-pokerDiceAll` <<< +2021-03-20 03:55:45 info: -------------------------------------------- +2021-03-20 03:56:02 info: All unit tests passed. +2021-03-20 03:56:15 info: All steps were completed successfully +2021-03-22 02:12:27 info: --------------------------------------------- +2021-03-22 02:12:27 info: >>> Running Unit Test `ex5-rollDiceChain` <<< +2021-03-22 02:12:27 info: --------------------------------------------- +2021-03-22 02:12:27 warn: A unit test file was not provided. +2021-03-22 02:12:46 info: All steps were completed successfully +2021-03-25 04:14:43 info: -------------------------------------------- +2021-03-25 04:14:43 info: >>> Running Unit Test `ex4-pokerDiceAll` <<< +2021-03-25 04:14:43 info: -------------------------------------------- +2021-03-25 04:15:38 info: All unit tests passed. +2021-03-25 04:15:56 info: All steps were completed successfully