From 9dd72d1ba902a1a49b9ca102f13304abbf8a2595 Mon Sep 17 00:00:00 2001 From: Heresh <108929624+HereshT@users.noreply.github.com> Date: Sun, 19 Mar 2023 08:09:31 +0000 Subject: [PATCH 1/6] mandatory/1-recipes.js - The Recipe Card is Done. --- 2-mandatory/1-recipes.js | 46 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..3c52e4be 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,48 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +const recipe1 = { + title: "Strawberry Banana Smoothie", + servings: 2, + ingredients: ["strawberries", "banana", "orange juice"], +}; + +const recipe2 = { + title: "Mixed Berry Smoothie", + servings: 2, + ingredients: ["mixed berries", "banana", "almond milk"], +}; + +const recipe3 = { + title: "Mango Pineapple Smoothie", + servings: 2, + ingredients: ["mango", "pineapple", "coconut milk"], +}; + +const recipe4 = { + title: "Peach Raspberry Smoothie", + servings: 2, + ingredients: ["peaches", "raspberries", "vanilla yogurt"], +}; + +const recipe5 = { + title: "Kiwi Strawberry Smoothie", + servings: 2, + ingredients: ["kiwi", "strawberries", "apple juice"], +}; + +const smoothieRecipes = [recipe1, recipe2, recipe3, recipe4, recipe5]; + +for (const recipe of smoothieRecipes) { + console.log(recipe.title); + console.log(`Serves: ${recipe.servings}`); + console.log("Ingredients:"); + for (const ingredient of recipe.ingredients) { + console.log(ingredient); + } + console.log("\n"); + console.log("🍹🍹🍹🍹🍹"); + console.log("\n"); +} From 42a4a3c80b4664f155977594fd23aa1e2beb68b6 Mon Sep 17 00:00:00 2001 From: Heresh <108929624+HereshT@users.noreply.github.com> Date: Sun, 19 Mar 2023 17:15:48 +0000 Subject: [PATCH 2/6] mandatory/2-currency-code-lookup.js is Done. --- 2-mandatory/2-currency-code-lookup.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..c1352312 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,6 +19,15 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here + // This is the easy way, using The Object.fromEntries() method + // return Object.fromEntries(countryCurrencyCodes); + + const COUNTRY_CURRENCY_CODES_OBJECT = {}; + for (let i = 0; i < countryCurrencyCodes.length; i++) { + const [countryCode, currencyCode] = countryCurrencyCodes[i]; + COUNTRY_CURRENCY_CODES_OBJECT[countryCode] = currencyCode; + } + return COUNTRY_CURRENCY_CODES_OBJECT; } /* ======= TESTS - DO NOT MODIFY ===== @@ -34,4 +43,4 @@ test("creates country currency code lookup", () => { NG: "NGN", MX: "MXN", }); -}); \ No newline at end of file +}); From c710e61e1a93b65138253cef9e0eb31c44e91ba0 Mon Sep 17 00:00:00 2001 From: Heresh <108929624+HereshT@users.noreply.github.com> Date: Sun, 19 Mar 2023 21:09:29 +0000 Subject: [PATCH 3/6] mandatory/3-shopping-list.js is Done. --- 2-mandatory/2-currency-code-lookup.js | 2 ++ 2-mandatory/3-shopping-list.js | 30 +++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index c1352312..f03ea953 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -26,6 +26,8 @@ function createLookup(countryCurrencyCodes) { for (let i = 0; i < countryCurrencyCodes.length; i++) { const [countryCode, currencyCode] = countryCurrencyCodes[i]; COUNTRY_CURRENCY_CODES_OBJECT[countryCode] = currencyCode; + + // I learned this: so we can pass the array's items into the bracket notation, and not always a string. as well as setting key/value pairs - (values) - again using a variable name. } return COUNTRY_CURRENCY_CODES_OBJECT; } diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..70c2904e 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -10,7 +10,7 @@ The createShoppingList function should return an object with two properties: - "name" of the recipe, which is a string, - - "items", which is an arry of the missing ingredients that need to be on the shopping list + - "items", which is an array of the missing ingredients that need to be on the shopping list */ let pantry = { @@ -20,6 +20,21 @@ let pantry = { function createShoppingList(recipe) { // write code here + + let missingIngredients = []; + + // 2 lovely array methods which I learned here + + const allPantryContents = Object.values(pantry).flat(); + + for (let i = 0; i < recipe.ingredients.length; i++) { + if (!allPantryContents.includes(recipe.ingredients[i])) + missingIngredients.push(recipe.ingredients[i]); + } + return { + name: recipe.name, + items: missingIngredients, + }; } /* ======= TESTS - DO NOT MODIFY ===== @@ -43,11 +58,18 @@ test("createShoppingList works for pancakes recipe", () => { test("createShoppingList works for margherita pizza recipe", () => { let recipe2 = { name: "margherita pizza", - ingredients: ["flour", "salt", "yeast", "tinned tomatoes", "oregano", "mozarella"], + ingredients: [ + "flour", + "salt", + "yeast", + "tinned tomatoes", + "oregano", + "mozarella", + ], }; expect(createShoppingList(recipe2)).toEqual({ name: "margherita pizza", - items: ["flour", "yeast", "mozarella"] + items: ["flour", "yeast", "mozarella"], }); -}); \ No newline at end of file +}); From 0524aee3d6222b9bf4e65642fac4c417f8d02cd9 Mon Sep 17 00:00:00 2001 From: Heresh <108929624+HereshT@users.noreply.github.com> Date: Mon, 20 Mar 2023 12:02:18 +0000 Subject: [PATCH 4/6] mandatory/4-restaurant.js is Done. --- 2-mandatory/4-restaurant.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..1dfb7634 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,7 +21,26 @@ const MENU = { let cashRegister = { // write code here -} + + orderBurger: function (balance) { + let enoughBalance = balance >= MENU.burger; + // or balance - MENU.burger must be > or = 0 + if (enoughBalance) { + balance = balance - MENU.burger; // OR balance -= MENU.burger + } + return balance; + }, + + // and same goes for the orderFalafel too + + orderFalafel: function (balance) { + let enoughBalance = balance >= MENU.falafel; + if (enoughBalance) { + balance -= MENU.falafel; + } + return balance; + }, +}; /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` From 6274bf365595edf24f5b12c60fcde6bfeb7430b8 Mon Sep 17 00:00:00 2001 From: Heresh <108929624+HereshT@users.noreply.github.com> Date: Mon, 20 Mar 2023 12:51:24 +0000 Subject: [PATCH 5/6] mandatory/5-writing-tests.js is Done. --- 2-mandatory/5-writing-tests.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..efd39e7b 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -35,7 +35,7 @@ function convertScoreToGrade(score) { passes. */ test("a score of 83 is grade A", () => { - expect(convertScoreToGrade(83), "Z"); + expect(convertScoreToGrade(83)).toBe("A"); }); /* @@ -43,33 +43,44 @@ test("a score of 83 is grade A", () => { write a matching test */ -test.skip("a score of 71 is grade B", () => { +test("a score of 71 is grade B", () => { /* Remove the .skip above, then write the test body. */ + expect(convertScoreToGrade(71)).toBe("B"); }); /* - Write a test that checks a score of 68 is grade C -*/ - -/* - Write a test that checks a score of 55 is grade D -*/ + /* Write a test that checks a score of 68 is grade C */ +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toBe("C"); +}); /* Write a test that checks a score of 55 is grade D */ +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toBe("D"); +}); /* Write a test that checks a score of 49 is grade E */ +test("a score of 49 is grade E", () => { + expect(convertScoreToGrade(49)).toBe("E"); +}); /* Write a test that checks a score of 30 is grade E */ +test("a score of 30 is grade E", () => { + expect(convertScoreToGrade(30)).toBe("E"); +}); /* Write a test that checks a score of 70 is grade B */ +test("a score of 70 is grade B", () => { + expect(convertScoreToGrade(70)).toBe("B"); +}); From 6c71f3c6e9925cff1b6bd6187d32faddb3145ac7 Mon Sep 17 00:00:00 2001 From: Heresh <108929624+HereshT@users.noreply.github.com> Date: Mon, 20 Mar 2023 13:43:42 +0000 Subject: [PATCH 6/6] mandatory/6-writing-tests-advanced.js is Done. --- 2-mandatory/6-writing-tests-advanced.js | 51 +++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..930e8574 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -10,7 +10,8 @@ trainee has completed. */ -function convertScoreToGrade() { +function convertScoreToGrade(score) { + //the score parameter was not being passed to the function let grade = null; if (score >= 80) { @@ -56,6 +57,16 @@ function formatCourseworkResult(trainee) { } */ +test("formatCourseworkResult", () => { + const trainee = { + name: "Xin", + score: 63, + }; + expect(formatCourseworkResult(trainee)).toBe( + "Xin's coursework was marked as grade C." + ); +}); + /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -63,6 +74,15 @@ function formatCourseworkResult(trainee) { score: 78 } */ +test("formatCourseworkResult", () => { + const trainee = { + name: "Mona", + score: 78, + }; + expect(formatCourseworkResult(trainee)).toBe( + "Mona's coursework was marked as grade B." + ); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -73,7 +93,17 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ - +test("formatCourseworkResult", () => { + const trainee = { + name: "Ali", + score: 49, + age: 33, + subjects: ["JavaScript", "React", "CSS"], + }; + expect(formatCourseworkResult(trainee)).toBe( + "Ali's coursework was marked as grade E." + ); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -81,7 +111,13 @@ function formatCourseworkResult(trainee) { age: 29 } */ - +test("formatCourseworkResult", () => { + const trainee = { + score: 90, + age: 29, + }; + expect(formatCourseworkResult(trainee)).toBe("Error: No trainee name!"); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -89,3 +125,12 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("formatCourseworkResult", () => { + const trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"], + }; + expect(formatCourseworkResult(trainee)).toBe( + "Error: Coursework percent is not a number!" + ); +});