diff --git a/1-exercises/A-accessing-values/exercise2.js b/1-exercises/A-accessing-values/exercise2.js index 5b523ace..26940beb 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -17,9 +17,9 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCapitalCity = capitalCities.UnitedKingdom; -console.log(myCapitalCity); +console.log(capitalCities[myCapitalCity]); /* EXPECTED RESULT diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..ba62e692 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,8 +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..88c87cfb 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,20 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +const dalRecipe = { + title: "Tadka Dal", + Serves: 2, + ingredients: ["veg-oil", "cumin", "cinnamon", "salt"], + + +} + + +console.log(dalRecipe.title); +console.log("Serves" + dalRecipe.Serves); +console.log("ingredients"); +for(let i = 0; i < dalRecipe.ingredients.length; i++) { + console.log(dalRecipe.ingredients[i]); +} + diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..a65a4a45 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -16,9 +16,12 @@ const COUNTRY_CURRENCY_CODES = [ ["NG", "NGN"], ["MX", "MXN"], ]; - function createLookup(countryCurrencyCodes) { - // write code here + const lookup = {}; + for (const [countryCode, currencyCode] of countryCurrencyCodes) { + lookup[countryCode] = currencyCode; + } + return lookup; } /* ======= TESTS - DO NOT MODIFY ===== @@ -34,4 +37,8 @@ test("creates country currency code lookup", () => { NG: "NGN", MX: "MXN", }); -}); \ No newline at end of file +}); + +function speak(line) { + console.log(`The ${this.type} rabbit says '${line}'`); +} \ No newline at end of file diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..9c1f40aa 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -10,18 +10,26 @@ The createShoppingList function should return an object with two properties: - "name" of the recipe, which is a string, - - "items", which is an arry of the missing ingredients that need to be on the shopping list + - "items", which is an array of the missing ingredients that need to be on the shopping list */ - let pantry = { fridgeContents: ["butter", "milk"], cupboardContents: ["salt", "tinned tomatoes", "oregano"], }; function createShoppingList(recipe) { - // write code here + const missingIngredients = recipe.ingredients.filter( + (ingredient) => + !pantry.fridgeContents.includes(ingredient) && + !pantry.cupboardContents.includes(ingredient) + ); + return { name: recipe.name, items: missingIngredients }; } +// Output: { name: 'Spaghetti Carbonara', items: [ 'bacon' ] } + +// Output: { name: "Chocolate Cake", items: ["cocoa powder", "baking powder"] } + /* ======= 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` @@ -50,4 +58,5 @@ test("createShoppingList works for margherita pizza recipe", () => { name: "margherita pizza", 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..ec7e7ee5 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,7 +20,19 @@ 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 ===== diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..78867491 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -34,42 +34,47 @@ function convertScoreToGrade(score) { The first test has been written for you. You need to fix the test so that it passes. */ +// test("a score of 83 is grade A", () => { +// expect(convertScoreToGrade(83).toEqual("A")); +// }); test("a score of 83 is grade A", () => { - expect(convertScoreToGrade(83), "Z"); + expect(convertScoreToGrade(83)).toEqual("A"); }); - /* The rest of the tests have comments describing what to test and you need to write a matching test */ -test.skip("a score of 71 is grade B", () => { +test("a score of 71 is grade B", () => { + expect(convertScoreToGrade(71)).toEqual("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 68 is grade C", () => { + expect(convertScoreToGrade(68)).toEqual("C"); +}) /* Write a test that checks a score of 55 is grade D */ - -/* - Write a test that checks a score of 68 is grade 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 E", () => { + expect(convertScoreToGrade(49)).toEqual("E"); +}) /* - Write a test that checks a score of 30 is grade E + Write a test that checks a score of 30 is grade F */ - +// test("a score of 30 is F", () => { +// expect(convertScoreToGrade(30)).toEqual("F"); +// }) /* Write a test that checks a score of 70 is grade B */ diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..f7324f85 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -10,7 +10,7 @@ trainee has completed. */ -function convertScoreToGrade() { +function convertScoreToGrade(score) { let grade = null; if (score >= 80) { @@ -28,20 +28,30 @@ function convertScoreToGrade() { return grade; } +// function formatCourseworkResult(trainee) { +// if (!trainee.name) { +// return "Error: No trainee name!"; +// } +// let traineeName = trainee.name; + +// if (typeof trainee.score != "number") { +// return "Error: Coursework percent is not a number!"; +// } +// let traineeGrade = convertScoreToGrade(trainee.score); + +// return `${traineeName}'s coursework was marked as grade ${traineeGrade}.`; +// } + function formatCourseworkResult(trainee) { if (!trainee.name) { return "Error: No trainee name!"; } - let traineeName = trainee.name; - - if (typeof trainee.score != "number") { + if (typeof trainee.score !== "number") { return "Error: Coursework percent is not a number!"; } let traineeGrade = convertScoreToGrade(trainee.score); - - return `${traineeName}'s coursework was marked as grade ${traineeGrade}.`; + return traineeGrade; } - /* ======= TESTS - FOR THIS EXERCISE YOU SHOULD MODIFY THEM! ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 6-writing-tests-advanced.js` - To run all exercises/tests in the mandatory folder, run `npm test` @@ -55,6 +65,11 @@ function formatCourseworkResult(trainee) { score: 63 } */ +test("score more than 60 and less than 70 is C", function() { + expect(formatCourseworkResult({name: "Xin", score: 63})).toBe("C"); +}); + + /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -63,7 +78,9 @@ function formatCourseworkResult(trainee) { score: 78 } */ - +test("score more than 70 and less than 80 is B", function() { + expect(formatCourseworkResult({name: "Mona", score: 78})).toBe("B"); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -73,6 +90,9 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ +test("trainee with coursework percent less than 50 returns grade E", function() { +expect(formatCourseworkResult({name: "Ali", score: 49, age: 33, subjects: ["JavaScript", "React", "CSS"]})).toBe("E") +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -81,7 +101,9 @@ function formatCourseworkResult(trainee) { age: 29 } */ - +test("trainee more than 80 and age 29 is grade A", function() { + expect(formatCourseworkResult({score: 90, age: 29})).toBe("Error: No trainee name!") +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -89,3 +111,11 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("trainee with missing name returns error message", function() { +expect(formatCourseworkResult({name: "Aman", subjects: ["HTML", "CSS", "Databases"]})).toBe("Error: Coursework percent is not a number!") +}); + + + + +