From f40bc57fd7817d32e5b33677e4683eaf24ba9102 Mon Sep 17 00:00:00 2001 From: Dawit-Dev <100426510+Dawit-Dev@users.noreply.github.com> Date: Sun, 11 Sep 2022 22:12:33 +0100 Subject: [PATCH 1/4] Completed Exercise --- 1-exercises/A-accessing-values/exercise1.js | 12 ++++++------ 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 11 +++++++---- 1-exercises/B-setting-values/exercise1.js | 9 ++++++++- 1-exercises/B-setting-values/exercise2.js | 4 +++- 1-exercises/C-undefined-properties/exercise.js | 6 +++--- 1-exercises/D-object-methods/exercise.js | 5 +++++ 2-mandatory/2-currency-code-lookup.js | 3 +++ 8 files changed, 36 insertions(+), 16 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..bd509eb5 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..e3467a9b 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..29ba6b04 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 - - -/* EXPECTED RESULT +let topPlayersNames = basketballTeam.topPlayers.sort() +topPlayersNames.forEach((player) => console.log(player)) + +// console.log(topPlayersNames); + /* EXPECTED RESULT Dennis Rodman Michael Jordan Scottie Pippen -*/ \ No newline at end of file +*/ + \ 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..61777f64 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -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 diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..56f02116 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -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,8 @@ let student = { // write code here +// let studentAttExam = ((student) => student.attendance >= 90 && student.examScore >= 60 ? true : false) + let properties = Object.keys(student) console.log(student); /* EXPECTED RESULT diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..bbaa6e0f 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) { @@ -25,7 +25,7 @@ let user = { name: "Mira" }; -sayHelloToUser(user); +sayHelloToUser(user);// The user object variable contains 'name,' while the function we are logging contains 'firstName.' // Example 3 let myPet = { @@ -35,4 +35,4 @@ let myPet = { }, }; -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..f38bf16f 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,6 +9,11 @@ let student = { // write code here + name: 'Daniel', + getName: function () { + console.log(`${"Student name:"} ${this.name}`); + } + } student.getName("Daniel"); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..316016b8 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,7 +19,10 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here + // return `${key} ${COUNTRY_CURRENCY_CODES[key]}.`; + 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` From 873f88470f5cb65b91642d7f45a61ce5efc77cad Mon Sep 17 00:00:00 2001 From: Dawit-Dev <100426510+Dawit-Dev@users.noreply.github.com> Date: Mon, 12 Sep 2022 20:00:49 +0100 Subject: [PATCH 2/4] Completed mandatory task --- 1-exercises/B-setting-values/exercise2.js | 5 +- 2-mandatory/1-recipes.js | 64 ++++++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 9 ++-- 2-mandatory/3-shopping-list.js | 20 ++++++- 2-mandatory/4-restaurant.js | 13 ++++- 5 files changed, 103 insertions(+), 8 deletions(-) diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 56f02116..87787abb 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -27,8 +27,9 @@ student['attendance'] = 90; // write code here -// let studentAttExam = ((student) => student.attendance >= 90 && student.examScore >= 60 ? true : false) - let properties = Object.keys(student) +if (student.attendance >= 90 && student.examScore > 60) { + student.hasPassed = true +} console.log(student); /* EXPECTED RESULT diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..997e4609 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 316016b8..6d66ef59 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,9 +18,12 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here - // return `${key} ${COUNTRY_CURRENCY_CODES[key]}.`; - return Object.fromEntries(countryCurrencyCodes) + let currencyObject = {} + for (let currency of countryCurrencyCodes) { + currencyObject[currency[0]] = currency[1] + } + return currencyObject + // return Object.fromEntries(countryCurrencyCodes) } // console.log(COUNTRY_CURRENCY_CODES) diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..b99b2eb3 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,7 +19,25 @@ 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 ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..d20e97cb 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,7 +20,18 @@ 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 ===== From 8964a707b37f1fcce2af680e8f2e91848698a053 Mon Sep 17 00:00:00 2001 From: Dawit Abraha Date: Wed, 14 Sep 2022 23:26:19 +0100 Subject: [PATCH 3/4] Formatted and Corrected some of the task --- 1-exercises/A-accessing-values/exercise1.js | 4 +- 1-exercises/A-accessing-values/exercise2.js | 8 +- 1-exercises/A-accessing-values/exercise3.js | 10 +- 1-exercises/B-setting-values/exercise1.js | 8 +- 1-exercises/B-setting-values/exercise2.js | 10 +- .../C-undefined-properties/exercise.js | 8 +- 1-exercises/D-object-methods/exercise.js | 13 +- 2-mandatory/1-recipes.js | 122 +++++++++--------- 2-mandatory/2-currency-code-lookup.js | 10 +- 2-mandatory/3-shopping-list.js | 48 ++++--- 2-mandatory/4-restaurant.js | 12 +- 11 files changed, 129 insertions(+), 124 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index bd509eb5..486fd049 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", // String name: "Spot", // String isHungry: true, // Boolean - happiness: 6 // Number + happiness: 6, // Number }; /* @@ -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 e3467a9b..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 = capitalCities['UnitedKingdom']; // 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 29ba6b04..1b2187fb 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,15 +21,15 @@ let basketballTeam = { */ // write code here -let topPlayersNames = basketballTeam.topPlayers.sort() -topPlayersNames.forEach((player) => console.log(player)) - +let topPlayersNames = basketballTeam.topPlayers + .sort() + .forEach((player) => console.log(player)); + // console.log(topPlayersNames); - /* EXPECTED RESULT +/* 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 61777f64..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", - } + }, }; /* @@ -26,9 +26,9 @@ let capitalCities = { capitalCities.UnitedKingdom.population = 8980000; capitalCities.China.population = 21500000; capitalCities.Peru = { - name: 'Lima', + name: "Lima", population: 9750000, -} +}; // capitalCities.Peru.name = 'Lima'; // capitalCities.Peru.population = 9750000; console.log(capitalCities); @@ -41,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 87787abb..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; +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,8 +27,8 @@ student['attendance'] = 90; // write code here -if (student.attendance >= 90 && student.examScore > 60) { - student.hasPassed = true +if (student["attendance"] >= 90 && student["examScore"] > 60) { + student["hasPassed"] = true; } console.log(student); @@ -41,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 bbaa6e0f..dd442585 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -22,17 +22,17 @@ function sayHelloToUser(user) { } let user = { - name: "Mira" + name: "Mira", }; -sayHelloToUser(user);// The user object variable contains 'name,' while the function we are logging contains 'firstName.' +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());// The 'return' statement is missing from the function. +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 f38bf16f..c15250e0 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,12 +9,11 @@ let student = { // write code here - name: 'Daniel', - getName: function () { - console.log(`${"Student name:"} ${this.name}`); - } - -} + // name: 'Daniel', + getName: function (name) { + console.log(`${"Student name:"} ${name}`); + }, +}; student.getName("Daniel"); @@ -22,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 997e4609..39529bf2 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -23,65 +23,65 @@ */ // 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 ") - ); - } +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"); +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 6d66ef59..4ad5c897 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,14 +18,14 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - let currencyObject = {} + let currencyObject = {}; for (let currency of countryCurrencyCodes) { - currencyObject[currency[0]] = currency[1] + currencyObject[currency[0]] = currency[1]; } - return currencyObject + return currencyObject; // return Object.fromEntries(countryCurrencyCodes) } - // console.log(COUNTRY_CURRENCY_CODES) +// 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` @@ -40,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 b99b2eb3..9a65513e 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,25 +19,24 @@ let pantry = { }; function createShoppingList(recipe) { - 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) - } + let recipeName = recipe.name; + let recipeIngredients = recipe.ingredients; - }); - - return { - name: recipeName, - items: emptyContainer - } + 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 ===== @@ -61,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 d20e97cb..28159e40 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -22,17 +22,17 @@ const MENU = { let cashRegister = { orderBurger: function (balance) { if (balance >= MENU.burger) { - balance -= MENU.burger + balance -= MENU.burger; } - return balance + return balance; }, orderFalafel: function (balance) { if (balance >= MENU.falafel) { - balance -= MENU.falafel + balance -= MENU.falafel; } - return balance - } -} + return balance; + }, +}; /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` From 707af03ed123c15bbb058adada88fb3a019a13b4 Mon Sep 17 00:00:00 2001 From: Dawit Abraha Date: Fri, 16 Sep 2022 23:40:47 +0100 Subject: [PATCH 4/4] Completed Extra task --- 3-extra/1-count-words.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) 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,