-
Notifications
You must be signed in to change notification settings - Fork 17
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
Implemented assignments #54
base: main
Are you sure you want to change the base?
Changes from 2 commits
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 |
---|---|---|
|
@@ -7,25 +7,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 | ||
export const getAnonName = (firstName, callback) => { | ||
setTimeout(() => { | ||
if (!firstName) { | ||
callback(new Error("You didn't pass in a first name!")); | ||
return; | ||
} | ||
|
||
const fullName = `${firstName} Doe`; | ||
export const getAnonName = (firstName) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
if (!firstName) { | ||
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. Nice solution. Great approach handling the rejection (fail-first) first. |
||
reject(new Error("You didn't pass in a first name!")); | ||
return; | ||
} | ||
|
||
callback(fullName); | ||
}, 1000); | ||
const fullName = `${firstName} Doe`; | ||
resolve(fullName); | ||
}, 1000); | ||
}); | ||
}; | ||
|
||
function main() { | ||
getAnonName('John', console.log); | ||
getAnonName('John') | ||
.then((fullName) => console.log(`Your anonymous name is: ${fullName}`)) | ||
.catch((error) => console.error(error.message)); | ||
} | ||
|
||
// ! Do not change or remove the code below | ||
if (process.env.NODE_ENV !== 'test') { | ||
main(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,23 @@ 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 (is2Digit(number)) { | ||
resolve("This is a double digit number!"); | ||
} else { | ||
reject(new Error(`Expected a double digit number but got ${number}`)); | ||
} | ||
}); | ||
} | ||
const is2Digit = (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. Well done with your solution. It works but we can improve it bit. Firstly, nice that we are using a function Note: Two digit number is a number that is equal to or greater than 10 and also a number that is equal to or less than 99 (e.g 10, 11, 12, ..., 97, 98, 99). Therefore, we can use an Alternaive approach:
In this solution, we are not checking edge cases such as when the number is a negative number. |
||
let count = 0; | ||
while (number) { | ||
number = Math.floor((number /= 10)); | ||
count++; | ||
} | ||
return count === 2; | ||
}; | ||
|
||
function main() { | ||
checkDoubleDigits(9) // should reject | ||
|
@@ -36,4 +50,4 @@ function main() { | |
// ! Do not change or remove the code below | ||
if (process.env.NODE_ENV !== 'test') { | ||
main(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,13 @@ Can you explain why? Please add your answer as a comment to the end of the | |
exercise file. | ||
------------------------------------------------------------------------------*/ | ||
|
||
// The line below makes the rollDie() function available to this file. | ||
// Do not change or remove it. | ||
|
||
import { rollDie } from '../../helpers/pokerDiceRoller.js'; | ||
|
||
export function rollDice() { | ||
// TODO Refactor this function | ||
const dice = [1, 2, 3, 4, 5]; | ||
return rollDie(1); | ||
const diceRolls = dice.map(() => rollDie()); | ||
return Promise.all(diceRolls); | ||
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. Great job. |
||
} | ||
|
||
function main() { | ||
|
@@ -43,4 +42,8 @@ if (process.env.NODE_ENV !== 'test') { | |
main(); | ||
} | ||
|
||
// TODO Replace this comment by your explanation that was asked for in the assignment description. | ||
|
||
// The dice that have not yet finished their roll continue to do so, | ||
// because of asynchronous nature of Promise.all(). | ||
// The Promise.all() method runs all the promises concurrently, | ||
// but it resolves or rejects only when all the promises succeed or any single promise fails. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,24 @@ 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); | ||
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. Great job, this works as expected. But if you noticed, we are repeating these two codes from line 22 to 39:
We can prevent this with a Software engineering principle called "DRY Principle" by putting both as a function and use the function when needed. "Don't Repeat Yourself" (DRY) is a software development principle that helps us to avoid duplicating code. If you aren't aware of it, you can Google Search. |
||
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; | ||
}); | ||
|
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 (1 ms) | ||
✅ `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: 1.057 s | ||
Ran all test suites matching /C:\\Users\\Samira\\Desktop\\API-assignments\\Assignments-cohort51\\.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 | ||
✅ 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 | ||
✅ (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.568 s | ||
Ran all test suites matching /C:\\Users\\Samira\\Desktop\\API-assignments\\Assignments-cohort51\\.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 (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.57 s | ||
Ran all test suites matching /C:\\Users\\Samira\\Desktop\\API-assignments\\Assignments-cohort51\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex3-rollDie.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/ex4-pokerDiceAll.test.js | ||
api-wk1-ex4-pokerDiceAll | ||
✅ should exist and be executable (1 ms) | ||
✅ should have all TODO comments removed | ||
✅ `rollDice` should not contain unneeded console.log calls | ||
✅ should use `dice.map()` | ||
✅ should use `Promise.all()` | ||
✅ should resolve when all dice settle successfully (14 ms) | ||
✅ should reject with an Error when a die rolls off the table (97 ms) | ||
|
||
Test Suites: 1 passed, 1 total | ||
Tests: 7 passed, 7 total | ||
Snapshots: 0 total | ||
Time: 0.966 s | ||
Ran all test suites matching /C:\\Users\\Samira\\Desktop\\API-assignments\\Assignments-cohort51\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex4-pokerDiceAll.test.js/i. | ||
No linting errors detected. | ||
No spelling errors detected. |
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 | ||
✅ should resolve when all dice settle successfully (18 ms) | ||
✅ should reject with an Error when a die rolls off the table (98 ms) | ||
|
||
Test Suites: 1 passed, 1 total | ||
Tests: 5 passed, 5 total | ||
Snapshots: 0 total | ||
Time: 0.696 s | ||
Ran all test suites matching /C:\\Users\\Samira\\Desktop\\API-assignments\\Assignments-cohort51\\.dist\\3-UsingAPIs\\Week1\\unit-tests\\ex5-pokerDiceChain.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.
@samira313 Great job completing week 1 assignment. Kudos!!! I will Approve it now.