diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..3ddf9c68 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; //dot notation using objectName.propertyName +let dogBreed = dog.breed; //dot notation using objectName.propertyName 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..2754bd32 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["UnitedKingdom"]; // using bracket notation => objectName["propertyName"] console.log(myCapitalCity); diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..4539dde8 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,6 +21,14 @@ let basketballTeam = { */ // write code here +basketballTeam = basketballTeam["topPlayers"]; // using bracket notation => objectName["propertyName"] +console.log(basketballTeam); + +basketballTeam.sort(); +console.log(basketballTeam); + +let sortedbasketballTeam= [...basketballTeam].sort(); +console.log(sortedbasketballTeam); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..0060be84 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -24,6 +24,12 @@ let capitalCities = { // write code here + capitalCities.UnitedKingdom.population = 8980000; // objectName.objectName.propertyName = "newValue"; + capitalCities.China.population = 21500000; + capitalCities.Peru = {}; + capitalCities.Peru.name = "Lima"; + capitalCities.Peru.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..13c59b16 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 @@ -26,6 +27,9 @@ let student = { */ // write code here +if(student["attendance"] >= 90 && student["examScore"] > 60){ + student["hasPassed"] = true; +} console.log(student); diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..54907982 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,11 +8,14 @@ */ let student = { - // write code here -} - +getName: function (name) { + console.log("Student name:" +name); + }, +}; student.getName("Daniel"); + + /* EXPECTED RESULT Student name: Daniel diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..e23cdb36 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -21,5 +21,22 @@ You should write and log at least 5 recipes */ - -// write code here \ No newline at end of file +const recipes = [ + { + title: "Mole", + servings: 2, + ingredients: ["cinnamon", "cumin", "cocoa"] + }, + { + title: "Pancakes", + servings: 4, + ingredients: ["flour", "sugar", "baking powder", "salt", "milk", "eggs", "butter"] + }]; + recipes.forEach(recipe => { + console.log(recipe.title); + console.log(`Serves: ${recipe.servings}`); + console.log("Ingredients:"); + recipe.ingredients.forEach(ingredient => { + console.log(ingredient); + }); + }); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..f142e8d7 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,8 +19,14 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here + const lookup = {}; + for (const [countryCode, currencyCode] of countryCurrencyCodes) { + lookup[countryCode] = currencyCode; + } + return lookup; } + /* ======= 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` diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..c097cbb4 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,6 +20,16 @@ let pantry = { function createShoppingList(recipe) { // write code here + const missingIngredients = recipe.ingredients.filter((ingredient) => { + return ( + !pantry.fridgeContents.includes(ingredient) && + !pantry.cupboardContents.includes(ingredient) + ); + }); + return { + name: recipe.name, + items: missingIngredients, + }; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..90155e18 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(balance) { + if (balance >= MENU.burger) { + return balance - MENU.burger; + } else { + return balance; + } + }, + orderFalafel(balance) { + if (balance >= MENU.falafel) { + return balance - MENU.falafel; + } else { + return balance; + } + }, +}; /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..e8542b74 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -35,7 +35,7 @@ function convertScoreToGrade(score) { passes. */ test("a score of 83 is grade A", () => { - expect(convertScoreToGrade(83), "Z"); + expect(convertScoreToGrade(83)).toEqual("A"); }); /* @@ -43,33 +43,49 @@ test("a score of 83 is grade A", () => { write a matching test */ -test.skip("a score of 71 is grade B", () => { +// test.skip("a score of 71 is grade B", () => { /* Remove the .skip above, then write the test body. */ -}); +// }); /* Write a test that checks a score of 68 is grade C + */ - +test("a score of 83 is grade A", () => { + expect(convertScoreToGrade(83)).toBe("A"); +}); /* Write a test that checks a score of 55 is grade D */ - +test("a score of 71 is grade B", () => { + expect(convertScoreToGrade(71)).toBe("B"); +}); /* Write a test that checks a score of 68 is grade C */ - +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toBe("C"); +}); /* Write a test that checks a score of 55 is grade D */ - +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toBe("D"); +}); /* Write a test that checks a score of 49 is grade E */ - +test("a score of 49 is grade E", () => { + expect(convertScoreToGrade(49)).toBe("E"); +}); /* Write a test that checks a score of 30 is grade E */ - +test("a score of 30 is grade E", () => { + expect(convertScoreToGrade(30)).toBe("E"); +}); /* Write a test that checks a score of 70 is grade B */ +test("a score of 70 is grade B", () => { + expect(convertScoreToGrade(70)).toBe("B"); +}); \ No newline at end of file diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..1724dbef 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -50,11 +50,13 @@ function formatCourseworkResult(trainee) { /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: - { - name: "Xin", - score: 63 - } -*/ + */ + test("formatCourseworkResult returns correct output for trainee with score 63", () => { +const trainee = { + name: "Xin", + score: 63, +}; + }); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -63,7 +65,12 @@ function formatCourseworkResult(trainee) { score: 78 } */ - +test("formatCourseworkResult returns correct output for trainee with score 63", () => { + const trainee = { + name: "Mona", + score: 78, + }; +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: {