From da0fb9d37c394c1dfeecefd8b4892a42a6182c5f Mon Sep 17 00:00:00 2001 From: elahemortazavi Date: Fri, 23 Dec 2022 23:19:32 +0000 Subject: [PATCH] exercise-mandatory js-score2 --- 1-exercises/A-accessing-values/exercise1.js | 12 ++++++------ 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 2 +- 1-exercises/B-setting-values/exercise1.js | 7 +++++++ 1-exercises/B-setting-values/exercise2.js | 3 ++- 1-exercises/C-undefined-properties/exercise.js | 5 ++++- 1-exercises/D-object-methods/exercise.js | 3 +++ 2-mandatory/1-recipes.js | 11 ++++++++++- 2-mandatory/2-currency-code-lookup.js | 9 ++++++++- 2-mandatory/3-shopping-list.js | 5 +++++ 2-mandatory/4-restaurant.js | 6 ++++++ 3-extra/1-count-words.js | 9 +++++++++ 12 files changed, 62 insertions(+), 12 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..1e500586 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..c7c8e188 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..609d0669 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().map(item => console.log(item)); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..3fd9b1ad 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -23,6 +23,13 @@ let capitalCities = { */ // write code here +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCities.Peru = { + name: "Lima", + population: 9750000, +} + console.log(capitalCities); diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..832435a4 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 @@ -26,6 +26,7 @@ let student = { */ // write code here +student["attendance"] >= 90 && student["examScore"] > 60 ? (student["hasPassed"] = true) : (student["hasPassed"] = false); console.log(student); diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..777ebc9a 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,7 +14,8 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]); +//because colour has not defined in this object. // Example 2 function sayHelloToUser(user) { @@ -26,6 +27,7 @@ let user = { }; sayHelloToUser(user); +//because firstName has not defined in this object. // Example 3 let myPet = { @@ -36,3 +38,4 @@ let myPet = { }; console.log(myPet.getName()); +// there is no return in the function. diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..8740c41d 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..296314ea 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,13 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +let cake = { + title: "carrotCake", + serves: 6, + Ingredients: ["egg", "milk", "flour", "carrot", "oil"] +} +console.log(cake['title']); +console.log(`serves: ${cake.serves}`); +console.log("ingredients:"); +(cake.Ingredients).map((item) => console.log(item)); \ 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..8dd9cbe5 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,7 +18,14 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + + let currencyCodes = {}; + for (let [x, y] of countryCurrencyCodes) { + currencyCodes[x] = y; + } + return currencyCodes; + + } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..4efbcc0b 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,6 +20,11 @@ let pantry = { function createShoppingList(recipe) { // write code here + let allContent = pantry.fridgeContents.concat(pantry.cupboardContents); + let missingIngredients = recipe.ingredients + .filter(ingredient => allContent.indexOf(ingredient) === -1); + return {name: recipe.name , + items: missingIngredients} } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..7d0335c6 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,6 +21,12 @@ const MENU = { let cashRegister = { // write code here + orderBurger : function (balance){ + return balance - MENU.burger >= 0 ? balance - MENU.burger: balance; + }, + orderFalafel : function(balance){ + return balance - MENU.falafel >= 0 ? balance - MENU.falafel : balance; + } } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..0ece0fd5 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -27,6 +27,15 @@ function countWords(string) { const wordCount = {}; // write code here + if (string.trim().length === 0) { + return wordCount; +} +string.split(" ").map((word) => { + if (wordCount[word] === undefined) { + wordCount[word] = 1; + } else { + wordCount[word]++; + }}); return wordCount; }