diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..3a083099 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..0227ddba 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -17,10 +17,12 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +//let myCapitalCity = capitalCities.UnitedKingdom; +let myCapitalCity = capitalCities[myCountry]; // complete the code console.log(myCapitalCity); + /* EXPECTED RESULT London diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..03b07d62 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -12,16 +12,15 @@ let basketballTeam = { address: "1901 W Madison St", }, }; - /* Write code that - accesses the basketball team's top players array - sorts the top players in alphabetical order - console.logs the name of each player on a new line */ - // write code here - +let basketballTeamsTopPlayers = basketballTeam.topPlayers.sort().join("\n"); +console.log(basketballTeamsTopPlayers); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..14cbe3c5 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -12,18 +12,20 @@ let capitalCities = { name: "Beijing", } }; - /* Using dot notation: - Change the value of UnitedKingdom's capital city population to 8980000. - - Add the property for population to China's capital city and set the value to 21500000. + - Add the property for population to China's capital city and + set the value to 21 500 000. - Add the country "Peru" to capitalCities object. - Add a name of "Lima" to Peru's capital city. - Add a population of 9750000 to Peru's capital city. */ - // write code here - +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +let peru = {name: "Lima", population: 9750000}; + capitalCities.Peru = peru; 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..383be573 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 @@ -24,18 +24,17 @@ let student = { exam score is above 60. - Use bracket notation to change the value of hasPassed */ - // write code here - +if (student["attendance"] >= 90 && student["examScore"] > 60) { + student["hasPassed"] = true; +} console.log(student); /* EXPECTED RESULT - { name: "Reshma Saujani", examScore: 65, hasPassed: true, 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..d34e5395 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -13,26 +13,25 @@ let car = { brand: "Ford", yearsOld: 8, }; - console.log(car["colour"]); +// In object car there is no property - "colour" // Example 2 function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); } - let user = { name: "Mira" }; - sayHelloToUser(user); +// the property of object is only one and called "name", not "firstName" // Example 3 let myPet = { animal: "Cat", getName: function() { - "My pet's name is Fluffy"; + "My pet's name is Fluffy"; }, }; - console.log(myPet.getName()); +// the getName is not a function , getName is property of object myPet diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..59f1df14 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,8 +9,10 @@ let student = { // write code here -} - + getName: function (name) { + console.log(`Student name: ${name}`); + } +}; student.getName("Daniel"); /* EXPECTED RESULT diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..eb01713f 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,68 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +console.log("\ ") +// Recipe 1 Borshch +let recipe1 = { + title: "Borsch", + servings: 75, + ingredients: [ + "Cabbage -5kg", + "Potato -5kg", + "Carrot -3kg", + "Onion -1kg", + "Beetroot -1kg", + "Bean-2kg", + "Tomatoes-2kg", + "Vegetable oil - 200g", + ] +}; +console.log("Recipe 1", " ", recipe1.title); +console.log("Servings: ", recipe1.servings); +console.log("Ingredients:", "\n", recipe1.ingredients.join("\n")); + console.log(`\ `); + +// Recipe 2 Pasta +let recipe2 = { + title: "Pasta", + servings: 2, + ingredients: ["Pasta -300g", "Butter -20g","Water -1000g"] +}; +console.log("Recipe 2", " ", recipe2.title); +console.log("Servings: ", recipe2.servings); +console.log("Ingredients:", "\n", recipe2.ingredients.join("\n")); + console.log(`\ `); + +// Recipe 3 Cake Kyiv +let recipe3 = { + title: "Cake Kyiv", + servings: 5, + ingredients: ["Nuts-100g", "Eggs-3", "Butter-20g", "Flour-500g", "Sugar-10g", "Milk-200g"], +}; +console.log("Recipe 3", " ", recipe3.title); +console.log("Servings: ",recipe3.servings); +console.log("Ingredients:", "\n", recipe3.ingredients.join("\n")); + console.log(`\ `); + + // Recipe 4 Potato + let recipe4 = { + title: "Potato", + servings: 15, + ingredients: ["Potato -5kg", "Butter -500g", "Water -10l"] + }; + console.log("Recipe 4", " ", recipe4.title); + console.log("Servings: ", recipe4.servings); + console.log("Ingredients:", "\n", recipe4.ingredients.join("\n")); + console.log(`\ `); + + // Recipe 5 + let recipe5 = { + title: "Latte", + servings: 75, + ingredients: ["Coffee -10kg", "Milk -5kg", "Water -10l"] + }; + console.log("Recipe 5", " ", recipe5.title); + console.log("Servings: ", recipe5.servings); + console.log("Ingredients:", "\n", recipe5.ingredients.join("\n")); + console.log(`\ `); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..a0d4f86f 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,6 +19,8 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here + return Object.fromEntries(countryCurrencyCodes); + // The Object.fromEntries() static method transforms a list of key-value pairs into an object. } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..0eb1a0b2 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,6 +20,15 @@ let pantry = { function createShoppingList(recipe) { // write code here + return { + name: recipe.name, + items: recipe.ingredients.filter( + (element) => + pantry.fridgeContents.indexOf(element) === -1 && + pantry.cupboardContents.indexOf(element) === -1 + ), + }; + // -- } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..239eebc9 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,7 +21,20 @@ const MENU = { let cashRegister = { // write code here -} + orderBurger: function (balance) { + if (balance - MENU.burger >= 0) { + balance -= MENU.burger; + } + return balance; + }, + orderFalafel: function (balance) { + if (balance - MENU.falafel >= 0) { + 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..152e42a6 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -27,7 +27,16 @@ function countWords(string) { const wordCount = {}; // write code here - + if (string.length) { + string + .split(" ") + .forEach((element) => + wordCount.hasOwnProperty(element) + ? (wordCount[element] += 1) + : (wordCount[element] = 1) + ); + } + // -- return wordCount; }