From a4bdf75959694db901b50bd934ba17d3662668b4 Mon Sep 17 00:00:00 2001 From: Muath Alawadhi <109815858+Muath-Alawadhi@users.noreply.github.com> Date: Thu, 30 Mar 2023 05:54:56 +0100 Subject: [PATCH 1/2] exercises : Done --- 1-exercises/A-accessing-values/exercise1.js | 15 +++++++-------- 1-exercises/A-accessing-values/exercise2.js | 6 +++--- 1-exercises/A-accessing-values/exercise3.js | 7 ++++--- 1-exercises/B-setting-values/exercise1.js | 10 +++++++--- 1-exercises/B-setting-values/exercise2.js | 11 ++++++----- 1-exercises/C-undefined-properties/exercise.js | 10 +++++++--- 1-exercises/D-object-methods/exercise.js | 8 +++++--- 7 files changed, 39 insertions(+), 28 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..3bfcd152 100644 --- a/1-exercises/A-accessing-values/exercise1.js +++ b/1-exercises/A-accessing-values/exercise1.js @@ -5,10 +5,10 @@ */ let dog = { - breed: "Dalmatian", - name: "Spot", - isHungry: true, - happiness: 6 + breed: "Dalmatian", // string + name: "Spot", // string + isHungry: true, // boolean + happiness: 6, // number }; /* @@ -16,13 +16,12 @@ let dog = { Log the name and breed of this dog using dot notation. */ -let dogName; // complete the code -let dogBreed; // complete the code -console.log(`${dogName} is a ${dogBreed}`); + +console.log(`${dog.name} is a ${dog.breed}`); /* EXPECTED RESULT Spot is a Dalmatian -*/ \ No newline at end of file +*/ diff --git a/1-exercises/A-accessing-values/exercise2.js b/1-exercises/A-accessing-values/exercise2.js index 5b523ace..97f6c7a9 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -7,7 +7,7 @@ let capitalCities = { UnitedKingdom: "London", China: "Beijing", - Peru: "Lima" + Peru: "Lima", }; /* @@ -17,7 +17,7 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCapitalCity = capitalCities["UnitedKingdom"]; console.log(myCapitalCity); @@ -25,4 +25,4 @@ console.log(myCapitalCity); London -*/ \ No newline at end of file +*/ diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..a9f5573f 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -20,8 +20,9 @@ let basketballTeam = { - console.logs the name of each player on a new line */ -// write code here - +console.log(basketballTeam.topPlayers[2]); +console.log(basketballTeam.topPlayers[0]); +console.log(basketballTeam.topPlayers[1]); /* EXPECTED RESULT @@ -29,4 +30,4 @@ let basketballTeam = { Michael Jordan Scottie Pippen -*/ \ No newline at end of file +*/ diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..009b1b78 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -10,7 +10,7 @@ let capitalCities = { }, China: { name: "Beijing", - } + }, }; /* @@ -22,7 +22,11 @@ let capitalCities = { - Add a population of 9750000 to Peru's capital city. */ -// write code here +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCities.peru = {}; +capitalCities.peru.name = "Lima"; +capitalCities.peru.population = 9750000; console.log(capitalCities); @@ -34,4 +38,4 @@ console.log(capitalCities); Peru: { name: "Lima", population: 9750000 } } -*/ \ No newline at end of file +*/ diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..4710e548 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -6,7 +6,7 @@ let student = { name: "Reshma Saujani", examScore: 65, - hasPassed: false + hasPassed: false, }; /* @@ -15,7 +15,7 @@ let student = { - Set the value of attendance to 90 */ -// write code here +student["attendance"] = 90; /* - Write an "if" statement that changes the value of hasPassed to true @@ -25,8 +25,9 @@ let student = { - Use bracket notation to change the value of hasPassed */ -// write code here - +if (student.attendance >= 90 && student.examScore > 60) { + student["hasPassed"] = true; +} console.log(student); /* EXPECTED RESULT @@ -38,4 +39,4 @@ console.log(student); attendance: 90 } -*/ \ No newline at end of file +*/ diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..0e1702dc 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -12,17 +12,20 @@ let car = { brand: "Ford", yearsOld: 8, + // We do not have a property called colour to the car object ex.. colour:"red" }; console.log(car["colour"]); // Example 2 + function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); } +// We do not have a property called firstName to the user object we can chang it to console.log(`Hello ${user.name}`); let user = { - name: "Mira" + name: "Mira", }; sayHelloToUser(user); @@ -30,8 +33,9 @@ sayHelloToUser(user); // Example 3 let myPet = { animal: "Cat", - getName: function() { - "My pet's name is Fluffy"; + getName: function () { + return "My pet's name is Fluffy"; + // we do noy have return here. the answer should be return("My pet's name is Fluffy"); }, }; diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..dd95a5d3 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,8 +8,10 @@ */ let student = { - // write code here -} + getName: function (name) { + console.log(`student name: ${name}`); + }, +}; student.getName("Daniel"); @@ -17,4 +19,4 @@ student.getName("Daniel"); Student name: Daniel -*/ \ No newline at end of file +*/ From fe7e8c4cfdd673742d52961dd038dd64ea1f3a65 Mon Sep 17 00:00:00 2001 From: Muath Alawadhi <109815858+Muath-Alawadhi@users.noreply.github.com> Date: Thu, 30 Mar 2023 05:58:33 +0100 Subject: [PATCH 2/2] mandatory : Done --- 2-mandatory/1-recipes.js | 66 ++++++++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 8 ++- 2-mandatory/3-shopping-list.js | 33 ++++++++++--- 2-mandatory/4-restaurant.js | 15 +++++- 2-mandatory/5-writing-tests.js | 29 ++++++----- 2-mandatory/6-writing-tests-advanced.js | 49 +++++++++++++++++- 6 files changed, 177 insertions(+), 23 deletions(-) diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..ee954167 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,68 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +let favoriteRecipe = { + RecipeOne: { + title: "Rice", + Serves: 2, + Ingredients: ["water", "chicken", "oil", "salt", "tomato", "onion"], + }, + + RecipeTow: { + title: "PIZZAS", + Serves: 4, + Ingredients: ["flours", "salt", "oil", "Milk"], + }, + RecipeThree: { + title: "pie", + Serves: 6, + Ingredients: ["flours", "oil", "onions", "leaves"], + }, + RecipeFour: { + title: "tomato soup", + Serves: 8, + Ingredients: ["onion", "salt", "garlic", "chilli"], + }, + RecipeFive: { + title: "Pasta", + Serves: 10, + Ingredients: ["water", "crème", "oil", "black pepper"], + }, +}; +// 1 + +console.log(favoriteRecipe.RecipeOne.title); +console.log(`Serves:${favoriteRecipe.RecipeOne.Serves}`); +console.log("Ingredients:"); +favoriteRecipe.RecipeOne.Ingredients.forEach((a) => console.log(a)); + +// 2 +console.log("//////////////////////////////////"); +console.log(favoriteRecipe.RecipeTow.title); +console.log(`Serves:${favoriteRecipe.RecipeTow.Serves}`); +console.log("Ingredients:"); +favoriteRecipe.RecipeTow.Ingredients.forEach((a) => console.log(a)); + +// 3 +console.log("//////////////////////////////////"); +console.log(favoriteRecipe.RecipeThree.title); +console.log(`Serves:${favoriteRecipe.RecipeThree.Serves}`); +console.log("Ingredients:"); +favoriteRecipe.RecipeThree.Ingredients.forEach((a) => console.log(a)); + +// 4 +console.log("//////////////////////////////////"); +console.log(favoriteRecipe.RecipeFour.title); +console.log(`Serves:${favoriteRecipe.RecipeFour.Serves}`); +console.log("Ingredients:"); +favoriteRecipe.RecipeFour.Ingredients.forEach((a) => console.log(a)); + +// 5 +console.log("//////////////////////////////////"); +console.log(favoriteRecipe.RecipeFive.title); +console.log(`Serves:${favoriteRecipe.RecipeFive.Serves}`); + +console.log("Ingredients:"); +favoriteRecipe.RecipeFive.Ingredients.forEach((a) => console.log(a)); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..ea0277ba 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,6 +19,12 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here + return (countryCode = { + ["GB"]: "GBP", + ["DE"]: "EUR", + ["NG"]: "NGN", + ["MX"]: "MXN", + }); } /* ======= TESTS - DO NOT MODIFY ===== @@ -34,4 +40,4 @@ test("creates country currency code lookup", () => { NG: "NGN", MX: "MXN", }); -}); \ No newline at end of file +}); diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..601b2918 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,7 +19,21 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + let result = { + name: recipe.name, + items: [], + }; + + for (const ingredient of recipe.ingredients) { + if ( + !pantry.fridgeContents.includes(ingredient) && + !pantry.cupboardContents.includes(ingredient) + ) { + result.items.push(ingredient); + } + } + + return result; } /* ======= TESTS - DO NOT MODIFY ===== @@ -31,23 +45,30 @@ function createShoppingList(recipe) { test("createShoppingList works for pancakes recipe", () => { let recipe1 = { name: "pancakes", - ingredients: ["flour", "salt", "milk", "eggs", "vegetable oil"], + ingredients: ["flour", "salt", "milk", "mozarella", "vegetable oil"], }; expect(createShoppingList(recipe1)).toEqual({ name: "pancakes", - items: ["flour", "eggs", "vegetable oil"], + items: ["flour", "mozarella", "vegetable oil"], }); }); 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 +}); diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..0abc2193 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,8 +20,19 @@ const MENU = { }; let cashRegister = { - // write code here -} + orderBurger: function (balance) { + if (balance >= MENU.burger) { + balance = balance - MENU.burger; + } + return balance; + }, + orderFalafel: function (balance) { + if (balance >= MENU.falafel) { + balance = balance - MENU.falafel; + } + return balance; + }, +}; /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..66bf8610 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)).toEqual("A"); }); /* @@ -43,33 +43,40 @@ 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)).toEqual("B"); }); /* Write a test that checks a score of 68 is grade C */ +test("a test of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toEqual("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 test of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toEqual("D"); +}); /* - Write a test that checks a score of 55 is grade D -*/ -/* Write a test that checks a score of 49 is grade E */ - +test("a test of 49 is grade E ", () => { + expect(convertScoreToGrade(49)).toEqual("E"); +}); /* Write a test that checks a score of 30 is grade E */ +test("a test of 30 is grade E", function () { + expect(convertScoreToGrade(30)).toEqual("E"); +}); /* Write a test that checks a score of 70 is grade B */ +test("a test of 70 is grade B", () => { + expect(convertScoreToGrade(70)).toEqual("B"); +}); diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..89f26fd7 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -10,7 +10,7 @@ trainee has completed. */ -function convertScoreToGrade() { +function convertScoreToGrade(score) { let grade = null; if (score >= 80) { @@ -53,8 +53,18 @@ function formatCourseworkResult(trainee) { { name: "Xin", score: 63 + } */ +test("checks the output of formatCourseworkResult with score 63", () => { + let trainee = { + name: "Xin", + score: 63, + }; + expect(formatCourseworkResult(trainee)).toEqual( + "Xin's coursework was marked as grade C." + ); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -63,6 +73,15 @@ function formatCourseworkResult(trainee) { score: 78 } */ +test("checks the output of formatCourseworkResult with score 78", () => { + let trainee = { + name: "Mona", + score: 78, + }; + expect(formatCourseworkResult(trainee)).toEqual( + "Mona's coursework was marked as grade B." + ); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -73,6 +92,17 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ +test("checks the output of formatCourseworkResult with numbers", () => { + let trainee = { + name: "Ali", + score: 49, + age: 33, + subjects: ["JavaScript", "React", "CSS"], + }; + expect(formatCourseworkResult(trainee)).toEqual( + "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("checks the output of formatCourseworkResult with string", () => { + let trainee = { + score: 90, + age: 29, + }; + expect(formatCourseworkResult(trainee)).toEqual("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("checks the output of formatCourseworkResult with number", () => { + let trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"], + }; + expect(formatCourseworkResult(trainee)).toEqual( + "Error: Coursework percent is not a number!" + ); +});