From 2cd19523b3ee3d1e58ffeee5c7730b59cacc2e92 Mon Sep 17 00:00:00 2001 From: Luleka Mgaga Date: Fri, 23 Sep 2022 18:38:10 +0100 Subject: [PATCH] javaScript-exercise --- 1-exercises/A-accessing-values/exercise1.js | 4 +- 1-exercises/A-accessing-values/exercise2.js | 4 +- 1-exercises/A-accessing-values/exercise3.js | 1 + 1-exercises/B-setting-values/exercise1.js | 6 +++ 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 | 46 ++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 3 ++ 2-mandatory/3-shopping-list.js | 9 +++- 2-mandatory/4-restaurant.js | 12 +++++ 11 files changed, 90 insertions(+), 9 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..605b7fcb 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..d517638d 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -8,7 +8,7 @@ let capitalCities = { UnitedKingdom: "London", China: "Beijing", Peru: "Lima" -}; +}; /* You have an object, capitalCities, that contains key/value pairs of countries and their capital cities. @@ -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..bf87a163 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,6 +21,7 @@ let basketballTeam = { */ // write code here +basketballTeam.topPlayers.sort().map(x => console.log(x)) /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..e8364768 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -23,7 +23,13 @@ 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..6b690227 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -16,6 +16,7 @@ let student = { */ // write code here +student['attendance'] = 90 /* - Write an "if" statement that changes the value of hasPassed to true @@ -27,6 +28,10 @@ let student = { // write code here +if (student.attendance >= 90 && student.examScore > 60) { + student['hasPassed'] = true +} + 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..ed826ebe 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"]); //colour does not exist in the object // Example 2 function sayHelloToUser(user) { @@ -22,7 +22,7 @@ function sayHelloToUser(user) { } let user = { - name: "Mira" + name: "Mira" //firstName does not exist in user object }; sayHelloToUser(user); @@ -31,7 +31,7 @@ sayHelloToUser(user); let myPet = { animal: "Cat", getName: function() { - "My pet's name is Fluffy"; + "My pet's name is Fluffy"; //function is not returning any value }, }; diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..01039b48 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..858a5f05 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,48 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +let vicSponge = { + title: 'vicSponge', + servings: 5, + ingredients: ['sugar', 'eggs', 'flour'] +}; +console.log(vicSponge.title) +console.log('serves: ' + vicSponge.servings) +vicSponge.ingredients.forEach(x => console.log(x)) + +let lemonCake = { + title: 'lemonCake', + servings: 5, + ingredients: ['sugar', 'eggs', 'flour'] +}; +console.log(lemonCake.title) +console.log('serves: ' + lemonCake.servings) +lemonCake.ingredients.forEach(x => console.log(x)) + +let fruitCake = { + title: 'fruitCake', + servings: 5, + ingredients: ['sugar', 'eggs', 'flour'] +}; +console.log(fruitCake.title) +console.log('serves: ' + fruitCake.servings) +fruitCake.ingredients.forEach(x => console.log(x)) + +let carrotCake = { + title: 'cake', + servings: 5, + ingredients: ['sugar', 'eggs', 'flour'] +}; +console.log(carrotCake.title) +console.log('serves: ' + carrotCake.servings) +carrotCake.ingredients.forEach(x => console.log(x)) + +let bananaLoaf = { + title: 'bananaLoaf', + servings: 5, + ingredients: ['sugar', 'eggs', 'flour'] +}; +console.log(bananaLoaf.title) +console.log('serves: ' + bananaLoaf.servings) +bananaLoaf.ingredients.forEach(x => console.log(x)) \ 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..d08a806c 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,6 +19,9 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here + let currencyCode = {} + countryCurrencyCodes.map(x => currencyCode[x[0]] = x[1]) + return currencyCode } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..62bc5398 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -10,7 +10,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 + - "items", which is an array of the missing ingredients that need to be on the shopping list */ let pantry = { @@ -20,6 +20,13 @@ let pantry = { function createShoppingList(recipe) { // write code here + let allContent = pantry.fridgeContents.concat(pantry.cupboardContents) + let shopItems = recipe.ingredients.filter(x => allContent.indexOf(x) < 0) + + return { + name: recipe.name, + items: shopItems + } } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..fa0c0346 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,6 +21,18 @@ const MENU = { let cashRegister = { // write code here + orderBurger: function(balance){ + if(balance - MENU.burger >= 0){ + return balance - MENU.burger + } + return balance + }, + orderFalafel: function(balance){ + if(balance - MENU.falafel >= 0){ + return balance - MENU.falafel + } + return balance + } } /* ======= TESTS - DO NOT MODIFY =====