diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..486fd049 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}`); @@ -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..a0de1202 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,12 +17,12 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCapitalCity = capitalCities["UnitedKingdom"]; // complete the code console.log(myCapitalCity); - +// console.log(capitalCities["UnitedKingdom"]); /* EXPECTED RESULT 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..1b2187fb 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,12 +21,15 @@ let basketballTeam = { */ // write code here +let topPlayersNames = basketballTeam.topPlayers + .sort() + .forEach((player) => console.log(player)); - +// console.log(topPlayersNames); /* EXPECTED RESULT Dennis Rodman 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..8b99408e 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,7 +23,14 @@ let capitalCities = { */ // write code here - +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCities.Peru = { + name: "Lima", + population: 9750000, +}; +// capitalCities.Peru.name = 'Lima'; +// capitalCities.Peru.population = 9750000; console.log(capitalCities); /* EXPECTED RESULT @@ -34,4 +41,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..ca2c1473 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,7 +16,7 @@ let student = { */ // write code here - +student["attendance"] = 90; /* - Write an "if" statement that changes the value of hasPassed to true if the student has attendance that is equal or greater than 90 @@ -27,6 +27,9 @@ let student = { // write code here +if (student["attendance"] >= 90 && student["examScore"] > 60) { + student["hasPassed"] = true; +} console.log(student); /* EXPECTED RESULT @@ -38,4 +41,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..dd442585 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,7 +14,7 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]); // There is color property which is not assigned. // Example 2 function sayHelloToUser(user) { @@ -22,17 +22,17 @@ function sayHelloToUser(user) { } let user = { - name: "Mira" + name: "Mira", }; -sayHelloToUser(user); +sayHelloToUser(user); // The user object variable contains 'name,' while the function we are logging contains 'firstName.' // Example 3 let myPet = { animal: "Cat", - getName: function() { + getName: function () { "My pet's name is Fluffy"; }, }; -console.log(myPet.getName()); +console.log(myPet.getName()); // The 'return' statement is missing from the function. diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..c15250e0 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,7 +9,11 @@ let student = { // write code here -} + // name: 'Daniel', + getName: function (name) { + console.log(`${"Student name:"} ${name}`); + }, +}; student.getName("Daniel"); @@ -17,4 +21,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..39529bf2 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,66 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +let recipes = { + easyEggFriedRice: { + title: `Easy egg-fried rice`, + servings: 2, + ingerdiants: [ + "250g long grain rice", + "3 tbsp vegetable oil", + "1 onion, finely chopped", + "4 eggs, beaten", + "2 spring onion, sliced, to serve", + " olive oil", + ], + }, + smokedMackerelRisotto: { + title: `Smoked mackerel risotto`, + servings: 3, + ingerdiants: [ + "1 tbsp butter", + "1 onion, finely chopped", + "250 risotte rice", + "100ml white wine", + "1l begetable stock", + "1 x 240 pack smoked mackerel", + "2 spring onions, sliced", + "100g bag fresh spinach", + ], + }, + RoastLamb: { + title: `Roast lamb`, + servings: 8, + ingerdiants: [ + "carrots", + "onion", + "small bunch parsley", + "thyme leaf", + "lemon", + ], + }, + Scampi: { + title: `Scampi with tartare sauce`, + servings: 2, + ingerdiants: [ + "cornflour", + "sparkling water", + "beer", + "Dublin Bay prawn tails", + " olive oil", + ], + }, +}; +function printOut(meals) { + console.log(recipes[meals]["title"]); + console.log("Serves: " + recipes[meals]["servings"]); + console.log( + " Ingredients:" + recipes[meals]["ingerdiants"].map((x) => x + " \n ") + ); +} + +printOut("easyEggFriedRice"); +printOut("smokedMackerelRisotto"); +printOut("RoastLamb"); +printOut("Scampi"); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..4ad5c897 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,8 +18,14 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + let currencyObject = {}; + for (let currency of countryCurrencyCodes) { + currencyObject[currency[0]] = currency[1]; + } + return currencyObject; + // return Object.fromEntries(countryCurrencyCodes) } +// console.log(COUNTRY_CURRENCY_CODES) /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 2-currency-code-lookup.js` @@ -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..9a65513e 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,7 +19,24 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + let recipeName = recipe.name; + let recipeIngredients = recipe.ingredients; + + let emptyContainer = []; + + recipeIngredients.forEach((item) => { + if ( + !pantry.fridgeContents.includes(item) && + !pantry.cupboardContents.includes(item) + ) { + emptyContainer.push(item); + } + }); + + return { + name: recipeName, + items: emptyContainer, + }; } /* ======= TESTS - DO NOT MODIFY ===== @@ -43,11 +60,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..28159e40 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 -= MENU.burger; + } + return balance; + }, + 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` diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..1792ddcb 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -26,7 +26,15 @@ function countWords(string) { const wordCount = {}; - // write code here + if (string === "") { + return wordCount; + } else { + string.split(" ").forEach((word) => { + if (wordCount[word]) { + wordCount[word] = wordCount[word] + 1; + } else wordCount[word] = 1; + }); + } return wordCount; } @@ -46,9 +54,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 +68,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,