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..ca3e19d7 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 - +for (let player of basketballTeam.topPlayers.sort()) { + console.log(player); +} /* EXPECTED RESULT @@ -29,4 +31,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..4fbb65f1 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,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 @@ -34,4 +41,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..0a7d5273 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 @@ -26,6 +28,9 @@ let student = { */ // write code here +if (student.attendance >= 90 && student.examScore > 60) { + student.hasPassed = true; +} console.log(student); @@ -38,4 +43,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..6d56dd9e 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -15,6 +15,7 @@ let car = { }; console.log(car["colour"]); +// there is no property called 'colour' in the car object // Example 2 function sayHelloToUser(user) { @@ -22,17 +23,19 @@ function sayHelloToUser(user) { } let user = { - name: "Mira" + name: "Mira", }; sayHelloToUser(user); +// there is no property called 'firstName' in the user object // Example 3 let myPet = { animal: "Cat", - getName: function() { + getName: function () { "My pet's name is Fluffy"; }, }; console.log(myPet.getName()); +// nothing is being returned in the method definition of the 'getName' method 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..edc3f871 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,63 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +const recipe1 = { + title: "Fried Egg", + servings: 1, + ingredients: ["Egg", "Oil"], +}; + +console.log(recipe1.title); +console.log(`Serves: ${recipe1.servings}`); +console.log(`Ingredients: +${recipe1.ingredients[0]} +${recipe1.ingredients[1]}`); + +const recipe2 = { + title: "Spinach Rice", + servings: 2, + ingredients: ["Spinach", "Rice"], +}; + +console.log(recipe2.title); +console.log(`Serves: ${recipe2.servings}`); +console.log(`Ingredients: +${recipe2.ingredients[0]} +${recipe2.ingredients[1]}`); + +const recipe3 = { + title: "Behari Kebab", + servings: 4, + ingredients: ["Kebab", "Masala"], +}; + +console.log(recipe3.title); +console.log(`Serves: ${recipe3.servings}`); +console.log(`Ingredients: +${recipe3.ingredients[0]} +${recipe3.ingredients[1]}`); + +const recipe4 = { + title: "Fried Fish", + servings: 1, + ingredients: ["Fish", "Oil"], +}; + +console.log(recipe4.title); +console.log(`Serves: ${recipe4.servings}`); +console.log(`Ingredients: +${recipe4.ingredients[0]} +${recipe4.ingredients[1]}`); + +const recipe5 = { + title: "Icecream Milkshake", + servings: 1, + ingredients: ["Icecream", "Milk"], +}; + +console.log(recipe5.title); +console.log(`Serves: ${recipe5.servings}`); +console.log(`Ingredients: +${recipe5.ingredients[0]} +${recipe5.ingredients[1]}`); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..ad452a9c 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,9 +18,17 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + const obj = {}; + + for (let pair of countryCurrencyCodes) { + obj[pair[0]] = pair[1]; + } + + return obj; } +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 +42,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..36d6a55d 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,7 +19,21 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + const newIngredientsList = []; + + for (let ingredient of recipe.ingredients) { + if ( + !pantry.fridgeContents.includes(ingredient) && + !pantry.cupboardContents.includes(ingredient) + ) { + newIngredientsList.push(ingredient); + } + } + + delete recipe.ingredients; + recipe.items = newIngredientsList; + + return recipe; } /* ======= TESTS - DO NOT MODIFY ===== @@ -43,11 +57,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..3c308279 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,8 +20,16 @@ const MENU = { }; let cashRegister = { - // write code here -} + orderBurger(balance) { + return balance >= MENU.burger ? balance - MENU.burger : balance; + }, + orderFalafel(balance) { + return balance >= MENU.falafel ? balance - MENU.falafel : balance; + }, +}; + +console.log(cashRegister.orderBurger(6.5)); +console.log(cashRegister.orderBurger(6.49)); /* ======= 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..64aba6ea 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -27,10 +27,29 @@ function countWords(string) { const wordCount = {}; // write code here + if (!string) { + return wordCount; + } else { + const split = string.split(" "); + + for (let word of split) { + wordCount[word] = count(word, split); + } + } return wordCount; } +function count(word, splitString) { + let count = 0; + + for (let word2 of splitString) { + if (word === word2) count++; + } + + return count; +} + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm run extra-tests` - To run all exercises/tests in the mandatory folder, run `npm test` @@ -46,9 +65,13 @@ test("Code works for a small string", () => { }); test("A string with, some punctuation", () => { - expect(countWords("A string with, some punctuation")).toEqual( - { A: 1, string: 1, "with,": 1, some: 1, punctuation: 1 } - ); + expect(countWords("A string with, some punctuation")).toEqual({ + A: 1, + string: 1, + "with,": 1, + some: 1, + punctuation: 1, + }); }); test("Empty string", () => { @@ -56,7 +79,11 @@ test("Empty string", () => { }); test("Example task string", () => { - expect(countWords("you're braver than you believe, stronger than you seem, and smarter than you think")).toEqual({ + expect( + countWords( + "you're braver than you believe, stronger than you seem, and smarter than you think" + ) + ).toEqual({ "you're": 1, and: 1, "believe,": 1,