From cc5adedc6927d92ca8833a878de021105fb6e272 Mon Sep 17 00:00:00 2001 From: Ahmad Omid Omari <105078583+OmidOmar@users.noreply.github.com> Date: Sat, 10 Sep 2022 22:56:38 +0100 Subject: [PATCH 1/5] exercises 1 done --- 1-exercises/A-accessing-values/exercise1.js | 4 ++-- 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 4 ++-- 1-exercises/B-setting-values/exercise1.js | 8 +++++--- 1-exercises/B-setting-values/exercise2.js | 7 +++++-- 1-exercises/C-undefined-properties/exercise.js | 8 ++++---- 1-exercises/D-object-methods/exercise.js | 3 +++ 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..e052a65b 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..88c9d042 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..f950f316 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -12,6 +12,7 @@ let basketballTeam = { address: "1901 W Madison St", }, }; +basketballTeam.topPlayers.sort().forEach(x =>{console.log(x)}); /* Write code that @@ -22,11 +23,10 @@ let basketballTeam = { // write code here - /* 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..90a7344a 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -10,9 +10,8 @@ let capitalCities = { }, China: { name: "Beijing", - } + }, }; - /* Using dot notation: - Change the value of UnitedKingdom's capital city population to 8980000. @@ -23,6 +22,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 +36,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..3ba09ac7 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,8 @@ let student = { */ // write code here +student.hasPassed = + student.attendance >= 90 && student.examScore > 60 ? true : false; console.log(student); @@ -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..08104110 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,15 +14,15 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]); // color property not defined in car object. // Example 2 function sayHelloToUser(user) { - console.log(`Hello ${user.firstName}`); + console.log(`Hello ${user.firstName}`); //firstName property is not defined in user object instead we need to use name property } let user = { - name: "Mira" + name: "Mira", }; sayHelloToUser(user); @@ -30,7 +30,7 @@ sayHelloToUser(user); // Example 3 let myPet = { animal: "Cat", - getName: function() { + getName: function () {// return keyword is missing "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..e7bf3896 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 d6fbbc298b16dc9c73c8427421bbec5b0e48adf9 Mon Sep 17 00:00:00 2001 From: Ahmad Omid Omari <105078583+OmidOmar@users.noreply.github.com> Date: Sat, 10 Sep 2022 22:57:35 +0100 Subject: [PATCH 2/5] exercies 1 done --- 1-exercises/D-object-methods/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index e7bf3896..bd4b74a7 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,7 +8,7 @@ */ let student = { - // write code here + // write code here` getName: function(name){ console.log(`Student name: ${name}`) } From ce6545e1badd5cd6bc1867c1f045129acf7cc3be Mon Sep 17 00:00:00 2001 From: Ahmad Omid Omari <105078583+OmidOmar@users.noreply.github.com> Date: Mon, 12 Sep 2022 00:56:38 +0100 Subject: [PATCH 3/5] extra exercise done --- 3-extra/1-count-words.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..8079ad20 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -27,7 +27,23 @@ function countWords(string) { const wordCount = {}; // write code here + const punctuation = [",", ".", "'", '"', ":"]; + if (string !== "") { + let words = string.split(" "); + for (let i = 0; i <= words.length; i++) { + i = 0; + let count = 0; + let word = words[i]; + for (let j = 0; j < words.length; j++) { + if (word === words[j]) count++; + } + punctuation.some((p) => word.includes(p)) + ? (wordCount[`${word}`] = count) + : (wordCount[word] = count); + while (words.indexOf(word) !== -1) words.splice(words.indexOf(word), 1); + } + } return wordCount; } @@ -46,9 +62,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 +76,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, From 0fe21b80c9620380c01537c18ea8ddcede56161a Mon Sep 17 00:00:00 2001 From: Ahmad Omid Omari <105078583+OmidOmar@users.noreply.github.com> Date: Thu, 15 Sep 2022 18:52:35 +0100 Subject: [PATCH 4/5] shoppling list exercie done --- 2-mandatory/3-shopping-list.js | 35 ++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..d9b4b273 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,8 +19,28 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + // write code here` + let res = { + name: recipe.name, + items: recipe.ingredients.filter( + (x) => !pantry.fridgeContents.concat(pantry.cupboardContents).includes(x) + ), + }; + return res; } +console.log( + createShoppingList({ + name: "margherita pizza", + ingredients: [ + "flour", + "salt", + "yeast", + "tinned tomatoes", + "oregano", + "mozarella", + ], + }) +); /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js` @@ -43,11 +63,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 +}); From adb06f8a4818c80b40adb1ff703db1ca5f3d2b65 Mon Sep 17 00:00:00 2001 From: Ahmad Omid Omari <105078583+OmidOmar@users.noreply.github.com> Date: Thu, 15 Sep 2022 21:53:58 +0100 Subject: [PATCH 5/5] restaurant exercise completed --- 2-mandatory/1-recipes.js | 40 ++++++++++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 9 ++++-- 2-mandatory/3-shopping-list.js | 3 +- 2-mandatory/4-restaurant.js | 9 +++++- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..5a7d7e35 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,42 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + + +let favoriteRecipe = { + recipe1: { + title: "Mole", + servings: 2, + ingredients: ["cinnamon", "cumin", "cocoa"], + }, + recipe2: { + title: "Pizza Margherita XL", + servings: 4, + ingredients: ["strong bread flour", "yeast", "salt", "olive", "tomato"], + }, + recipe3: { + title: "Pancake", + servings: 3, + ingredients: ["egg", "milk", "flour", "butter", "salt", "baking powder"], + }, + recipe4: { + title: "Mango Lassi", + servings: 2, + ingredients: ["mango", "plain yogurt", "milk", "honey", "cardamom", "ice"], + }, + recipe5: { + title: "Kebab", + servings: 1, + ingredients: ["lamb", "tomato", "salt", "lime", "garlic"], + }, + display: function (recipe) { + let res = `${this[recipe].title}\nServes: ${this[recipe].servings}\nIngredients:\n`; + for (let i = 0; i < this[recipe].ingredients.length; i++) + res += this[recipe].ingredients[i] + "\n"; + return res; + }, +}; + +for (let i = 1; i < Object.keys(favoriteRecipe).length; i++) + console.log(favoriteRecipe.display("recipe" + i)); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..cc259463 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -16,10 +16,15 @@ const COUNTRY_CURRENCY_CODES = [ ["NG", "NGN"], ["MX", "MXN"], ]; - function createLookup(countryCurrencyCodes) { // write code here + let res = {}; + for (let i = 0; i < countryCurrencyCodes.length; i++) + res[countryCurrencyCodes[i][0]] = countryCurrencyCodes[i][1]; + + return res; } +console.log(createLookup(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 +39,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 d9b4b273..a951fa8b 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,13 +20,12 @@ let pantry = { function createShoppingList(recipe) { // write code here` - let res = { + return { name: recipe.name, items: recipe.ingredients.filter( (x) => !pantry.fridgeContents.concat(pantry.cupboardContents).includes(x) ), }; - return res; } console.log( createShoppingList({ diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..ec03d3fd 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,7 +21,14 @@ const MENU = { let cashRegister = { // write code here -} + orderBurger: function (balance) { + return balance >= MENU.burger ? balance - MENU.burger : balance; + }, + orderFalafel: function (balance) { + return balance >= MENU.falafel ? balance - MENU.falafel : balance;`` + }, +}; +console.log(cashRegister.orderBurger(6.4)); /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js`