From a96d23e56ca03efae8799a84f7b331219bffdfba Mon Sep 17 00:00:00 2001 From: lexiisying Date: Sat, 8 Apr 2023 14:33:03 +0100 Subject: [PATCH 1/8] A - accessing values --- 1-exercises/A-accessing-values/exercise1.js | 4 ++-- 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 12 ++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..605b7fcb 100644 --- a/1-exercises/A-accessing-values/exercise1.js +++ b/1-exercises/A-accessing-values/exercise1.js @@ -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..c7c8e188 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[myCountry]; // 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..e5413803 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -22,6 +22,18 @@ let basketballTeam = { // write code here +// Access the top players array +let topPlayers = basketballTeam.topPlayers; + +// Sort the top players in alphabetical order +topPlayers.sort(); + +// Log the name of each player on a new line +console.log("Top Players:"); +topPlayers.forEach(player => { + console.log(player); +}); + /* EXPECTED RESULT From 82f496d1feea3aea2c9d2aaec13f87ced5d01d9c Mon Sep 17 00:00:00 2001 From: lexiisying Date: Sat, 8 Apr 2023 14:41:20 +0100 Subject: [PATCH 2/8] B - setting values --- 1-exercises/B-setting-values/exercise1.js | 11 +++++++++++ 1-exercises/B-setting-values/exercise2.js | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..5fae08c4 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -24,6 +24,17 @@ let capitalCities = { // write code here + +capitalCities.UnitedKingdom.population = 8980000; + +capitalCities.China.population = 21500000; + +capitalCities.Peru = {}; + +capitalCities.Peru.name = "Lima"; + +capitalCities.Peru.population = 9750000; + console.log(capitalCities); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..529381a3 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -27,6 +27,14 @@ let student = { // write code here +student['attendance'] = 90; + +if (student['attendance'] >= 90 && student['examScore'] > 60) { + student['hasPassed'] = true; +} + +console.log("Has Passed:", student['hasPassed']); + console.log(student); /* EXPECTED RESULT From e2f647e31760bad736ef85ca00496f4a961b8fb5 Mon Sep 17 00:00:00 2001 From: lexiisying Date: Sat, 8 Apr 2023 14:50:01 +0100 Subject: [PATCH 3/8] exercises C + D --- 1-exercises/C-undefined-properties/exercise.js | 3 +++ 1-exercises/D-object-methods/exercise.js | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..169456f6 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -15,6 +15,7 @@ let car = { }; console.log(car["colour"]); +// the "car" object does not have a property called "colour", so it returns undefined. // Example 2 function sayHelloToUser(user) { @@ -26,6 +27,7 @@ let user = { }; sayHelloToUser(user); +//the "user" object does not have a property called "firstName", as it only has a "name" property. // Example 3 let myPet = { @@ -36,3 +38,4 @@ let myPet = { }; console.log(myPet.getName()); +//the function inside "getName" does not have a return statement. diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..5fe2a903 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,7 +8,9 @@ */ let student = { - // write code here + getName: function(name) { + console.log("Student name: " + name); + } } student.getName("Daniel"); From 8323fae59eaa25afe6f6bbdb17a942b7ff9eb912 Mon Sep 17 00:00:00 2001 From: lexiisying Date: Sat, 8 Apr 2023 15:10:30 +0100 Subject: [PATCH 4/8] 1 - recipes & 2 - currency code lookup --- 2-mandatory/1-recipes.js | 72 ++++++++++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 14 +++++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..c4f9faf2 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,74 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +// Recipe 1 +let recipe1 = { + title: "Mole", + servings: 2, + ingredients: ["cinnamon", "cumin", "cocoa"] +}; + +console.log(recipe1.title); +console.log("Serves: " + recipe1.servings); +console.log("Ingredients:"); +for (let i = 0; i < recipe1.ingredients.length; i++) { + console.log(recipe1.ingredients[i]); +} + +// Recipe 2 +let recipe2 = { + title: "Pasta Carbonara", + servings: 4, + ingredients: ["spaghetti", "eggs", "bacon", "parmesan cheese", "black pepper"] +}; + +console.log(recipe2.title); +console.log("Serves: " + recipe2.servings); +console.log("Ingredients:"); +for (let i = 0; i < recipe2.ingredients.length; i++) { + console.log(recipe2.ingredients[i]); +} + +// Recipe 3 +let recipe3 = { + title: "Chicken Alfredo", + servings: 6, + ingredients: ["chicken breast", "butter", "garlic", "heavy cream", "parmesan cheese", "parsley"] +}; + +console.log(recipe3.title); +console.log("Serves: " + recipe3.servings); +console.log("Ingredients:"); +for (let i = 0; i < recipe3.ingredients.length; i++) { + console.log(recipe3.ingredients[i]); +} + +// Recipe 4 +let recipe4 = { + title: "Beef Stroganoff", + servings: 4, + ingredients: ["beef sirloin", "onion", "mushrooms", "sour cream", "beef broth", "flour"] +}; + +console.log(recipe4.title); +console.log("Serves: " + recipe4.servings); +console.log("Ingredients:"); +for (let i = 0; i < recipe4.ingredients.length; i++) { + console.log(recipe4.ingredients[i]); +} + +// Recipe 5 +let recipe5 = { + title: "Vegetable Stir-Fry", + servings: 3, + ingredients: ["bell peppers", "broccoli", "carrots", "onion", "garlic", "soy sauce"] +}; + +console.log(recipe5.title); +console.log("Serves: " + recipe5.servings); +console.log("Ingredients:"); +for (let i = 0; i < recipe5.ingredients.length; i++) { + console.log(recipe5.ingredients[i]); +} diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..5df27c6c 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,9 +18,21 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + let lookup = {}; + + for (let i = 0; i < countryCurrencyCodes.length; i++) { + let countryCode = countryCurrencyCodes[i][0]; + let currencyCode = countryCurrencyCodes[i][1]; + + lookup[countryCode] = currencyCode; + } + + return lookup; } +const countryCurrencyLookup = createLookup(COUNTRY_CURRENCY_CODES); +console.log(countryCurrencyLookup); + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 2-currency-code-lookup.js` - To run all exercises/tests in the mandatory folder, run `npm test` From f6f75a7b9bdff317083b6b51f0b49df2aedcea9b Mon Sep 17 00:00:00 2001 From: lexiisying Date: Sat, 8 Apr 2023 15:17:53 +0100 Subject: [PATCH 5/8] 3 - shopping list --- 2-mandatory/3-shopping-list.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..94a78b5f 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,8 +19,27 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + let missingIngredients = []; + for (let i = 0; i < recipe.ingredients.length; i++) { + let ingredient = recipe.ingredients[i]; + if (!pantry.fridgeContents.includes(ingredient) && !pantry.cupboardContents.includes(ingredient)) { + missingIngredients.push(ingredient); + } + } + let shoppingList = { + name: recipe.name, + items: missingIngredients + }; + return shoppingList; } +let recipe = { + name: "Spaghetti Bolognese", + ingredients: ["spaghetti", "minced beef", "onion", "garlic", "tomato sauce", "herbs", "cheese"] +}; + +let shoppingList = createShoppingList(recipe); +console.log(shoppingList); + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js` From 0294db0b7f006e2f721084f7c73043f7141729ad Mon Sep 17 00:00:00 2001 From: lexiisying Date: Sat, 8 Apr 2023 15:25:24 +0100 Subject: [PATCH 6/8] 4 - restaurant --- 2-mandatory/4-restaurant.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..e0752b45 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -13,15 +13,30 @@ Add another method to the cashRegister object which is called orderFalafel and handles ordering a falafel, in the same way as ordering a burger. */ - const MENU = { burger: 6.5, falafel: 7.25, }; let cashRegister = { - // write code here -} + // method to order a burger + orderBurger: function(balance) { + if (balance >= MENU.burger) { + balance -= MENU.burger; + } + return balance; + }, + + // method to order a falafel + orderFalafel: function(balance) { + if (balance >= MENU.falafel) { + balance -= MENU.falafel; + } + return balance; + } +}; + + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` From c426d8c8a00ee319d38a9c8f44b86102872cca06 Mon Sep 17 00:00:00 2001 From: lexiisying Date: Sat, 8 Apr 2023 15:38:27 +0100 Subject: [PATCH 7/8] extra - count words --- 3-extra/1-count-words.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..80c395a2 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -23,13 +23,41 @@ - Setting values on an object */ -function countWords(string) { +// function countWords(string) { +// const wordCount = {}; +// const words = string.toLowerCase().split(" "); +// words.forEach(word => { +// if (!wordCount.hasOwnProperty(word)) { +// wordCount[word] = 1; +// } else { +// wordCount[word] ++; +// } +// }); + +// return wordCount; +// } + +// below is the solution : + + function countWords(string) { const wordCount = {}; - // write code here + let arrayOfWords = string.split(" "); + + arrayOfWords.forEach((word) => { + if (word !== "") { + if (wordCount.hasOwnProperty(word)) { + wordCount[word] += 1; + } else { + wordCount[word] = 1; + } + } + }); return wordCount; } + + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm run extra-tests` From 704978ca35238aa5efa10ee8126e7e8c9dcba71d Mon Sep 17 00:00:00 2001 From: lexiisying Date: Sat, 8 Apr 2023 15:55:26 +0100 Subject: [PATCH 8/8] test --- 3-extra/1-count-words.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 80c395a2..753f5cf1 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -39,7 +39,7 @@ // below is the solution : - function countWords(string) { +function countWords(string) { const wordCount = {}; let arrayOfWords = string.split(" ");