diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..82f46143 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..6f9a48ee 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -17,7 +17,7 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCapitalCity = capitalCities[myCountry]// 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..201041de 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,7 +21,11 @@ let basketballTeam = { */ // write code here - +let newArray = basketballTeam.topPlayers.sort() +console.log(newArray); +for (num of newArray ) { + console.log(num) +} /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..ec88baf2 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 = {}; +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..e8c0b88c 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -8,15 +8,14 @@ let student = { examScore: 65, hasPassed: false }; - /* Using bracket notation - Add a property to the student object for attendance - Set the value of attendance to 90 */ - // write code here - +// let attendance = student.attendance; +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,7 +25,9 @@ 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..934c245e 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,15 +14,15 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]); // we have not assigned a new property named colour // Example 2 function sayHelloToUser(user) { - console.log(`Hello ${user.firstName}`); + console.log(`Hello ${user.firstName}`); // there is not a property called firstName } let user = { - name: "Mira" + name: "Mira", }; sayHelloToUser(user); @@ -30,8 +30,8 @@ sayHelloToUser(user); // Example 3 let myPet = { animal: "Cat", - getName: function() { - "My pet's name is Fluffy"; + getName: function () { + "My pet's name is Fluffy"; // if we would add return then the function would return the text "My pet's name is Fluffy" otherwise its undefined }, }; 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..5518d1ec 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,56 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +let recipeCard = { + recipe1: { + name: "Salad", + serves: 3, + ingredients: ["cucumber", "tomatoes", "beans"], + }, + recipe2: { + name: "Pie", + serves: 2, + ingredients: ["cheese", "flour", "water", "butter"], + }, + recipe3: { + name: "Pizza", + serves: 3, + ingredients: ["olives", "flour", "oil", "peperoni"], + }, + recipe4: { + name: "Fries", + serves: 3, + ingredients: ["potatoes", "oil", "salt", "pepper"], + }, + recipe5: { + name: "Rice", + serves: 4, + ingredients: ["rice", "carrots", "peas"], + }, +}; +console.log(recipeCard.recipe1.name); +console.log(`Serves:${recipeCard.recipe1.serves}`); +for (index in recipeCard.recipe1.ingredients) { + console.log(recipeCard.recipe1.ingredients[index]); +} +console.log(recipeCard.recipe2.name); +console.log(`Serves:${recipeCard.recipe2.serves}`); +for (index in recipeCard.recipe2.ingredients) { + console.log(recipeCard.recipe2.ingredients[index]); +} +console.log(recipeCard.recipe3.name); +console.log(`Serves:${recipeCard.recipe1.serves}`); +for (index in recipeCard.recipe3.ingredients) { + console.log(recipeCard.recipe3.ingredients[index]); +} +console.log(recipeCard.recipe4.name); +console.log(`Serves:${recipeCard.recipe4.serves}`); +for (index in recipeCard.recipe4.ingredients) { + console.log(recipeCard.recipe4.ingredients[index]); +} +console.log(recipeCard.recipe5.name); +console.log(`Serves:${recipeCard.recipe5.serves}`); +for (index in recipeCard.recipe5.ingredients) { + console.log(recipeCard.recipe5.ingredients[index]); +} diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..c1cfd449 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,7 +19,12 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here +let createLookup = {}; +createLookup.forEach(element => createLookup[element[0]]= [element[1]]; + + // return Object.keys(COUNTRY_CURRENCY_CODES) } +return createLookup; /* ======= 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..2767a20d 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,8 +20,24 @@ let pantry = { function createShoppingList(recipe) { // write code here -} + let recipeName = recipe.name; + let recipeIngredients = recipe.ingredients; + + let emptyContainer = []; + recipeIngredients.forEach((item) => { + if ( + !pantry.fridgeContents.includes(item) && + !pantry.cupboardContents.includes(item) + ) { + emptyContainer.push(item); + } + }); + return { + name: recipeName, + items: emptyContainer, + }; +} /* ======= 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` @@ -43,11 +59,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..c33ff6b4 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,8 +20,20 @@ const MENU = { }; let cashRegister = { - // write code here -} + orderBurger: function (balance) { + if (balance >= MENU.burger) { + balance -= MENU.burger; + } + return balance; + }, + orderFalafel: function(balance) { + if (balance >= MENU.falafel) { + 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/package.json b/package.json index 2bef6f48..8da059a8 100644 --- a/package.json +++ b/package.json @@ -17,5 +17,8 @@ "homepage": "https://github.com/CodeYourFuture/JavaScript-Core-2-Coursework-Week1-London8#readme", "devDependencies": { "jest": "^26.6.3" + }, + "dependencies": { + "jshint": "^2.13.5" } }