From dc58a583ddbe2217a32206334940080e592b36bd Mon Sep 17 00:00:00 2001 From: Yesna-Omar <109865751+Yesna-Omar@users.noreply.github.com> Date: Thu, 6 Apr 2023 07:36:02 +0100 Subject: [PATCH] core 2 week 1 --- 1-exercises/A-accessing-values/exercise1.js | 8 ++--- 1-exercises/A-accessing-values/exercise2.js | 6 ++-- 1-exercises/A-accessing-values/exercise3.js | 5 +-- 1-exercises/B-setting-values/exercise1.js | 7 ++-- 1-exercises/B-setting-values/exercise2.js | 8 +++-- .../C-undefined-properties/exercise.js | 10 ++++-- 1-exercises/D-object-methods/exercise.js | 7 ++-- 2-mandatory/1-recipes.js | 30 +++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 11 +++++- 2-mandatory/3-shopping-list.js | 18 ++++++++++ 2-mandatory/4-restaurant.js | 20 +++++++++-- 2-mandatory/5-writing-tests.js | 32 +++++++++++------ 2-mandatory/6-writing-tests-advanced.js | 35 +++++++++++++++++++ 13 files changed, 165 insertions(+), 32 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..dcf5a5ce 100644 --- a/1-exercises/A-accessing-values/exercise1.js +++ b/1-exercises/A-accessing-values/exercise1.js @@ -8,7 +8,7 @@ let dog = { breed: "Dalmatian", name: "Spot", isHungry: true, - happiness: 6 + happiness: 6, }; /* @@ -16,8 +16,8 @@ let dog = { Log the name and breed of this dog using dot notation. */ -let dogName; // complete the code -let dogBreed; // complete the code +let dogName = dog.name; // complete the code +let dogBreed = dog.breed; // complete the code console.log(`${dogName} is a ${dogBreed}`); @@ -25,4 +25,4 @@ console.log(`${dogName} is a ${dogBreed}`); 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..02189d2b 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[myCountry]; // complete the code 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..3462a9b7 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,7 +21,8 @@ let basketballTeam = { */ // write code here - +const basketballPlayers = basketballTeam.topPlayers.sort(); +console.log(basketballPlayers); /* 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..5cd60c5e 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", - } + }, }; /* @@ -23,6 +23,9 @@ let capitalCities = { */ // write code here +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCities.Peru = { name: "Lima", population: "9750000" }; console.log(capitalCities); @@ -34,4 +37,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..322c7eaa 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, }; /* @@ -16,6 +16,7 @@ let student = { */ // write code here +student[`attendance`] = 90; /* - Write an "if" statement that changes the value of hasPassed to true @@ -26,6 +27,9 @@ let student = { */ // write code here +if (student.attendance >= 90 && student.examScore > 60) { + student.hasPassed = true; +} console.log(student); @@ -38,4 +42,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..8df0f38d 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -16,23 +16,29 @@ let car = { console.log(car["colour"]); +//There is no (car : color) as an object properties. + // Example 2 function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); } let user = { - name: "Mira" + name: "Mira", }; sayHelloToUser(user); +//The object properties is name not firstName. + // Example 3 let myPet = { animal: "Cat", - getName: function() { + getName: function () { "My pet's name is Fluffy"; }, }; console.log(myPet.getName()); + +// the getName function does not have return. diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..fa03d48e 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,7 +9,10 @@ let student = { // write code here -} + getName: function (name) { + console.log(`Student name: ${name}`); + }, +}; student.getName("Daniel"); @@ -17,4 +20,4 @@ student.getName("Daniel"); Student name: Daniel -*/ \ No newline at end of file +*/ diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..86f3569a 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -21,5 +21,33 @@ You should write and log at least 5 recipes */ +// write code here -// write code here \ No newline at end of file +let recipes = [ + { title: "Mole", serves: 2, ingredients: ["cinnamon", "cumin", "cocoa"] }, + { title: "Rice", serves: 5, ingredients: ["rice", "water", "oil"] }, + { + title: "pizza", + serves: 3, + ingredients: ["flour", "onions", "meat", "garlic"], + }, + { + title: "curry", + serves: 6, + ingredients: ["chicken", "onions", "mushrooms"], + }, + { + title: "soup", + serves: 1, + ingredients: ["water", "flour", "peas", "mushroom"], + }, +]; +for (let i = 0; i < recipes.length; i++) { + console.log(recipes[i].title); + console.log("Serves: ", recipes[i].serves); + console.log("Ingredients:"); + for (let j = 0; j < recipes[i].ingredients.length; j++) { + console.log(recipes[i].ingredients[j]); + } + console.log(" "); +} diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..82ca3ea3 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 + let currencyObject = {}; + + for (let aCountryCurrency of countryCurrencyCodes) { + const singleCountry = aCountryCurrency[0]; + const singleCurrency = aCountryCurrency[1]; + currencyObject[singleCountry] = singleCurrency; + } + + return currencyObject; } /* ======= TESTS - DO NOT MODIFY ===== @@ -34,4 +43,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..f5cfd8ac 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,6 +20,24 @@ let pantry = { function createShoppingList(recipe) { // write code here + let shoppingList = {}; + let theRecipeName = recipe.name; + let shoppingIngredients = []; + + for (let ingredient of recipe.ingredients) { + let fridgeArray = pantry.fridgeContents; + let cupboardArray = pantry.cupboardContents; + if ( + fridgeArray.includes(ingredient) || + cupboardArray.includes(ingredient) + ) { + } else { + shoppingIngredients.push(ingredient); + } + } + shoppingList["name"] = theRecipeName; + shoppingList["items"] = shoppingIngredients; + return shoppingList; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..09cfb4a7 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -19,10 +19,24 @@ const MENU = { falafel: 7.25, }; -let cashRegister = { - // write code here -} + +let cashRegister = { + orderBurger: function (balance) { + if (balance >= MENU.burger) { + return balance - MENU.burger; + } else { + return balance; + } + }, + orderFalafel: function (balance) { + if (balance >= MENU.falafel) { + return balance - MENU.falafel; + } else { + return balance; + } + }, +}; /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` - To run all exercises/tests in the mandatory folder, run `npm test` diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..10ee1073 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,45 @@ test("a score of 83 is grade A", () => { write a matching test */ -test.skip("a score of 71 is grade B", () => { - /* Remove the .skip above, then write the test body. */ +test("a score of 71 is grade B", () => { + expect(convertScoreToGrade(71)).toEqual("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)).toEqual("C"); +}); /* Write a test that checks a score of 55 is grade D */ +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toEqual("D"); +}); + /* Write a test that checks a score of 49 is grade E */ +test("a score of 49 is grade E", () => { + expect(convertScoreToGrade(49)).toEqual("E"); +}); + /* Write a test that checks a score of 30 is grade E */ +test("a score of 30 is grade E", () => { + expect(convertScoreToGrade(30)).toEqual("E"); +}); + /* Write a test that checks a score of 70 is grade B */ + +test("a score 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..23098865 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -55,6 +55,12 @@ function formatCourseworkResult(trainee) { score: 63 } */ +test("Xin's grade is C ", () => { + let trainee = { + name: "Xin", + score: 63, + }; +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -64,6 +70,13 @@ function formatCourseworkResult(trainee) { } */ +test("Mona's grade is B ", () => { + let trainee = { + name: "Mona", + score: 78, + }; +}); + /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -74,6 +87,15 @@ function formatCourseworkResult(trainee) { } */ +test("Ali's grade is E", () => { + let trainee = { + name: "Ali", + score: 49, + age: 33, + subjects: ["JavaScript", "React", "CSS"], + }; +}); + /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -82,6 +104,13 @@ function formatCourseworkResult(trainee) { } */ +test("There is no trainee name , the grade is A", () => { + let trainee = { + score: 90, + age: 29, + }; +}); + /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -89,3 +118,9 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("Aman ,there is no trainee score", () => { + let trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"], + }; +});