diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..b2509097 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; +let dogBreed = dog.breed; 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..b034edc9 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"]; console.log(myCapitalCity); diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..0e5ea894 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -20,7 +20,15 @@ let basketballTeam = { - console.logs the name of each player on a new line */ -// write code here +function bestPlayers() { + let topPlayersNames = basketballTeam.topPlayers + topPlayersNames = topPlayersNames.sort() + console.log(topPlayersNames) + +} + +bestPlayers(); + /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..192bb9df 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..950edc5d 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,12 @@ let student = { */ // write code here +function isPassed (attendance, examScore) { + if ((attendance >= 90) && (examScore > 60)) { + return true; + } +} +student ["hasPassed"] = isPassed(90, 65); console.log(student); diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..7f1dd197 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,25 +14,25 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]); // the property colour is not defined in the object car // Example 2 function sayHelloToUser(user) { - console.log(`Hello ${user.firstName}`); + console.log(`Hello ${user.firstName}`); } let user = { name: "Mira" }; -sayHelloToUser(user); +sayHelloToUser(user); // in this example the object user has the property name and not firstName as used in the function sayHelloToUser // Example 3 let myPet = { animal: "Cat", getName: function() { - "My pet's name is Fluffy"; + return("My pet's name is Fluffy"); }, }; -console.log(myPet.getName()); +console.log(myPet.getName()); // the function in the property getName should has a return statement diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..bdc0971f 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,7 +8,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..17de1ed4 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,22 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +let favRecipe1 = { + name: "salads", + servings: "services: " + 2, + ingredients: ["tomato", "cucamber", "lemon", "avocado", "sweetcorn", "eggs"] +} +function ingredientPrinting(favRecipe1) { + console.log("ingredients: ") + for ( let i = 0; i < favRecipe1.ingredients.length; i++) { + console.log(favRecipe1.ingredients[i]); + } +} + + +console.log(favRecipe1.name); +console.log(favRecipe1.servings); +ingredientPrinting(favRecipe1); + diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..68933c42 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,8 +18,18 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + let key; + let value; + let countryCurrencyObject = {}; + for (let i = 0; i < countryCurrencyCodes.length; i++) { + key = countryCurrencyCodes[i][0]; + value = countryCurrencyCodes[i][1]; + countryCurrencyObject[key] = value; } +return countryCurrencyObject +} + +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` @@ -34,4 +44,5 @@ 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..543d995c 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,8 +19,28 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here -} + let shoppingList = {}; + shoppingList.name = recipe.name; + shoppingList.items = []; + for (let element of recipe.ingredients) { + if (pantry.fridgeContents.includes(element)) { + shoppingList.items = shoppingList.items + } else if (pantry.cupboardContents.includes(element)) { + shoppingList.items = shoppingList.items + } else { + shoppingList.items.push(element) + } + } + return shoppingList + } +// let recipe1 = { +// name: "pancakes", +// ingredients: ["flour", "salt", "milk", "eggs", "vegetable oil"], +// }; +// console.log(createShoppingList(recipe1)); + + + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js` @@ -34,6 +54,7 @@ test("createShoppingList works for pancakes recipe", () => { ingredients: ["flour", "salt", "milk", "eggs", "vegetable oil"], }; + expect(createShoppingList(recipe1)).toEqual({ name: "pancakes", items: ["flour", "eggs", "vegetable oil"], diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..b9fca535 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -18,11 +18,26 @@ const MENU = { burger: 6.5, falafel: 7.25, }; - +let balance = 6.5 let cashRegister = { - // write code here +orderBurger: function(balance) { + if (balance >= MENU.burger) { + balance = balance - MENU.burger + } + return balance; +}, +orderFalafel: function(balance) { + if (balance >= MENU.falafel) { + balance = balance - MENU.falafel + } + return balance; +}, } + + + + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` - To run all exercises/tests in the mandatory folder, run `npm test` diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..6d9d3ae6 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,54 @@ test("a score of 83 is grade A", () => { write a matching test */ -test.skip("a score of 71 is grade B", () => { - /* Remove the .skip above, then write the test body. */ +test("a score of 71 is grade B", () => { + expect(convertScoreToGrade(71)).toEqual("B") }); /* Write a test that checks a score of 68 is grade C */ +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toEqual("C") +}); /* Write a test that checks a score of 55 is grade D */ +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toEqual("D") +}); /* Write a test that checks a score of 68 is grade C */ +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toEqual("C") +}); /* Write a test that checks a score of 55 is grade D */ +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toEqual("D") +}); /* Write a test that checks a score of 49 is grade E */ +test("a score of 49 is grade E", () => { + expect(convertScoreToGrade(49)).toEqual("E") +}); /* Write a test that checks a score of 30 is grade E */ +test("a score of 30 is grade E", () => { + expect(convertScoreToGrade(30)).toEqual("E") +}); /* Write a test that checks a score of 70 is grade B */ +test("a score of 70 is grade B", () => { + expect(convertScoreToGrade(70)).toEqual("B") +}); diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..0ac5dc39 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -10,7 +10,9 @@ trainee has completed. */ -function convertScoreToGrade() { +const { TestScheduler } = require("jest"); + +function convertScoreToGrade(score) { let grade = null; if (score >= 80) { @@ -55,6 +57,13 @@ function formatCourseworkResult(trainee) { score: 63 } */ +test("Xin's coursework was marked as grade C", () => { + let trainee = { + name: "Xin", + score: 63, + } + expect(formatCourseworkResult(trainee)).toEqual("Xin's coursework was marked as grade C.") +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -63,6 +72,13 @@ function formatCourseworkResult(trainee) { score: 78 } */ +test("Mona's coursework was marked as grade B", () => { + let trainee = { + name: "Mona", + score: 78, + } + expect(formatCourseworkResult(trainee)).toEqual("Mona's coursework was marked as grade B.") +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -73,6 +89,15 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ +test("Ali's coursework was marked as grade E", () => { + let trainee = { + name: "Ali", + score: 49, + age: 33, + subjects: ["JavaScript", "React", "CSS"] + } + expect(formatCourseworkResult(trainee)).toEqual("Ali's coursework was marked as grade E.") +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -81,6 +106,13 @@ function formatCourseworkResult(trainee) { age: 29 } */ +test("no trainee name is equal to Error: No trainee name!", () => { + let trainee = { + score: 90, + age: 29, + } + expect(formatCourseworkResult(trainee)).toEqual("Error: No trainee name!") +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -89,3 +121,12 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("no trainee score is equal to Error: Coursework percent is not a number!", () => { + let trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databasese"], + + } + expect(formatCourseworkResult(trainee)).toEqual("Error: Coursework percent is not a number!") +}); + diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..40f5c6a8 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -25,8 +25,8 @@ function countWords(string) { const wordCount = {}; - - // write code here + + return wordCount; }