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..cb66fc91 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,7 +21,7 @@ let basketballTeam = { */ // write code here - +basketballTeam["topPlayers"].sort().forEach((x) => console.log(x)); /* EXPECTED RESULT @@ -29,4 +29,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..2c149def 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", - } + }, }; /* @@ -24,6 +24,9 @@ let capitalCities = { // write code here +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCities.Peru = { name: "Lima", population: 9750000 }; console.log(capitalCities); /* EXPECTED RESULT @@ -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..c60acd50 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,10 @@ let student = { */ // write code here +student["attendance"] = 90; +student["attendance"] >= 90 && student["examScore"] > 60 + ? (student["hasPassed"] = true) + : (student["hasPassed"] = false); /* - Write an "if" statement that changes the value of hasPassed to true @@ -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..e5ff20f7 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"]); +//there is no key name of "colour" in the variable name car. // Example 2 function sayHelloToUser(user) { @@ -22,17 +23,19 @@ function sayHelloToUser(user) { } let user = { - name: "Mira" + name: "Mira", }; sayHelloToUser(user); +///there is no key name of "firstName" in the variable name user. // Example 3 let myPet = { animal: "Cat", - getName: function() { + getName: function () { "My pet's name is Fluffy"; }, }; console.log(myPet.getName()); +//it has to insert "return" on the "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..6cee0e6d 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: (string) => { + console.log(`Student name:${string}`); + }, +}; 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..44e5dc63 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,79 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +const recipe1 = { + title: "Mole1", + Serves: 2, + Ingredients: ["cinnamon1", "cumin1", "cocoa1"], +}; +const recipe2 = { + title: "Mole2", + Serves: 2, + Ingredients: ["cinnamon2", "cumin2", "cocoa2"], +}; +const recipe3 = { + title: "Mole3", + Serves: 2, + Ingredients: ["cinnamon3", "cumin3", "cocoa3"], +}; +const recipe4 = { + title: "Mole4", + Serves: 2, + Ingredients: ["cinnamon4", "cumin4", "cocoa4"], +}; +const recipe5 = { + title: "Mole5", + Serves: 2, + Ingredients: ["cinnamon5", "cumin5", "cocoa5"], +}; +const recipeArr = [recipe1, recipe2, recipe3, recipe4, recipe5]; +for (let i = 0; i < recipeArr.length; i++) { + for (const property in recipeArr[i]) { + if (property === "title") { + console.log(`${recipeArr[i][property]}`); + } else if (property === "Serves") { + console.log(`${property}:${recipeArr[i][property]}`); + } else if (property === "Ingredients") { + console.log(`${property}:`); + Array.from(recipeArr[i][property]).forEach((x) => console.log(x)); + console.log(`----------------------`); + } + } +} +// Result: +// Mole1 +// Serves:2 +// Ingredients: +// cinnamon1 +// cumin1 +// cocoa1 +// ---------------------- +// Mole2 +// Serves:2 +// Ingredients: +// cinnamon2 +// cumin2 +// cocoa2 +// ---------------------- +// Mole3 +// Serves:2 +// Ingredients: +// cinnamon3 +// cumin3 +// cocoa3 +// ---------------------- +// Mole4 +// Serves:2 +// Ingredients: +// cinnamon4 +// cumin4 +// cocoa4 +// ---------------------- +// Mole5 +// Serves:2 +// Ingredients: +// cinnamon5 +// cumin5 +// cocoa5 +// ---------------------- \ No newline at end of file diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..31fd442f 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -17,10 +17,19 @@ const COUNTRY_CURRENCY_CODES = [ ["MX", "MXN"], ]; +// Object.fromEntries way: function createLookup(countryCurrencyCodes) { - // write code here + return Object.fromEntries(countryCurrencyCodes); } +//---another way if like "reduce" method---: +// function createLookup(countryCurrencyCodes) { +// return countryCurrencyCodes.reduce((acc, cur) => { +// acc[cur[0]] = cur[1]; +// return acc; +// }, {}); +// } + /* ======= 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` @@ -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..717befec 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,7 +19,16 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + const resultObj = {}; + resultObj.name = recipe.name; + resultObj.items = []; + const pantryAllList = Object.values(pantry).flat(); + + for (let i = 0; i < recipe.ingredients.length; i++) { + if (!pantryAllList.includes(recipe.ingredients[i])) + resultObj.items.push(recipe.ingredients[i]); + } + return resultObj; } /* ======= TESTS - DO NOT MODIFY ===== @@ -43,11 +52,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..ad93bdf0 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,8 +20,13 @@ const MENU = { }; let cashRegister = { - // write code here -} + orderBurger: (balance) => { + return balance >= MENU.burger ? balance - MENU.burger : balance; + }, + orderFalafel: (balance) => { + return balance >= MENU.falafel ? balance - MENU.falafel : 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..3574dff0 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -24,9 +24,19 @@ */ function countWords(string) { - const wordCount = {}; - - // write code here + let wordCount = {}; + let arr = []; + if (string.length > 0) { + arr = string.split(" "); + } + wordCount = arr.reduce((acc, cur) => { + if (cur in acc) { + acc[cur]++; + } else { + acc[cur] = 1; + } + return acc; + }, {}); return wordCount; } @@ -46,9 +56,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 +70,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,