From fe8068ae3696bf9cfddc5188527d14a85c051954 Mon Sep 17 00:00:00 2001 From: Majd Hamde Date: Fri, 22 Aug 2025 13:11:49 +0200 Subject: [PATCH] completed week 2 js exercises 1-7 --- .test-summary/TEST_SUMMARY.md | 15 +++++++ .../Week2/assignment/ex1-giveCompliment.js | 42 ++++++++++--------- 1-JavaScript/Week2/assignment/ex2-dogYears.js | 23 ++++------ .../Week2/assignment/ex3-tellFortune.js | 40 +++++++++--------- .../Week2/assignment/ex4-shoppingCart.js | 15 ++++++- .../Week2/assignment/ex5-shoppingCartPure.js | 13 +++++- .../Week2/assignment/ex6-totalCost.js | 32 +++++++++++--- .../Week2/assignment/ex7-mindPrivacy.js | 10 ++++- .../ex1-giveCompliment.report.txt | 23 ++++++++++ .../test-reports/ex2-dogYears.report.txt | 19 +++++++++ .../test-reports/ex3-tellFortune.report.txt | 22 ++++++++++ .../test-reports/ex4-shoppingCart.report.txt | 3 ++ .../ex5-shoppingCartPure.report.txt | 3 ++ .../test-reports/ex6-totalCost.report.txt | 3 ++ .../test-reports/ex7-mindPrivacy.report.txt | 10 +++++ 15 files changed, 209 insertions(+), 64 deletions(-) create mode 100644 .test-summary/TEST_SUMMARY.md create mode 100644 1-JavaScript/Week2/test-reports/ex1-giveCompliment.report.txt create mode 100644 1-JavaScript/Week2/test-reports/ex2-dogYears.report.txt create mode 100644 1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt create mode 100644 1-JavaScript/Week2/test-reports/ex4-shoppingCart.report.txt create mode 100644 1-JavaScript/Week2/test-reports/ex5-shoppingCartPure.report.txt create mode 100644 1-JavaScript/Week2/test-reports/ex6-totalCost.report.txt create mode 100644 1-JavaScript/Week2/test-reports/ex7-mindPrivacy.report.txt diff --git a/.test-summary/TEST_SUMMARY.md b/.test-summary/TEST_SUMMARY.md new file mode 100644 index 000000000..ad95f550b --- /dev/null +++ b/.test-summary/TEST_SUMMARY.md @@ -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 | - | - | ✓ | diff --git a/1-JavaScript/Week2/assignment/ex1-giveCompliment.js b/1-JavaScript/Week2/assignment/ex1-giveCompliment.js index 93806cfaf..efd5c2d39 100644 --- a/1-JavaScript/Week2/assignment/ex1-giveCompliment.js +++ b/1-JavaScript/Week2/assignment/ex1-giveCompliment.js @@ -1,29 +1,33 @@ /* ----------------------------------------------------------------------------- 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. +This function gives a random compliment to the name passed as an argument. -----------------------------------------------------------------------------*/ -export function giveCompliment(/* TODO parameter(s) go here */) { - // TODO complete this function +export function giveCompliment(name) { + // Array of 10 compliments + const compliments = [ + 'awesome', + 'fantastic', + 'amazing', + 'incredible', + 'wonderful', + 'brilliant', + 'outstanding', + 'excellent', + 'marvelous', + 'great', + ]; + + // Pick a random compliment + const randomIndex = Math.floor(Math.random() * compliments.length); + const compliment = compliments[randomIndex]; + + // Return the final string + return `You are ${compliment}, ${name}!`; } function main() { - // TODO substitute your own name for "HackYourFuture" - const myName = 'HackYourFuture'; + const myName = 'Majd'; console.log(giveCompliment(myName)); console.log(giveCompliment(myName)); diff --git a/1-JavaScript/Week2/assignment/ex2-dogYears.js b/1-JavaScript/Week2/assignment/ex2-dogYears.js index c88d88dd6..68285fcc6 100644 --- a/1-JavaScript/Week2/assignment/ex2-dogYears.js +++ b/1-JavaScript/Week2/assignment/ex2-dogYears.js @@ -1,25 +1,18 @@ /*------------------------------------------------------------------------------ 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! +This function calculates a dog's age in dog years based on the input human years. +------------------------------------------------------------------------------*/ +export function calculateDogAge(humanYears) { + // Step 1: Multiply the human years by 7 to get dog years + const dogYears = humanYears * 7; -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 + // Step 2: Return the final string + return `Your doggie is ${dogYears} years old in dog years!`; } function main() { + // Test the function with three different ages 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!" diff --git a/1-JavaScript/Week2/assignment/ex3-tellFortune.js b/1-JavaScript/Week2/assignment/ex3-tellFortune.js index c80aae955..7337c7816 100644 --- a/1-JavaScript/Week2/assignment/ex3-tellFortune.js +++ b/1-JavaScript/Week2/assignment/ex3-tellFortune.js @@ -32,30 +32,32 @@ 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(array) { + const randomIndex = Math.floor(Math.random() * array.length); + return array[randomIndex]; } -export function tellFortune(/* TODO add parameter(s) here */) { - // TODO complete this function +export function tellFortune( + numKidsArray, + partnerNamesArray, + locationsArray, + jobTitlesArray +) { + // Select random elements from each array using selectRandomly + const numKids = selectRandomly(numKidsArray); + const partnerName = selectRandomly(partnerNamesArray); + const location = selectRandomly(locationsArray); + const jobTitle = selectRandomly(jobTitlesArray); + + // Return the formatted fortune string + return `You will be a ${jobTitle} in ${location}, married to ${partnerName} with ${numKids} kids.`; } function main() { - const numKids = [ - // TODO add elements here - ]; - - const partnerNames = [ - // TODO add elements here - ]; - - const locations = [ - // TODO add elements here - ]; - - const jobTitles = [ - // TODO add elements here - ]; + const numKids = [0, 1, 2, 3, 4]; // Array of 5 possible numbers of kids + const partnerNames = ['Alice', 'Bob', 'Charlie', 'Dana', 'Eve']; // Array of 5 partner names + const locations = ['Amsterdam', 'Berlin', 'London', 'Paris', 'Rome']; // Array of 5 locations + const jobTitles = ['Engineer', 'Teacher', 'Designer', 'Chef', 'Artist']; // Array of 5 job titles console.log(tellFortune(numKids, partnerNames, locations, jobTitles)); console.log(tellFortune(numKids, partnerNames, locations, jobTitles)); diff --git a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js index a3f15a2a7..9f1b6936b 100644 --- a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js +++ b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js @@ -19,8 +19,19 @@ you have more than 3 items in your shopping cart the first item gets taken out. const shoppingCart = ['bananas', 'milk']; // ! Function to be tested -function addToShoppingCart(/* parameters go here */) { - // TODO complete this function +function addToShoppingCart(item) { + // If an item is provided, add it to the shopping cart + if (item !== undefined) { + shoppingCart.push(item); + } + + // If there are more than 3 items, remove the first one + if (shoppingCart.length > 3) { + shoppingCart.shift(); + } + + // Return the comma-separated string + return `You bought ${shoppingCart.join(', ')}!`; } // ! Test functions (plain vanilla JavaScript) diff --git a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js index fc5e02f23..d24f17764 100644 --- a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js +++ b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js @@ -14,9 +14,18 @@ it pure. Do the following: 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) { + // Create a new array using spread syntax to avoid mutating the original + const newCart = [...shoppingCart, item]; + + // If more than 3 items, remove the first one + if (newCart.length > 3) { + newCart.shift(); + } + + return newCart; } // ! Test functions (plain vanilla JavaScript) diff --git a/1-JavaScript/Week2/assignment/ex6-totalCost.js b/1-JavaScript/Week2/assignment/ex6-totalCost.js index eba643bca..c705ae311 100644 --- a/1-JavaScript/Week2/assignment/ex6-totalCost.js +++ b/1-JavaScript/Week2/assignment/ex6-totalCost.js @@ -19,23 +19,45 @@ instead! 3. Complete the unit test functions and verify that all is working as expected. -----------------------------------------------------------------------------*/ + +// Create an object with 5 grocery items const cartForParty = { - // TODO complete this object + beers: 5.25, + chips: 1.5, + soda: 2.0, + cookies: 3.75, + cheese: 4.25, }; -function calculateTotalPrice(/* TODO parameter(s) go here */) { - // TODO replace this comment with your code +// Function to calculate total price of items in an object +function calculateTotalPrice(cart) { + let total = 0; + + // Loop through object values and sum them + for (const item in cart) { + if (cart.hasOwnProperty(item)) { + total += cart[item]; + } + } + + // Return formatted total + return `Total: €${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); } function test2() { console.log('\nTest 2: return correct output when passed cartForParty'); - // TODO replace this comment with your code + const expected = 'Total: €16.75'; + const actual = calculateTotalPrice(cartForParty); + console.assert( + actual === expected, + `Expected "${expected}", but got "${actual}"` + ); } function test() { diff --git a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js index ae686deab..2976d7f37 100644 --- a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js +++ b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js @@ -29,8 +29,14 @@ const employeeRecords = [ ]; // ! Function under test -function filterPrivateData(/* TODO parameter(s) go here */) { - // TODO complete this function +function filterPrivateData(records) { + // Create a new array by mapping over the original records + return records.map((record) => { + // Destructure the non-private properties + const { name, occupation, email } = record; + // Return a new object with only the allowed properties + return { name, occupation, email }; + }); } // ! Test functions (plain vanilla JavaScript) diff --git a/1-JavaScript/Week2/test-reports/ex1-giveCompliment.report.txt b/1-JavaScript/Week2/test-reports/ex1-giveCompliment.report.txt new file mode 100644 index 000000000..a1fc8221e --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex1-giveCompliment.report.txt @@ -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 (1 ms) + ✅ the `compliments` array should be initialized with 10 strings + ✅ should give a random compliment: You are `compliment`, `name`! (1 ms) + +Test Suites: 1 passed, 1 total +Tests: 7 passed, 7 total +Snapshots: 0 total +Time: 0.479 s, estimated 1 s +Ran all test suites matching /\/Users\/majdjadalhaq\/Desktop\/Assignments-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:30:19 - Unknown word (Majd) diff --git a/1-JavaScript/Week2/test-reports/ex2-dogYears.report.txt b/1-JavaScript/Week2/test-reports/ex2-dogYears.report.txt new file mode 100644 index 000000000..be0b2c3ec --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex2-dogYears.report.txt @@ -0,0 +1,19 @@ +*** Unit Test Error Report *** + + PASS .dist/1-JavaScript/Week2/unit-tests/ex2-dogYears.test.js + js-wk2-ex2-dogYears + ✅ should exist and be executable (1 ms) + ✅ should have all TODO comments removed + ✅ `calculateDogAge` should not contain unneeded console.log calls + ✅ should take a single parameter + ✅ should give 7 dog years for 1 human year + ✅ should give 14 dog years for 2 human years + ✅ give 21 dog years for 3 human years + +Test Suites: 1 passed, 1 total +Tests: 7 passed, 7 total +Snapshots: 0 total +Time: 0.501 s +Ran all test suites matching /\/Users\/majdjadalhaq\/Desktop\/Assignments-Cohort54\/.dist\/1-JavaScript\/Week2\/unit-tests\/ex2-dogYears.test.js/i. +No linting errors detected. +No spelling errors detected. diff --git a/1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt b/1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt new file mode 100644 index 000000000..697e31f6e --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt @@ -0,0 +1,22 @@ +*** Unit Test Error Report *** + + PASS .dist/1-JavaScript/Week2/unit-tests/ex3-tellFortune.test.js + js-wk2-ex3-tellFortune + ✅ should exist and be executable (2 ms) + ✅ should have all TODO comments removed + ✅ `tellFortune` should not contain unneeded console.log calls (1 ms) + ✅ should take four parameters (1 ms) + ✅ should call function `selectRandomly` for each of its arguments (1 ms) + ✅ `numKids` should be an array initialized with 5 elements + ✅ `locations` should be an array initialized with 5 elements (1 ms) + ✅ `partnerNames` should be an array initialized with 5 elements + ✅ `jobTitles` should be an array initialized with 5 elements (1 ms) + ✅ should tell the fortune by randomly selecting array values (1 ms) + +Test Suites: 1 passed, 1 total +Tests: 10 passed, 10 total +Snapshots: 0 total +Time: 0.523 s +Ran all test suites matching /\/Users\/majdjadalhaq\/Desktop\/Assignments-Cohort54\/.dist\/1-JavaScript\/Week2\/unit-tests\/ex3-tellFortune.test.js/i. +No linting errors detected. +No spelling errors detected. diff --git a/1-JavaScript/Week2/test-reports/ex4-shoppingCart.report.txt b/1-JavaScript/Week2/test-reports/ex4-shoppingCart.report.txt new file mode 100644 index 000000000..d985f405c --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex4-shoppingCart.report.txt @@ -0,0 +1,3 @@ +A unit test file was not provided for this exercise. +No linting errors detected. +No spelling errors detected. diff --git a/1-JavaScript/Week2/test-reports/ex5-shoppingCartPure.report.txt b/1-JavaScript/Week2/test-reports/ex5-shoppingCartPure.report.txt new file mode 100644 index 000000000..d985f405c --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex5-shoppingCartPure.report.txt @@ -0,0 +1,3 @@ +A unit test file was not provided for this exercise. +No linting errors detected. +No spelling errors detected. diff --git a/1-JavaScript/Week2/test-reports/ex6-totalCost.report.txt b/1-JavaScript/Week2/test-reports/ex6-totalCost.report.txt new file mode 100644 index 000000000..d985f405c --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex6-totalCost.report.txt @@ -0,0 +1,3 @@ +A unit test file was not provided for this exercise. +No linting errors detected. +No spelling errors detected. diff --git a/1-JavaScript/Week2/test-reports/ex7-mindPrivacy.report.txt b/1-JavaScript/Week2/test-reports/ex7-mindPrivacy.report.txt new file mode 100644 index 000000000..0462c4fd7 --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex7-mindPrivacy.report.txt @@ -0,0 +1,10 @@ +A unit test file was not provided for this exercise. +No linting errors detected. + + +*** Spell Checker Report *** + +1-JavaScript/Week2/assignment/ex7-mindPrivacy.js:16:12 - Unknown word (majd) +1-JavaScript/Week2/assignment/ex7-mindPrivacy.js:23:12 - Unknown word (marit) +1-JavaScript/Week2/assignment/ex7-mindPrivacy.js:52:14 - Unknown word (majd) +1-JavaScript/Week2/assignment/ex7-mindPrivacy.js:57:14 - Unknown word (marit)