-
Notifications
You must be signed in to change notification settings - Fork 7
Alaa Nasher - Week 1 (Add the answer code to the assignment) #6
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
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,15 @@ | ||
| ## 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). | ||
|
|
||
| ### 1-JavaScript - Week2 | ||
|
|
||
| | Exercise | Passed | Failed | ESLint | | ||
| |----------------------|--------|--------|--------| | ||
| | ex1-giveCompliment | 7 | - | ✓ | | ||
| | ex2-dogYears | 7 | - | ✓ | | ||
| | ex3-tellFortune | 10 | - | ✓ | | ||
| | ex4-shoppingCart | - | - | ✓ | | ||
| | ex5-shoppingCartPure | - | - | ✓ | | ||
| | ex6-totalCost | - | - | ✓ | | ||
| | ex7-mindPrivacy | - | - | ✓ | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,22 @@ | ||
| /* ----------------------------------------------------------------------------- | ||
| Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-1-you-are-amazing | ||
|
|
||
| 1. Complete the function named `giveCompliment`as follows: | ||
|
|
||
| - It should take a single parameter: `name`. | ||
| - Its function body should include a variable that holds an array, | ||
| `compliments`, initialized with 10 strings. Each string should be a | ||
| compliment, like `"great"`, `"awesome"` and so on. | ||
| - It should randomly select a compliment from the array. | ||
| - It should return the string "You are `compliment`, `name`!", where | ||
| `compliment` is a randomly selected compliment and `name` is the name that | ||
| was passed as an argument to the function. | ||
|
|
||
| 2. Call the function three times, giving each function call the same argument: | ||
| your name. | ||
| Use `console.log` each time to display the return value of the | ||
| `giveCompliment` function to the console. | ||
| -----------------------------------------------------------------------------*/ | ||
| export function giveCompliment(/* TODO parameter(s) go here */) { | ||
| // TODO complete this function | ||
| export function giveCompliment(name) { | ||
| const compliments = [ | ||
| 'kind', | ||
| 'smart', | ||
| 'beautiful', | ||
| 'strong', | ||
| 'brave', | ||
| 'talented', | ||
| 'generous', | ||
| 'helpful', | ||
| 'creative', | ||
| 'friendly', | ||
| ]; | ||
|
|
||
| return `You are ${compliments[Math.floor(Math.random() * compliments.length)]}, ${name}!`; | ||
| } | ||
|
|
||
| function main() { | ||
| // TODO substitute your own name for "HackYourFuture" | ||
| const myName = 'HackYourFuture'; | ||
| const myName = 'Alaa'; | ||
|
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. 👍 |
||
|
|
||
| console.log(giveCompliment(myName)); | ||
| console.log(giveCompliment(myName)); | ||
|
|
@@ -36,7 +29,6 @@ function main() { | |
| console.log(giveCompliment(yourName)); | ||
| } | ||
|
|
||
| // ! 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 |
|---|---|---|
| @@ -1,31 +1,13 @@ | ||
| /*------------------------------------------------------------------------------ | ||
| Full description at: https://github.com/HackYourFuture/Assignment/tree/main/1-JavaScript/Week3#exercise-2-dog-years | ||
| You know how old your dog is in human years, but what about dog years? Let's | ||
| calculate it! | ||
| 1. Complete the function named `calculateDogAge`. | ||
| - It takes one parameter: your (fictional) puppy's age (number). | ||
| - Calculate your dog's age based on the conversion rate of 1 human year to | ||
| 7 dog years. | ||
| - Return a string: "Your doggie is `age` years old in dog years!" | ||
| 2. Use `console.log` to display the result of the function for three different | ||
| ages. | ||
| -----------------------------------------------------------------------------*/ | ||
|
|
||
| export function calculateDogAge(/* TODO parameter(s) go here */) { | ||
| // TODO complete this function | ||
| export function calculateDogAge(age) { | ||
| return `Your doggie is ${age * 7} years old in dog years!`; | ||
|
Comment on lines
+1
to
+2
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() { | ||
| console.log(calculateDogAge(1)); // -> "Your doggie is 7 years old in dog years!" | ||
| console.log(calculateDogAge(2)); // -> "Your doggie is 14 years old in dog years!" | ||
| console.log(calculateDogAge(3)); // -> "Your doggie is 21 years old in dog years!" | ||
| console.log(calculateDogAge(1)); | ||
| console.log(calculateDogAge(2)); | ||
| console.log(calculateDogAge(3)); | ||
| } | ||
|
|
||
| // ! 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 |
|---|---|---|
| @@ -1,68 +1,30 @@ | ||
| /*------------------------------------------------------------------------------ | ||
| Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-3-be-your-own-fortune-teller | ||
| Why pay a fortune teller when you can just program your fortune yourself? | ||
| 1. Create four arrays, `numKids`, `partnerNames`, `locations` and `jobTitles`. | ||
| Give each array five random values that have to do with the name of | ||
| the variable. | ||
| 2. Complete the function `selectRandomly`. This function should take an array | ||
| as a parameter and return a randomly selected element as its return value. | ||
| 3. Complete the function named `tellFortune` as follows: | ||
| - It should take four arguments (in the order listed): | ||
| * the array with the options for the number of children, | ||
| * the array with the options for the partner's name, | ||
| * the array with the options for the geographic location and | ||
| * the array with the options for the job title. | ||
| - It should use the `selectRandomly` function to randomly select values from | ||
| the arrays. | ||
| - It should return a string: "You will be a `jobTitle` in `location`, | ||
| married to `partnerName` with `numKids` kids." | ||
| 4. Call the function three times, passing the arrays as arguments. Use ` | ||
| console.log` to display the results. | ||
| Note: The DRY principle is put into practice here: instead of repeating the code to | ||
| randomly select array elements four times inside the `tellFortune` function | ||
| body, this code is now written once only in a separated function. | ||
| -----------------------------------------------------------------------------*/ | ||
|
|
||
| // This function should take an array as its parameter and return | ||
| // a randomly selected element as its return value. | ||
| function selectRandomly(/* TODO parameter(s) go here */) { | ||
| // TODO complete this function | ||
| function selectRandomly(choices) { | ||
| return choices[Math.floor(Math.random() * choices.length)]; | ||
|
Comment on lines
+1
to
+2
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. 👍 |
||
| } | ||
|
|
||
| export function tellFortune(/* TODO add parameter(s) here */) { | ||
| // TODO complete this function | ||
| export function tellFortune(childrenArr, partnersArr, locationsArr, jobsArr) { | ||
| const numKids = selectRandomly(childrenArr); | ||
| const partnerName = selectRandomly(partnersArr); | ||
| const location = selectRandomly(locationsArr); | ||
| const jobTitle = selectRandomly(jobsArr); | ||
|
|
||
| return `You will be a ${jobTitle} in ${location}, married to ${partnerName} with ${numKids} kids.`; | ||
|
Comment on lines
+5
to
+11
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! The function parameters and constants are both descriptive making the function easy to read and understand. |
||
| } | ||
|
|
||
| function main() { | ||
| const numKids = [ | ||
| // TODO add elements here | ||
| ]; | ||
| const numKids = [2, 4, 1, 7, 5]; | ||
|
|
||
| const partnerNames = [ | ||
| // TODO add elements here | ||
| ]; | ||
| const partnerNames = ['Yusuf', 'Leen', 'Sarah', 'Eliana', 'Amal']; | ||
|
|
||
| const locations = [ | ||
| // TODO add elements here | ||
| ]; | ||
| const locations = ['Amsterdam', 'Breda', 'Tilburg', 'Utrecht', 'Rotterdam']; | ||
|
|
||
| const jobTitles = [ | ||
| // TODO add elements here | ||
| ]; | ||
| const jobTitles = ['Programmer', 'Teacher', 'Soldier', 'Manager', 'Musician']; | ||
|
Comment on lines
+15
to
+21
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. 👍 |
||
|
|
||
| console.log(tellFortune(numKids, partnerNames, locations, jobTitles)); | ||
| console.log(tellFortune(numKids, partnerNames, locations, jobTitles)); | ||
| console.log(tellFortune(numKids, partnerNames, locations, jobTitles)); | ||
| } | ||
|
|
||
| // ! 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 |
|---|---|---|
| @@ -1,29 +1,14 @@ | ||
| /*------------------------------------------------------------------------------ | ||
| Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-4-shopping-at-the-supermarket | ||
| Let's do some grocery shopping! We're going to get some things to cook dinner | ||
| with. However, you like to spend money and always buy too many things. So when | ||
| you have more than 3 items in your shopping cart the first item gets taken out. | ||
| 1. Complete the function named `addToShoppingCart` as follows: | ||
| - It should take one argument: a grocery item (string) | ||
| - It should add the grocery item to the `shoppingCart` array. If the number of items is | ||
| more than three remove the first one in the array. | ||
| - It should return a string "You bought <list-of-items>!", where | ||
| <list-of-items>is a comma-separated list of items from the shopping cart | ||
| array. | ||
| 2. Confirm that your code passes the unit tests. | ||
| -----------------------------------------------------------------------------*/ | ||
| const shoppingCart = ['bananas', 'milk']; | ||
|
|
||
| // ! Function to be tested | ||
| function addToShoppingCart(/* parameters go here */) { | ||
| // TODO complete this function | ||
| function addToShoppingCart(item) { | ||
| shoppingCart.push(item); | ||
| if (shoppingCart.length > 3) { | ||
| shoppingCart.shift(); | ||
| } | ||
| const shoppingCartItems = shoppingCart.join(', '); | ||
| return `You bought ${shoppingCartItems}!`; | ||
|
Comment on lines
+3
to
+9
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! |
||
| } | ||
|
|
||
| // ! Test functions (plain vanilla JavaScript) | ||
| function test1() { | ||
| console.log( | ||
| 'Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,19 @@ | ||
| /*------------------------------------------------------------------------------ | ||
| Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-5-improved-shopping-at-the-supermarket | ||
| In the current exercise we will rewrite the `addToShoppingCart` function to make | ||
| it pure. Do the following: | ||
| 1. Complete the parameter list of `addToShopping()`. As a first parameter it | ||
| should accept a shopping cart array and as a second parameter it should | ||
| accept a grocery item to be added. | ||
| 2. The function should return a new shopping cart array, following the same rule | ||
| as in the previous exercise: it should contain a maximum of three items. | ||
| 3. The shopping cart passed as an argument should not be mutated. | ||
| 4. When constructing the new shopping cart array you should make use of the ES6 | ||
| spread syntax. | ||
| 5. Confirm that you function passes the provided unit tests. | ||
| ------------------------------------------------------------------------------*/ | ||
| // ! Function under test | ||
| function addToShoppingCart(/* TODO parameter(s) go here */) { | ||
| // TODO complete this function | ||
| function addToShoppingCart([...shoppingCart], item) { | ||
|
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. Good work using spread syntax. |
||
| shoppingCart.push(item); | ||
| if (shoppingCart.length > 3) { | ||
| shoppingCart.shift(); | ||
| } | ||
|
Comment on lines
+2
to
+5
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! |
||
| const shoppingCartItems = shoppingCart.join(', '); | ||
| return `You bought ${shoppingCartItems}!`; | ||
| } | ||
|
|
||
| // ! Test functions (plain vanilla JavaScript) | ||
| function test1() { | ||
| console.log('Test 1: addToShoppingCart should take two parameters'); | ||
| console.assert(addToShoppingCart.length === 2); | ||
| } | ||
|
|
||
| function test2() { | ||
| console.log('Test 2: addToShoppingCart should be a pure function'); | ||
| // A pure function should return the same result when called with | ||
| // identical arguments. It should also have no side effects (not tested here). | ||
| const initialCart = ['bananas', 'milk']; | ||
| const result1 = addToShoppingCart(initialCart, 'chocolate'); | ||
| const result2 = addToShoppingCart(initialCart, 'chocolate'); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,32 @@ | ||
| /*------------------------------------------------------------------------------ | ||
| Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-6-total-cost-is | ||
|
|
||
| You want to buy a couple of things from the supermarket to prepare for a party. | ||
| After scanning all the items the cashier wants to give you the total price, but | ||
| the machine is broken! Let's write her a function that does it for her | ||
| instead! | ||
|
|
||
| 1. Create an object named `cartForParty` with five properties. Each property | ||
| should be a grocery item (like `beers` or `chips`) and hold a number value | ||
| (like `1.75` or `0.99`). | ||
|
|
||
| 2. Complete the function called `calculateTotalPrice`. | ||
|
|
||
| - It takes one parameter: an object that contains properties that only contain | ||
| number values. | ||
| - Loop through the object and add all the number values together. | ||
| - Return a string: "Total: €`amount`". | ||
|
|
||
| 3. Complete the unit test functions and verify that all is working as expected. | ||
| -----------------------------------------------------------------------------*/ | ||
| const cartForParty = { | ||
| // TODO complete this object | ||
| chips: 1.75, | ||
| juice: 2.4, | ||
| frenchfries: 4.99, | ||
| cake: 8.99, | ||
| choclate: 2.99, | ||
|
Comment on lines
+2
to
+6
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 calculateTotalPrice(/* TODO parameter(s) go here */) { | ||
| // TODO replace this comment with your code | ||
| function calculateTotalPrice(obj) { | ||
| // let sum = 0; | ||
| // for (const [_, value] of Object.entries(obj)) { | ||
| // sum += value; | ||
| // } | ||
|
|
||
| // return Number(sum.toFixed(2)); | ||
| const total = Object.values(obj).reduce((total, item) => total + item); | ||
| return Number(total.toFixed(2)); | ||
| } | ||
|
|
||
| // ! Test functions (plain vanilla JavaScript) | ||
| function test1() { | ||
| console.log('\nTest 1: calculateTotalPrice should take one parameter'); | ||
| // TODO replace this comment with your code | ||
| console.assert(calculateTotalPrice.length === 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. 👍 |
||
| } | ||
|
|
||
| function test2() { | ||
| console.log('\nTest 2: return correct output when passed cartForParty'); | ||
| // TODO replace this comment with your code | ||
| const expected = 21.12; | ||
| const actual = calculateTotalPrice(cartForParty); | ||
| console.assert(actual === expected); | ||
|
Comment on lines
+27
to
+29
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 test() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,3 @@ | ||
| /*------------------------------------------------------------------------------ | ||
| Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-7-mind-the-privacy | ||
|
|
||
| 1. Complete the `filterPrivateData()` function. It should take a single | ||
| parameter: the array of employee records. | ||
| 2. It should create a _new_ array, containing employee data without the private | ||
| data. | ||
| 3. Use object destructuring to extract the non-private properties from an | ||
| employee record (an `object`) and object literal shorthand to create a new | ||
| employee record with just the non-private parts (name, occupation and email). | ||
| 4. Return the new array as the return value of the function. | ||
| 5. Run the exercise and verify that it passes all the unit tests. | ||
| ------------------------------------------------------------------------------*/ | ||
| const employeeRecords = [ | ||
| { | ||
| name: 'John', | ||
|
|
@@ -28,12 +15,17 @@ const employeeRecords = [ | |
| }, | ||
| ]; | ||
|
|
||
| // ! Function under test | ||
| function filterPrivateData(/* TODO parameter(s) go here */) { | ||
| // TODO complete this function | ||
| function filterPrivateData(employeesRecord) { | ||
| // const nonPrivateData = []; | ||
| // for (let { name, occupation, email } of employeesRecord) { | ||
| // nonPrivateData.push({ name, occupation, email }); | ||
| // } | ||
| const nonPrivateData = employeesRecord.map(({ name, occupation, email }) => { | ||
| return { name, occupation, email }; | ||
| }); | ||
| return nonPrivateData; | ||
|
Comment on lines
+18
to
+26
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! |
||
| } | ||
|
|
||
| // ! Test functions (plain vanilla JavaScript) | ||
| function test1() { | ||
| console.log('Test 1: filterPrivateData should take one parameter'); | ||
| console.assert(filterPrivateData.length === 1); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| *** Unit Test Error Report *** | ||
|
|
||
| PASS .dist/1-JavaScript/Week2/unit-tests/ex1-giveCompliment.test.js | ||
| js-wk2-ex1-giveCompliment | ||
| ✅ should exist and be executable (2 ms) | ||
| ✅ should have all TODO comments removed (1 ms) | ||
| ✅ `giveCompliment` should not contain unneeded console.log calls (1 ms) | ||
| ✅ should take a single parameter | ||
| ✅ should include a `compliments` array inside its function body | ||
| ✅ the `compliments` array should be initialized with 10 strings | ||
| ✅ should give a random compliment: You are `compliment`, `name`! | ||
|
|
||
| Test Suites: 1 passed, 1 total | ||
| Tests: 7 passed, 7 total | ||
| Snapshots: 0 total | ||
| Time: 0.522 s, estimated 1 s | ||
| Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-Cohort54\/.dist\/1-JavaScript\/Week2\/unit-tests\/ex1-giveCompliment.test.js/i. | ||
| No linting errors detected. | ||
|
|
||
|
|
||
| *** Spell Checker Report *** | ||
|
|
||
| 1-JavaScript/Week2/assignment/ex1-giveCompliment.js:19:19 - Unknown word (Alaa) |
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.
Great job using the string template literal method.