From dedd92458af53ff59621cdd045e1bdaff68713ca Mon Sep 17 00:00:00 2001 From: clav00cl <105980474+clav00cl@users.noreply.github.com> Date: Wed, 5 Oct 2022 18:16:18 +0100 Subject: [PATCH] JavaScript-Core-2-Week 1 --- 1-exercises/A-accessing-values/exercise1.js | 4 +- 1-exercises/A-accessing-values/exercise2.js | 4 +- 1-exercises/A-accessing-values/exercise3.js | 4 +- 1-exercises/B-setting-values/exercise1.js | 5 ++ 1-exercises/B-setting-values/exercise2.js | 65 +++++++-------- .../C-undefined-properties/exercise.js | 10 ++- 1-exercises/D-object-methods/exercise.js | 5 +- 2-mandatory/1-recipes.js | 83 ++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 8 +- 2-mandatory/3-shopping-list.js | 14 +++- 2-mandatory/4-restaurant.js | 19 ++++- 3-extra/1-count-words.js | 16 +++- 12 files changed, 185 insertions(+), 52 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..bd089fbf 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -17,7 +17,9 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +// let myCapitalCity=capitalCities["UnitedKingdom"]; // complete the code +//or + let myCapitalCity=capitalCities[myCountry] console.log(myCapitalCity); diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..44fa067b 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,7 +21,9 @@ let basketballTeam = { */ // write code here - +console.log(basketballTeam.topPlayers); +let topPlayersArraySorted=basketballTeam.topPlayers.sort(); +console.log(`${topPlayersArraySorted.map(el=>el+ "\n")}`); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..2409daf7 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -23,6 +23,11 @@ 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); diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..939c02a2 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -1,41 +1,40 @@ /* - You can add properties to objects and assign values to them using bracket notation. - You can also change the value assigned to a property using bracket notation. -*/ + You have already seen "undefined", either in an error message or being output from your program. -let student = { - name: "Reshma Saujani", - examScore: 65, - hasPassed: false -}; + As a reminder, in some cases, undefined will be used by a programmer intentionally, and they will write code to handle it. + But usually, when you see undefined - it means something has gone wrong! -/* - Using bracket notation - - Add a property to the student object for attendance - - Set the value of attendance to 90 + Below are some examples of when you would see undefined when using objects. + For each example, can you explain why we are seeing undefined? */ -// write code here - -/* - - Write an "if" statement that changes the value of hasPassed to true - if the student has attendance that is equal or greater than 90 - AND - exam score is above 60. - - Use bracket notation to change the value of hasPassed -*/ - -// write code here - -console.log(student); +// Example 1 +let car = { + brand: "Ford", + yearsOld: 8, + colour: blue, +}; -/* EXPECTED RESULT +console.log(car["colour"]); +// WE HAVE NOT COLOUR PROPERTY - { - name: "Reshma Saujani", - examScore: 65, - hasPassed: true, - attendance: 90 - } +// Example 2 +function sayHelloToUser(user) { + console.log(`Hello ${user.firstName}`); +} -*/ \ No newline at end of file +let user = { + firstName: "Mira", +}; +// IT HAS A NAME PROPERTY NOT FIRST NAME +sayHelloToUser(user); + +// Example 3 +let myPet = { + animal: "Cat", + getName: function () { + "My pet's name is Fluffy"; + }, +}; +console.log(myPet.getName()); +// does not return anything \ 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..939c02a2 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -12,9 +12,11 @@ let car = { brand: "Ford", yearsOld: 8, + colour: blue, }; console.log(car["colour"]); +// WE HAVE NOT COLOUR PROPERTY // Example 2 function sayHelloToUser(user) { @@ -22,17 +24,17 @@ function sayHelloToUser(user) { } let user = { - name: "Mira" + firstName: "Mira", }; - +// IT HAS A NAME PROPERTY NOT FIRST NAME sayHelloToUser(user); // Example 3 let myPet = { animal: "Cat", - getName: function() { + getName: function () { "My pet's name is Fluffy"; }, }; - console.log(myPet.getName()); +// does not return anything \ No newline at end of file diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..45a215c0 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,7 +9,10 @@ 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..4a7fd491 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -10,6 +10,7 @@ - servings (a number), and - ingredients (an array of strings) + On separate lines (one console.log statement for each), log the recipe information so it looks like: Mole @@ -21,5 +22,85 @@ You should write and log at least 5 recipes */ +// write code here +let myFavoriteRecipe1 = { + tittle: "Mirzaghasemi", + serves: 4, + ingredients: ["tomato", "garlic", "eggplant"], +}; +console.log(myFavoriteRecipe1.tittle); +console.log("serves:" + myFavoriteRecipe1.serves); +console.log( + "ingredients:" + + "\n" + + myFavoriteRecipe1.ingredients[0] + + "\n" + + myFavoriteRecipe1.ingredients[1] + + "\n" + + myFavoriteRecipe1.ingredients[2] +); -// write code here \ No newline at end of file +let myFavoriteRecipe2 = { + tittle: "Sholezard", + serves: 4, + ingredients: ["rice", "zafron", "rosesyrup"], +}; +console.log(myFavoriteRecipe2.tittle); +console.log("serves:" + myFavoriteRecipe2.serves); +console.log( + "ingredients:" + + "\n" + + myFavoriteRecipe2.ingredients[0] + + "\n" + + myFavoriteRecipe2.ingredients[1] + + "\n" + + myFavoriteRecipe2.ingredients[2] +); +let myFavoriteRecipe3 = { + tittle: "chicken soup", + serves: 3, + ingredients: ["chicken", "vegtables", "carot", "barley"], +}; +console.log(myFavoriteRecipe3.tittle); +console.log("serves:" + myFavoriteRecipe3.serves); +console.log( + "ingredients:" + + "\n" + + myFavoriteRecipe3.ingredients[0] + + "\n" + + myFavoriteRecipe3.ingredients[1] + + "\n" + + myFavoriteRecipe3.ingredients[2] +); +let myFavoriteRecipe4 = { + tittle: "choclate cake", + serves: 4, + ingredients: ["eggs", "flour", "choclate", "sugar"], +}; +console.log(myFavoriteRecipe4.tittle); +console.log("serves:" + myFavoriteRecipe4.serves); +console.log( + "ingredients:" + + "\n" + + myFavoriteRecipe4.ingredients[0] + + "\n" + + myFavoriteRecipe4.ingredients[1] + + "\n" + + myFavoriteRecipe4.ingredients[2] +); +let myFavoriteRecipe5 = { + tittle: "ash", + serves: 4, + ingredients: ["vegtables", "lentil", "meat", "ugurt"], +}; +console.log(myFavoriteRecipe5.tittle); +console.log("serves:" + myFavoriteRecipe5.serves); +console.log( + "ingredients:" + + "\n" + + myFavoriteRecipe5.ingredients[0] + + "\n" + + myFavoriteRecipe5.ingredients[1] + + "\n" + + myFavoriteRecipe5.ingredients[2] +); \ 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..0933e0da 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -17,9 +17,15 @@ const COUNTRY_CURRENCY_CODES = [ ["MX", "MXN"], ]; -function createLookup(countryCurrencyCodes) { +function createLookup(countryCurrencyCodes) {// or const obj = Object.fromEntries(COUNTRY_CURRENCY_CODES) // write code here +const newObject={}; +for(let x of countryCurrencyCodes){ + newObject[x[0]]=x[1]; + } + return newObject; } +// or const obj = Object.fromEntries(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..1311a7da 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -18,9 +18,19 @@ let pantry = { cupboardContents: ["salt", "tinned tomatoes", "oregano"], }; +const allOfThem=[] + for (let x of pantry.fridgeContents){ + allOfThem.push(x) + } + for(let x of pantry.cupboardContents){ + allOfThem.push(x) + } function createShoppingList(recipe) { - // write code here -} + let newObject={}; + newObject.name=recipe.name; + newObject.items=recipe["ingredients"].filter(e=>!allOfThem.includes(e)); + return newObject; + }; /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js` diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..62dde9cc 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,8 +20,21 @@ const MENU = { }; let cashRegister = { - // write code here -} + orderBurger:function(balance){ + if( balance>=MENU.burger){ + const newBalance=balance-MENU.burger; + return newBalance + }else return balance; + }, + orderFalafel:function(balance){ + if( balance>=MENU.falafel){ + const newBalance=balance-MENU.falafel; + return newBalance; + }else return balance; + } + } + + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` @@ -47,4 +60,4 @@ test("orderBurger will not subtract from balance if balance is too low", () => { test("orderFalafel will not subtract from balance if balance is too low", () => { let balance = 7.24; expect(cashRegister.orderFalafel(balance)).toEqual(7.24); -}); +}); \ No newline at end of file diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..b3e00726 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -24,10 +24,20 @@ */ function countWords(string) { + if (string===""){ + return {}; + } const wordCount = {}; - + string.split(" ").forEach(word=>{ + if (wordCount[word] === undefined ) { + wordCount[word] = 1; + } else { + wordCount[word]++; + } + }); +// for (let i = 0; i < split.length; i++) { +// } // write code here - return wordCount; } @@ -68,4 +78,4 @@ test("Example task string", () => { think: 1, you: 3, }); -}); +}); \ No newline at end of file