From 0a36dd80253fa88f5bce2693bdcc3a5152962ece Mon Sep 17 00:00:00 2001 From: Alaa Date: Tue, 26 Aug 2025 22:22:37 +0200 Subject: [PATCH 1/3] Add the answer code to the assignment --- .../Week2/assignment/ex1-giveCompliment.js | 24 ++++++++++--- 1-JavaScript/Week2/assignment/ex2-dogYears.js | 6 ++-- .../Week2/assignment/ex3-tellFortune.js | 34 ++++++++++++++++-- .../Week2/assignment/ex4-shoppingCart.js | 36 +++++++++++-------- .../Week2/assignment/ex5-shoppingCartPure.js | 8 ++++- .../Week2/assignment/ex6-totalCost.js | 19 ++++++++-- .../Week2/assignment/ex7-mindPrivacy.js | 7 +++- 7 files changed, 106 insertions(+), 28 deletions(-) diff --git a/1-JavaScript/Week2/assignment/ex1-giveCompliment.js b/1-JavaScript/Week2/assignment/ex1-giveCompliment.js index 93806cfaf..29e9aa7ba 100644 --- a/1-JavaScript/Week2/assignment/ex1-giveCompliment.js +++ b/1-JavaScript/Week2/assignment/ex1-giveCompliment.js @@ -17,19 +17,35 @@ Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-J 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 */) { +export function giveCompliment(name) { // TODO complete this function + const compliments = [ + "kind", + "smart", + "beautiful", + "strong", + "brave", + "talented", + "generous", + "helpful", + "creative", + "friendly", + ]; + + return `${name} you are ${ + compliments[Math.floor(Math.random() * compliments.length)] + }`; } function main() { // TODO substitute your own name for "HackYourFuture" - const myName = 'HackYourFuture'; + const myName = "Alaa"; console.log(giveCompliment(myName)); console.log(giveCompliment(myName)); console.log(giveCompliment(myName)); - const yourName = 'Amsterdam'; + const yourName = "Amsterdam"; console.log(giveCompliment(yourName)); console.log(giveCompliment(yourName)); @@ -37,6 +53,6 @@ function main() { } // ! Do not change or remove the code below -if (process.env.NODE_ENV !== 'test') { +if (process.env.NODE_ENV !== "test") { main(); } diff --git a/1-JavaScript/Week2/assignment/ex2-dogYears.js b/1-JavaScript/Week2/assignment/ex2-dogYears.js index c88d88dd6..bc3db07a5 100644 --- a/1-JavaScript/Week2/assignment/ex2-dogYears.js +++ b/1-JavaScript/Week2/assignment/ex2-dogYears.js @@ -9,14 +9,16 @@ calculate it! - 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. + // 1 human year = 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 */) { +export function calculateDogAge(age /* TODO parameter(s) go here */) { // TODO complete this function + return `Your doggie is ${age * 7} years old in dog years!`; } function main() { @@ -26,6 +28,6 @@ function main() { } // ! Do not change or remove the code below -if (process.env.NODE_ENV !== 'test') { +if (process.env.NODE_ENV !== "test") { main(); } diff --git a/1-JavaScript/Week2/assignment/ex3-tellFortune.js b/1-JavaScript/Week2/assignment/ex3-tellFortune.js index c80aae955..371d72e63 100644 --- a/1-JavaScript/Week2/assignment/ex3-tellFortune.js +++ b/1-JavaScript/Week2/assignment/ex3-tellFortune.js @@ -32,29 +32,57 @@ 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 */) { +function selectRandomly(arr /* TODO parameter(s) go here */) { // TODO complete this function + return arr[Math.floor(Math.random() * arr.length)]; } -export function tellFortune(/* TODO add parameter(s) here */) { +export function tellFortune( + childrenArr, + partnersArr, + locationsArr, + jobsArr /* TODO add parameter(s) here */ +) { // TODO complete this function + 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.`; } function main() { const numKids = [ // TODO add elements here + 2, 4, 1, 7, 5, ]; const partnerNames = [ // TODO add elements here + "Yusuf", + "Leen", + "Sarah", + "Eliana", + "Amal", ]; const locations = [ // TODO add elements here + "Amsterdam", + "Breda", + "Tilburg", + "Utrecht", + "Rotterdam", ]; const jobTitles = [ // TODO add elements here + "Programmer", + "Teacher", + "Soldier", + "Manager", + "Musician", ]; console.log(tellFortune(numKids, partnerNames, locations, jobTitles)); @@ -63,6 +91,6 @@ function main() { } // ! Do not change or remove the code below -if (process.env.NODE_ENV !== 'test') { +if (process.env.NODE_ENV !== "test") { main(); } diff --git a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js index a3f15a2a7..17be4525c 100644 --- a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js +++ b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js @@ -16,48 +16,56 @@ you have more than 3 items in your shopping cart the first item gets taken out. 2. Confirm that your code passes the unit tests. -----------------------------------------------------------------------------*/ -const shoppingCart = ['bananas', 'milk']; +const shoppingCart = ["bananas", "milk"]; // ! Function to be tested -function addToShoppingCart(/* parameters go here */) { +function addToShoppingCart(item /* parameters go here */) { // TODO complete this function + if (!item) { + return `You bought ${shoppingCart.join(", ")}!`; + } + // shoppingCart.length > 2 ? shoppingCart.splice(0, 1) : shoppingCart; + shoppingCart.length > 2 && shoppingCart.splice(0, 1); + + shoppingCart.push(item); + return `You bought ${shoppingCart.join(", ")}!`; } // ! Test functions (plain vanilla JavaScript) function test1() { console.log( - 'Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged' + "Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged" ); - const expected = 'You bought bananas, milk!'; + const expected = "You bought bananas, milk!"; const actual = addToShoppingCart(); console.assert(actual === expected); } function test2() { - console.log('Test 2: addShoppingCart() should take one parameter'); + console.log("Test 2: addShoppingCart() should take one parameter"); const expected = 1; const actual = addToShoppingCart.length; console.assert(actual === expected); } function test3() { - console.log('Test 3: `chocolate` should be added'); - const expected = 'You bought bananas, milk, chocolate!'; - const actual = addToShoppingCart('chocolate'); + console.log("Test 3: `chocolate` should be added"); + const expected = "You bought bananas, milk, chocolate!"; + const actual = addToShoppingCart("chocolate"); console.assert(actual === expected); } function test4() { - console.log('Test 4: `waffles` should be added and `bananas` removed'); - const expected = 'You bought milk, chocolate, waffles!'; - const actual = addToShoppingCart('waffles'); + console.log("Test 4: `waffles` should be added and `bananas` removed"); + const expected = "You bought milk, chocolate, waffles!"; + const actual = addToShoppingCart("waffles"); console.assert(actual === expected); } function test5() { - console.log('Test 5: `tea` should be added and `milk` removed'); - const expected = 'You bought chocolate, waffles, tea!'; - const actual = addToShoppingCart('tea'); + console.log("Test 5: `tea` should be added and `milk` removed"); + const expected = "You bought chocolate, waffles, tea!"; + const actual = addToShoppingCart("tea"); console.assert(actual === expected); } diff --git a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js index fc5e02f23..bf9340a01 100644 --- a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js +++ b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js @@ -15,8 +15,14 @@ it pure. Do the following: 5. Confirm that you function passes the provided unit tests. ------------------------------------------------------------------------------*/ // ! Function under test -function addToShoppingCart(/* TODO parameter(s) go here */) { +function addToShoppingCart([...shoppingCart],item ) { // TODO complete this function + if (!item) { + return shoppingCart; + } + shoppingCart.length > 2 ? shoppingCart.splice(0, 1) : shoppingCart; + shoppingCart.push(item); + return shoppingCart; } // ! 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..a0d30ad1a 100644 --- a/1-JavaScript/Week2/assignment/ex6-totalCost.js +++ b/1-JavaScript/Week2/assignment/ex6-totalCost.js @@ -21,21 +21,34 @@ instead! -----------------------------------------------------------------------------*/ const cartForParty = { // TODO complete this object + chips: 1.75, + juice: 2.4, + frenchfries: 4.99, + cake: 8.99, + choclate: 2.99, }; -function calculateTotalPrice(/* TODO parameter(s) go here */) { +function calculateTotalPrice(obj/* TODO parameter(s) go here */) { // TODO replace this comment with your code + let sum = 0; + for (const [_, value] of Object.entries(obj)) { + sum += value; + } + return Number(sum.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 = 21.12; + const actual = calculateTotalPrice(cartForParty); + console.assert(actual === expected); } function test() { diff --git a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js index ae686deab..4c581d139 100644 --- a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js +++ b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js @@ -29,8 +29,13 @@ const employeeRecords = [ ]; // ! Function under test -function filterPrivateData(/* TODO parameter(s) go here */) { +function filterPrivateData(employeesRecord/* TODO parameter(s) go here */) { // TODO complete this function + const nonPrivateData = []; + for (let { name, occupation, email } of employeesRecord) { + nonPrivateData.push({ name, occupation, email }); + } + return nonPrivateData; } // ! Test functions (plain vanilla JavaScript) From 0af6842306ac25e9df914aaa9eda0bc43a189645 Mon Sep 17 00:00:00 2001 From: Alaa Date: Sun, 31 Aug 2025 18:47:19 +0200 Subject: [PATCH 2/3] Add generated test reports --- .test-summary/TEST_SUMMARY.md | 15 ++++ .../Week2/assignment/ex1-giveCompliment.js | 52 ++++-------- 1-JavaScript/Week2/assignment/ex2-dogYears.js | 30 ++----- .../Week2/assignment/ex3-tellFortune.js | 80 ++----------------- .../Week2/assignment/ex4-shoppingCart.js | 54 ++++--------- .../Week2/assignment/ex5-shoppingCartPure.js | 23 +----- .../Week2/assignment/ex6-totalCost.js | 27 +------ .../Week2/assignment/ex7-mindPrivacy.js | 18 +---- .../ex1-giveCompliment.report.txt | 23 ++++++ .../test-reports/ex2-dogYears.report.txt | 19 +++++ .../test-reports/ex3-tellFortune.report.txt | 29 +++++++ .../test-reports/ex4-shoppingCart.report.txt | 3 + .../ex5-shoppingCartPure.report.txt | 3 + .../test-reports/ex6-totalCost.report.txt | 8 ++ .../test-reports/ex7-mindPrivacy.report.txt | 3 + 15 files changed, 148 insertions(+), 239 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 29e9aa7ba..1bd7b33f6 100644 --- a/1-JavaScript/Week2/assignment/ex1-giveCompliment.js +++ b/1-JavaScript/Week2/assignment/ex1-giveCompliment.js @@ -1,58 +1,34 @@ -/* ----------------------------------------------------------------------------- -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(name) { - // TODO complete this function const compliments = [ - "kind", - "smart", - "beautiful", - "strong", - "brave", - "talented", - "generous", - "helpful", - "creative", - "friendly", + 'kind', + 'smart', + 'beautiful', + 'strong', + 'brave', + 'talented', + 'generous', + 'helpful', + 'creative', + 'friendly', ]; - return `${name} you are ${ - compliments[Math.floor(Math.random() * compliments.length)] - }`; + return `You are ${compliments[Math.floor(Math.random() * compliments.length)]}, ${name}!`; } function main() { - // TODO substitute your own name for "HackYourFuture" - const myName = "Alaa"; + const myName = 'Alaa'; console.log(giveCompliment(myName)); console.log(giveCompliment(myName)); console.log(giveCompliment(myName)); - const yourName = "Amsterdam"; + const yourName = 'Amsterdam'; console.log(giveCompliment(yourName)); console.log(giveCompliment(yourName)); console.log(giveCompliment(yourName)); } -// ! Do not change or remove the code below -if (process.env.NODE_ENV !== "test") { +if (process.env.NODE_ENV !== 'test') { main(); } diff --git a/1-JavaScript/Week2/assignment/ex2-dogYears.js b/1-JavaScript/Week2/assignment/ex2-dogYears.js index bc3db07a5..4f1c662e3 100644 --- a/1-JavaScript/Week2/assignment/ex2-dogYears.js +++ b/1-JavaScript/Week2/assignment/ex2-dogYears.js @@ -1,33 +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. - // 1 human year = 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(age /* TODO parameter(s) go here */) { - // TODO complete this function +export function calculateDogAge(age) { return `Your doggie is ${age * 7} years old in dog years!`; } 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") { +if (process.env.NODE_ENV !== 'test') { main(); } diff --git a/1-JavaScript/Week2/assignment/ex3-tellFortune.js b/1-JavaScript/Week2/assignment/ex3-tellFortune.js index 371d72e63..1b8f48eec 100644 --- a/1-JavaScript/Week2/assignment/ex3-tellFortune.js +++ b/1-JavaScript/Week2/assignment/ex3-tellFortune.js @@ -1,49 +1,8 @@ -/*------------------------------------------------------------------------------ -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(arr /* TODO parameter(s) go here */) { - // TODO complete this function +function selectRandomly(arr) { return arr[Math.floor(Math.random() * arr.length)]; } -export function tellFortune( - childrenArr, - partnersArr, - locationsArr, - jobsArr /* 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); @@ -53,44 +12,19 @@ export function tellFortune( } function main() { - const numKids = [ - // TODO add elements here - 2, 4, 1, 7, 5, - ]; + const numKids = [2, 4, 1, 7, 5]; - const partnerNames = [ - // TODO add elements here - "Yusuf", - "Leen", - "Sarah", - "Eliana", - "Amal", - ]; + const partnerNames = ['Yusuf', 'Leen', 'Sarah', 'Eliana', 'Amal']; - const locations = [ - // TODO add elements here - "Amsterdam", - "Breda", - "Tilburg", - "Utrecht", - "Rotterdam", - ]; + const locations = ['Amsterdam', 'Breda', 'Tilburg', 'Utrecht', 'Rotterdam']; - const jobTitles = [ - // TODO add elements here - "Programmer", - "Teacher", - "Soldier", - "Manager", - "Musician", - ]; + const jobTitles = ['Programmer', 'Teacher', 'Soldier', 'Manager', 'Musician']; 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") { +if (process.env.NODE_ENV !== 'test') { main(); } diff --git a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js index 17be4525c..e6f3fd5db 100644 --- a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js +++ b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js @@ -1,71 +1,49 @@ -/*------------------------------------------------------------------------------ -Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-4-shopping-at-the-supermarket +const shoppingCart = ['bananas', 'milk']; -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 !", where - 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(item /* parameters go here */) { - // TODO complete this function +function addToShoppingCart(item) { if (!item) { - return `You bought ${shoppingCart.join(", ")}!`; + return `You bought ${shoppingCart.join(', ')}!`; } - // shoppingCart.length > 2 ? shoppingCart.splice(0, 1) : shoppingCart; shoppingCart.length > 2 && shoppingCart.splice(0, 1); shoppingCart.push(item); - return `You bought ${shoppingCart.join(", ")}!`; + return `You bought ${shoppingCart.join(', ')}!`; } -// ! Test functions (plain vanilla JavaScript) function test1() { console.log( - "Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged" + 'Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged' ); - const expected = "You bought bananas, milk!"; + const expected = 'You bought bananas, milk!'; const actual = addToShoppingCart(); console.assert(actual === expected); } function test2() { - console.log("Test 2: addShoppingCart() should take one parameter"); + console.log('Test 2: addShoppingCart() should take one parameter'); const expected = 1; const actual = addToShoppingCart.length; console.assert(actual === expected); } function test3() { - console.log("Test 3: `chocolate` should be added"); - const expected = "You bought bananas, milk, chocolate!"; - const actual = addToShoppingCart("chocolate"); + console.log('Test 3: `chocolate` should be added'); + const expected = 'You bought bananas, milk, chocolate!'; + const actual = addToShoppingCart('chocolate'); console.assert(actual === expected); } function test4() { - console.log("Test 4: `waffles` should be added and `bananas` removed"); - const expected = "You bought milk, chocolate, waffles!"; - const actual = addToShoppingCart("waffles"); + console.log('Test 4: `waffles` should be added and `bananas` removed'); + const expected = 'You bought milk, chocolate, waffles!'; + const actual = addToShoppingCart('waffles'); console.assert(actual === expected); } function test5() { - console.log("Test 5: `tea` should be added and `milk` removed"); - const expected = "You bought chocolate, waffles, tea!"; - const actual = addToShoppingCart("tea"); + console.log('Test 5: `tea` should be added and `milk` removed'); + const expected = 'You bought chocolate, waffles, tea!'; + const actual = addToShoppingCart('tea'); console.assert(actual === expected); } diff --git a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js index bf9340a01..74a6c83b5 100644 --- a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js +++ b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js @@ -1,22 +1,4 @@ -/*------------------------------------------------------------------------------ -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([...shoppingCart],item ) { - // TODO complete this function +function addToShoppingCart([...shoppingCart], item) { if (!item) { return shoppingCart; } @@ -25,7 +7,6 @@ function addToShoppingCart([...shoppingCart],item ) { return shoppingCart; } -// ! Test functions (plain vanilla JavaScript) function test1() { console.log('Test 1: addToShoppingCart should take two parameters'); console.assert(addToShoppingCart.length === 2); @@ -33,8 +14,6 @@ function test1() { 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'); diff --git a/1-JavaScript/Week2/assignment/ex6-totalCost.js b/1-JavaScript/Week2/assignment/ex6-totalCost.js index a0d30ad1a..5226bb44c 100644 --- a/1-JavaScript/Week2/assignment/ex6-totalCost.js +++ b/1-JavaScript/Week2/assignment/ex6-totalCost.js @@ -1,26 +1,4 @@ -/*------------------------------------------------------------------------------ -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, @@ -28,8 +6,7 @@ const cartForParty = { choclate: 2.99, }; -function calculateTotalPrice(obj/* 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; @@ -37,11 +14,9 @@ function calculateTotalPrice(obj/* TODO parameter(s) go here */) { return Number(sum.toFixed(2)); } -// ! Test functions (plain vanilla JavaScript) function test1() { console.log('\nTest 1: calculateTotalPrice should take one parameter'); console.assert(calculateTotalPrice.length === 1); - } function test2() { diff --git a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js index 4c581d139..19b71e9ad 100644 --- a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js +++ b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js @@ -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,9 +15,7 @@ const employeeRecords = [ }, ]; -// ! Function under test -function filterPrivateData(employeesRecord/* 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 }); @@ -38,7 +23,6 @@ function filterPrivateData(employeesRecord/* TODO parameter(s) go here */) { return nonPrivateData; } -// ! Test functions (plain vanilla JavaScript) function test1() { console.log('Test 1: filterPrivateData should take one parameter'); console.assert(filterPrivateData.length === 1); 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..8ce987726 --- /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 + ✅ 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) 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..ea605f727 --- /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 (2 ms) + ✅ should have all TODO comments removed (1 ms) + ✅ `calculateDogAge` should not contain unneeded console.log calls (1 ms) + ✅ should take a single parameter (1 ms) + ✅ 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.512 s +Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-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..70b2d34ce --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt @@ -0,0 +1,29 @@ +*** 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 (1 ms) + ✅ `tellFortune` should not contain unneeded console.log calls + ✅ should take four parameters + ✅ 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.532 s +Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-Cohort54\/.dist\/1-JavaScript\/Week2\/unit-tests\/ex3-tellFortune.test.js/i. +No linting errors detected. + + +*** Spell Checker Report *** + +1-JavaScript/Week2/assignment/ex3-tellFortune.js:17:26 - Unknown word (Yusuf) +1-JavaScript/Week2/assignment/ex3-tellFortune.js:17:35 - Unknown word (Leen) +1-JavaScript/Week2/assignment/ex3-tellFortune.js:17:52 - Unknown word (Eliana) +1-JavaScript/Week2/assignment/ex3-tellFortune.js:17:62 - Unknown word (Amal) 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..d85aca3ec --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex6-totalCost.report.txt @@ -0,0 +1,8 @@ +A unit test file was not provided for this exercise. +No linting errors detected. + + +*** Spell Checker Report *** + +1-JavaScript/Week2/assignment/ex6-totalCost.js:4:3 - Unknown word (frenchfries) +1-JavaScript/Week2/assignment/ex6-totalCost.js:6:3 - Unknown word (choclate) fix: (chocolate) 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..d985f405c --- /dev/null +++ b/1-JavaScript/Week2/test-reports/ex7-mindPrivacy.report.txt @@ -0,0 +1,3 @@ +A unit test file was not provided for this exercise. +No linting errors detected. +No spelling errors detected. From 776556815daeb69ced063ba829c07a24863ce614 Mon Sep 17 00:00:00 2001 From: Alaa Date: Sat, 6 Sep 2025 23:48:29 +0200 Subject: [PATCH 3/3] Error fixed assignment week2 --- 1-JavaScript/Week2/assignment/ex3-tellFortune.js | 4 ++-- 1-JavaScript/Week2/assignment/ex4-shoppingCart.js | 11 +++++------ .../Week2/assignment/ex5-shoppingCartPure.js | 10 +++++----- 1-JavaScript/Week2/assignment/ex6-totalCost.js | 13 ++++++++----- 1-JavaScript/Week2/assignment/ex7-mindPrivacy.js | 11 +++++++---- .../Week2/test-reports/ex3-tellFortune.report.txt | 12 ++++++------ 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/1-JavaScript/Week2/assignment/ex3-tellFortune.js b/1-JavaScript/Week2/assignment/ex3-tellFortune.js index 1b8f48eec..f06f079db 100644 --- a/1-JavaScript/Week2/assignment/ex3-tellFortune.js +++ b/1-JavaScript/Week2/assignment/ex3-tellFortune.js @@ -1,5 +1,5 @@ -function selectRandomly(arr) { - return arr[Math.floor(Math.random() * arr.length)]; +function selectRandomly(choices) { + return choices[Math.floor(Math.random() * choices.length)]; } export function tellFortune(childrenArr, partnersArr, locationsArr, jobsArr) { diff --git a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js index e6f3fd5db..5a2b8ead4 100644 --- a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js +++ b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js @@ -1,13 +1,12 @@ const shoppingCart = ['bananas', 'milk']; function addToShoppingCart(item) { - if (!item) { - return `You bought ${shoppingCart.join(', ')}!`; - } - shoppingCart.length > 2 && shoppingCart.splice(0, 1); - shoppingCart.push(item); - return `You bought ${shoppingCart.join(', ')}!`; + if (shoppingCart.length > 3) { + shoppingCart.shift(); + } + const shoppingCartItems = shoppingCart.join(', '); + return `You bought ${shoppingCartItems}!`; } function test1() { diff --git a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js index 74a6c83b5..0f3d42d27 100644 --- a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js +++ b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js @@ -1,10 +1,10 @@ function addToShoppingCart([...shoppingCart], item) { - if (!item) { - return shoppingCart; - } - shoppingCart.length > 2 ? shoppingCart.splice(0, 1) : shoppingCart; shoppingCart.push(item); - return shoppingCart; + if (shoppingCart.length > 3) { + shoppingCart.shift(); + } + const shoppingCartItems = shoppingCart.join(', '); + return `You bought ${shoppingCartItems}!`; } function test1() { diff --git a/1-JavaScript/Week2/assignment/ex6-totalCost.js b/1-JavaScript/Week2/assignment/ex6-totalCost.js index 5226bb44c..917a334a2 100644 --- a/1-JavaScript/Week2/assignment/ex6-totalCost.js +++ b/1-JavaScript/Week2/assignment/ex6-totalCost.js @@ -7,11 +7,14 @@ const cartForParty = { }; function calculateTotalPrice(obj) { - let sum = 0; - for (const [_, value] of Object.entries(obj)) { - sum += value; - } - return Number(sum.toFixed(2)); + // 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)); } function test1() { diff --git a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js index 19b71e9ad..3b1b92618 100644 --- a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js +++ b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js @@ -16,10 +16,13 @@ const employeeRecords = [ ]; function filterPrivateData(employeesRecord) { - const nonPrivateData = []; - for (let { name, occupation, email } of employeesRecord) { - nonPrivateData.push({ name, occupation, email }); - } + // 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; } diff --git a/1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt b/1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt index 70b2d34ce..145561839 100644 --- a/1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt +++ b/1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt @@ -4,19 +4,19 @@ js-wk2-ex3-tellFortune ✅ should exist and be executable (2 ms) ✅ should have all TODO comments removed (1 ms) - ✅ `tellFortune` should not contain unneeded console.log calls + ✅ `tellFortune` should not contain unneeded console.log calls (1 ms) ✅ should take four parameters ✅ 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) + ✅ `numKids` should be an array initialized with 5 elements (1 ms) + ✅ `locations` should be an array initialized with 5 elements ✅ `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) + ✅ `jobTitles` should be an array initialized with 5 elements + ✅ should tell the fortune by randomly selecting array values Test Suites: 1 passed, 1 total Tests: 10 passed, 10 total Snapshots: 0 total -Time: 0.532 s +Time: 1.661 s Ran all test suites matching /\/Users\/Alaa\/Desktop\/JavaScript-Cohort54\/.dist\/1-JavaScript\/Week2\/unit-tests\/ex3-tellFortune.test.js/i. No linting errors detected.