From 5dfda83ea1c99edd809a10facc6cef5b6f469efe Mon Sep 17 00:00:00 2001 From: Sana Asaf Date: Sat, 31 Dec 2022 16:45:36 +0000 Subject: [PATCH] sss --- 1-exercises/A-accessing-values/exercise1.js | 4 +- 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 3 +- 1-exercises/B-setting-values/exercise1.js | 4 +- 1-exercises/B-setting-values/exercise2.js | 5 +- .../C-undefined-properties/exercise.js | 6 +-- 1-exercises/D-object-methods/exercise.js | 3 ++ 2-mandatory/1-recipes.js | 51 ++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 7 ++- 2-mandatory/3-shopping-list.js | 13 ++++- 2-mandatory/4-restaurant.js | 13 ++++- 3-extra/1-count-words.js | 10 ++++ 12 files changed, 105 insertions(+), 16 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..694330ac 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..a18919e5 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..1dbcff9f 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -19,7 +19,8 @@ let basketballTeam = { - sorts the top players in alphabetical order - console.logs the name of each player on a new line */ - +let array=basketballTeam.topPlayers; +console.log(array.sort()); // write code here diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..963a224b 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -23,7 +23,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 diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..9a6ac07c 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -15,7 +15,7 @@ let student = { - Set the value of attendance to 90 */ -// write code here +student["attendance"]=90; /* - Write an "if" statement that changes the value of hasPassed to true @@ -24,8 +24,9 @@ let student = { exam score is above 60. - Use bracket notation to change the value of hasPassed */ +if (student["examScore"]>60 && student["attendance"]>=90) +student["hasPassed"] = true ; -// write code here console.log(student); diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..b3e5af04 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,11 +14,11 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]);// undefined because color property has not been defined in object specification // Example 2 function sayHelloToUser(user) { - console.log(`Hello ${user.firstName}`); + console.log(`Hello ${user.firstName}`);//undefined because firstName property has not been defined in object specification } let user = { @@ -35,4 +35,4 @@ let myPet = { }, }; -console.log(myPet.getName()); +console.log(myPet.getName());//undefined because the function getName() does not return value and hence undefined will be returned diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..f7fc45b5 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"); diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..b133288e 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -20,6 +20,55 @@ cocoa You should write and log at least 5 recipes + + */ -// write code here \ No newline at end of file +let recipe = { + title :"Mole", + servings : 2 , + ingredients : [ + "cinnamon" , "cumin" , "cocoa" + ], + displayRecipe : function (){ + console.log(this.title); + console.log("Serves : "+ this.servings); + console.log("Ingredients:"); + for (item of this.ingredients){ + console.log(item); + } + } +} +let recipe1 = { + title :"Biryani", + servings : 2 , + ingredients : [ + "rice" , "spices" , "meat" + ], + displayRecipe : function (){ + console.log(this.title); + console.log("Serves : "+ this.servings); + console.log("Ingredients:"); + for (item of this.ingredients){ + console.log(item); + } + } +} +let recipe2 = { + title :"Anda Paratha", + servings : 2 , + ingredients : [ + "eggs" , "spices" , "atta" , "oil" + ], + displayRecipe : function (){ + console.log(this.title); + console.log("Serves : "+ this.servings); + console.log("Ingredients:"); + for (item of this.ingredients){ + console.log(item); + } + } +} +recipe.displayRecipe(); +recipe1.displayRecipe(); +recipe2.displayRecipe(); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..16e97399 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,8 +18,13 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + let codesObject = {}; + countryCurrencyCodes.forEach(element => { + codesObject[element[0]] = element[1]; + }); + return codesObject; } +createLookup(COUNTRY_CURRENCY_CODES); /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 2-currency-code-lookup.js` diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..6139a773 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -11,6 +11,7 @@ The createShoppingList function should return an object with two properties: - "name" of the recipe, which is a string, - "items", which is an arry of the missing ingredients that need to be on the shopping list + */ let pantry = { @@ -19,7 +20,14 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + let shoppingList = {}; + shoppingList.name = recipe.name; + shoppingList.items = []; + recipe.ingredients.map(item => { + if (!pantry.fridgeContents.includes(item) && !pantry.cupboardContents.includes(item)) + shoppingList.items.push(item); + }); + return shoppingList; } /* ======= TESTS - DO NOT MODIFY ===== @@ -50,4 +58,5 @@ test("createShoppingList works for margherita pizza recipe", () => { name: "margherita pizza", 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..7a56b7b4 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -12,6 +12,7 @@ - the method should return the new balance 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 = { @@ -20,8 +21,16 @@ const MENU = { }; let cashRegister = { - // write code here -} + + orderBurger: function (balance) { + if (balance >= MENU.burger) balance = balance-MENU.burger; + return balance; + }, + orderFalafel: function(balance){ + if (balance >= MENU.falafel) balance = 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..e7513598 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -21,6 +21,16 @@ - Loops or forEach - Comparison inside if statements - Setting values on an object + if (string === "") { + return wordCount; + } + string.split(" ").forEach((word) => { + if (wordCount[word] >= 1) { + wordCount[word]++; + } else { + wordCount[word] = 1; + } + }); */ function countWords(string) {