diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..dcf5a5ce 100644 --- a/1-exercises/A-accessing-values/exercise1.js +++ b/1-exercises/A-accessing-values/exercise1.js @@ -8,7 +8,7 @@ let dog = { breed: "Dalmatian", name: "Spot", isHungry: true, - happiness: 6 + happiness: 6, }; /* @@ -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}`); @@ -25,4 +25,4 @@ console.log(`${dogName} is a ${dogBreed}`); Spot is a Dalmatian -*/ \ No newline at end of file +*/ diff --git a/1-exercises/A-accessing-values/exercise2.js b/1-exercises/A-accessing-values/exercise2.js index 5b523ace..02189d2b 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -7,7 +7,7 @@ let capitalCities = { UnitedKingdom: "London", China: "Beijing", - Peru: "Lima" + Peru: "Lima", }; /* @@ -17,7 +17,7 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCapitalCity = capitalCities[myCountry]; // complete the code console.log(myCapitalCity); @@ -25,4 +25,4 @@ console.log(myCapitalCity); London -*/ \ No newline at end of file +*/ diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..0f9991b4 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -22,6 +22,10 @@ let basketballTeam = { // write code here +let sortedNames = basketballTeam.topPlayers.sort(); +sortedNames.forEach((name) => { + console.log(name); +}); /* EXPECTED RESULT @@ -29,4 +33,4 @@ let basketballTeam = { Michael Jordan Scottie Pippen -*/ \ No newline at end of file +*/ diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..1a9f5914 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -10,7 +10,7 @@ let capitalCities = { }, China: { name: "Beijing", - } + }, }; /* @@ -24,6 +24,16 @@ let capitalCities = { // write code here +capitalCities.UnitedKingdom.population = 8980000; + +capitalCities.China.population = 21500000; + +capitalCities.Peru = {}; + +capitalCities.Peru.name = "Lima"; + +capitalCities.Peru.population = 9750000; + console.log(capitalCities); /* EXPECTED RESULT @@ -34,4 +44,4 @@ console.log(capitalCities); Peru: { name: "Lima", population: 9750000 } } -*/ \ No newline at end of file +*/ diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..36abda05 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -6,7 +6,7 @@ let student = { name: "Reshma Saujani", examScore: 65, - hasPassed: false + hasPassed: false, }; /* @@ -17,6 +17,8 @@ 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 @@ -27,6 +29,12 @@ let student = { // write code here +// let passing = hasPassed; + +if (student["attendance"] >= 90 && student["examScore"] > 60) { + student.hasPassed = true; +} + console.log(student); /* EXPECTED RESULT @@ -38,4 +46,4 @@ console.log(student); 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..a3bc2f18 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -8,7 +8,7 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1 - colour has not been defined so nothing logs out let car = { brand: "Ford", yearsOld: 8, @@ -16,21 +16,21 @@ let car = { console.log(car["colour"]); -// Example 2 +// Example 2 - firstName has not been defined, only name has function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); } let user = { - name: "Mira" + name: "Mira", }; sayHelloToUser(user); -// Example 3 +// Example 3 - there is no return so calling the function doesnt return anything let myPet = { animal: "Cat", - getName: function() { + getName: function () { "My pet's name is Fluffy"; }, }; diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..fa03d48e 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"); @@ -17,4 +20,4 @@ student.getName("Daniel"); Student name: Daniel -*/ \ No newline at end of file +*/ diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..554750b3 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,70 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +let myRecipe = { + recipe1: { + title: "Cheesecake", + serving: 12, + ingredients: [ + "biscuits", + "cream cheese", + "double cream", + "sugar", + "vanilla essence", + ], + }, + + recipe2: { + title: "Brownies", + serving: 10, + ingredients: ["cocoa powder", "butter", "eggs", "sugar"], + }, + + recipe3: { + title: "Cupcakes", + serving: 10, + ingredients: ["sugar", "flour", "butter", "eggs"], + }, + + recipe4: { + title: "Eggnog", + serving: 5, + ingredients: ["eggs", "milk", "vanilla essence", "sugar"], + }, + + recipe5: { + title: "Cookies", + serving: 20, + ingredients: ["flour", "eggs", "sugar", "butter"], + }, +}; + +// let recipe0 = { +// title: "Cheesecake", +// serving: 12, +// ingredients: [ +// "biscuits", +// "cream cheese", +// "double cream", +// "sugar", +// "vanilla essence", +// ], +// }; + +// for (const key in myRecipe.recipe1) { +// console.log(`${key}: ${myRecipe.recipe1[key]}`); +// } + +function allRecipes(eachRecipe) { + for (key in eachRecipe) { + console.log(`${key}: ${eachRecipe[key]}`); + } +} + +allRecipes(myRecipe.recipe1); +allRecipes(myRecipe.recipe2); +allRecipes(myRecipe.recipe3); +allRecipes(myRecipe.recipe4); +allRecipes(myRecipe.recipe5); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..1daa3b9e 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,9 +18,12 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + const countryCurrency = Object.fromEntries(countryCurrencyCodes); + return countryCurrency; } +//console.log(createLookup(COUNTRY_CURRENCY_CODES)); + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 2-currency-code-lookup.js` - To run all exercises/tests in the mandatory folder, run `npm test` @@ -34,4 +37,4 @@ test("creates country currency code lookup", () => { NG: "NGN", MX: "MXN", }); -}); \ No newline at end of file +}); diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..0538103a 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,13 +20,35 @@ let pantry = { function createShoppingList(recipe) { // write code here + + const newShoppingList = {}; + newShoppingList.name = recipe.name; + newShoppingList.items = []; + + const pantryList = Object.values(pantry).flat(); + + for (eachIngredient of recipe.ingredients) { + if (!pantryList.includes(eachIngredient)) { + newShoppingList.items.push(eachIngredient); + } + } + return newShoppingList; + + // const pantryList = Object.values(pantry).flat(); + + // for (eachIngredient in recipe.ingredients) { + // newShoppingList.items = recipe.ingredients.filter( + // (eachIngredient) => !pantryList.includes(eachIngredient) + // ); + // } + // return newShoppingList; } -/* ======= TESTS - DO NOT MODIFY ===== -- To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js` -- To run all exercises/tests in the mandatory folder, run `npm test` -- (Reminder: You must have run `npm install` one time before this will work!) -*/ +// /* ======= TESTS - DO NOT MODIFY ===== +// - To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js` +// - To run all exercises/tests in the mandatory folder, run `npm test` +// - (Reminder: You must have run `npm install` one time before this will work!) +// */ test("createShoppingList works for pancakes recipe", () => { let recipe1 = { @@ -43,11 +65,18 @@ test("createShoppingList works for pancakes recipe", () => { test("createShoppingList works for margherita pizza recipe", () => { let recipe2 = { name: "margherita pizza", - ingredients: ["flour", "salt", "yeast", "tinned tomatoes", "oregano", "mozarella"], + ingredients: [ + "flour", + "salt", + "yeast", + "tinned tomatoes", + "oregano", + "mozarella", + ], }; expect(createShoppingList(recipe2)).toEqual({ name: "margherita pizza", - items: ["flour", "yeast", "mozarella"] + 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..c547ae98 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,7 +21,24 @@ const MENU = { let cashRegister = { // write code here -} + orderBurger: function (balance) { + if (balance >= MENU.burger) { + let newBalance = MENU.burger - balance; + return newBalance; + } else { + return balance; + } + }, + + orderFalafel: function (balance) { + if (balance >= MENU.falafel) { + let newBalance = MENU.falafel - balance; + return newBalance; + } else { + return balance; + } + }, +}; /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js`