From 7111d2cc1309fe1bbfd65e4701229dc33e116e15 Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Sun, 2 Oct 2022 18:32:44 +0100 Subject: [PATCH 1/3] Complete the exercises --- 1-exercises/A-accessing-values/exercise1.js | 12 ++++++------ 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 2 ++ 1-exercises/B-setting-values/exercise1.js | 5 +++++ 1-exercises/B-setting-values/exercise2.js | 5 +++++ 1-exercises/C-undefined-properties/exercise.js | 6 ++++++ 1-exercises/D-object-methods/exercise.js | 3 +++ 7 files changed, 28 insertions(+), 7 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..1e500586 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,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}`); diff --git a/1-exercises/A-accessing-values/exercise2.js b/1-exercises/A-accessing-values/exercise2.js index 5b523ace..a18919e5 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -17,7 +17,7 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCapitalCity = capitalCities["UnitedKingdom"]; // complete the code console.log(myCapitalCity); diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..cb5843c7 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,7 +21,9 @@ let basketballTeam = { */ // write code here +let topPlayersArr = basketballTeam.topPlayers.sort(); +topPlayersArr.forEach(player => console. log(player)); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..3126fe62 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -23,6 +23,11 @@ let capitalCities = { */ // write code here +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCities.Peru = {}; +capitalCities.Peru.name = "Lima"; +capitalCities.Peru.population = 975000; console.log(capitalCities); diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..849a9c05 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -16,6 +16,11 @@ let student = { */ // write code here +student["attendence"] = 90; + +if (student["attendence"] >= 90 && student["examScore"] > 60) { + student["hasPassed"] = true; +} /* - Write an "if" statement that changes the value of hasPassed to true diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..64e89a45 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -16,6 +16,8 @@ let car = { console.log(car["colour"]); +// Because there's no such a property named colour in the object car. + // Example 2 function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); @@ -27,6 +29,8 @@ let user = { sayHelloToUser(user); +// Because there's no such a property called firstName in the object user. + // Example 3 let myPet = { animal: "Cat", @@ -36,3 +40,5 @@ let myPet = { }; console.log(myPet.getName()); + +// Because in the object method, it omits the keyword return for the string. diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..3b312056 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,6 +9,9 @@ let student = { // write code here + getName : function (name) { + console.log("Student name: " + name); + } } student.getName("Daniel"); From 3565cb26b45cde1cf8c408372f6bee90290dbf3a Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Sun, 2 Oct 2022 22:35:58 +0100 Subject: [PATCH 2/3] Complete the mandatory --- 2-mandatory/1-recipes.js | 47 ++++++++++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 5 +++ 2-mandatory/3-shopping-list.js | 21 ++++++++++-- 2-mandatory/4-restaurant.js | 19 ++++++++++- 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..e592971b 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,49 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +let chicken = { + title:"chicken", + serves:4, + ingredients: ["chicken","potatoes"] +} + +let pork = { + title:"pork", + serves:4, + ingredients: ["pork","tomtoes"] +} + +let steak = { + title:"steak", + serves:2, + ingredients: ["beef","pepper"] +} + +let lamb = { + title:"lamb", + serves:3, + ingredients: ["lamb","garlic"] +} + +let fish = { + title:"fish", + serves:6, + ingredients: ["fish","onion"] +} +console.log(Object.entries(fish)); + +function printRecipe(obj) { + for ([key, value] of Object.entries(obj)) { + console.log(`${key}: ${value}`); + } +} + +printRecipe(chicken); +printRecipe(pork); +printRecipe(steak); +printRecipe(lamb); +printRecipe(fish); + + diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..72bf5874 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,6 +19,11 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here + let result = {}; + for (let [country, currency] of countryCurrencyCodes) { + result[country] = currency; + } + return result; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..1170bc5d 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,6 +20,14 @@ let pantry = { function createShoppingList(recipe) { // write code here + let result = {}; + result.name = recipe["name"]; + let pantryArr = pantry.fridgeContents.concat(pantry.cupboardContents); + let missingArr = recipe["ingredients"].filter( + (food) => pantryArr.indexOf(food) === -1 + ); + result.items = missingArr; + return result; } /* ======= TESTS - DO NOT MODIFY ===== @@ -43,11 +51,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 +}); diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..32997805 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,7 +21,24 @@ const MENU = { let cashRegister = { // write code here -} + orderBurger: function (balance) { + if (balance >= MENU.burger) { + let newBalance = balance - MENU.burger; + return newBalance; + } else { + return balance; + } + }, + orderFalafel: function (balance) { + if (balance >= MENU.falafel) { + let newBalance = balance - MENU.falafel; + return newBalance; + } else { + return balance; + } + }, +}; + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` From 5610ffd185be9cea444fc28be8e9aea61b6dd066 Mon Sep 17 00:00:00 2001 From: Doris-Siu Date: Sun, 2 Oct 2022 23:26:00 +0100 Subject: [PATCH 3/3] Complete the extra --- 3-extra/1-count-words.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..4d0fd395 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -25,10 +25,21 @@ function countWords(string) { const wordCount = {}; - + if (!string || string.trim().length === 0) { + return wordCount; + } + let strArr = string.split(" "); + strArr.map((word) => { + if (wordCount[word] === undefined) { + wordCount[word] = 1; + } else { + wordCount[word] = wordCount[word] + 1; + } + }); // write code here return wordCount; + } /* ======= TESTS - DO NOT MODIFY ===== @@ -46,9 +57,13 @@ test("Code works for a small string", () => { }); test("A string with, some punctuation", () => { - expect(countWords("A string with, some punctuation")).toEqual( - { A: 1, string: 1, "with,": 1, some: 1, punctuation: 1 } - ); + expect(countWords("A string with, some punctuation")).toEqual({ + A: 1, + string: 1, + "with,": 1, + some: 1, + punctuation: 1, + }); }); test("Empty string", () => { @@ -56,7 +71,11 @@ test("Empty string", () => { }); test("Example task string", () => { - expect(countWords("you're braver than you believe, stronger than you seem, and smarter than you think")).toEqual({ + expect( + countWords( + "you're braver than you believe, stronger than you seem, and smarter than you think" + ) + ).toEqual({ "you're": 1, and: 1, "believe,": 1,