diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..f314b9f7 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,18 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +const Recipe = { + title: "Mole", + servings: 2, + ingredients: ["cinnamon", "cumin", "cocoa"], +}; + +console.log(Recipe.title); + +console.log(`Serves: ${Recipe.servings}`); +console.log("Ingredients:"); +console.log(Recipe.ingredients[0]); +console.log(Recipe.ingredients[1]); +console.log(Recipe.ingredients[2]); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..cd7cb2d8 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,8 +18,15 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + let CurrencyCodesObject = {}; + for (let element of countryCurrencyCodes) { + let objectKey = element[0]; + let objectValue = element[1]; + CurrencyCodesObject[objectKey] = objectValue; + } + return CurrencyCodesObject; } +// 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 +41,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..fa6aa19a 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -18,8 +18,25 @@ let pantry = { cupboardContents: ["salt", "tinned tomatoes", "oregano"], }; +// name: "pancakes", +// ingredients: ["flour", "salt", "milk", "eggs", "vegetable oil"], + function createShoppingList(recipe) { - // write code here + let shoppingItems = { + name: recipe.name, + items: [], + }; + + for (let element of recipe.ingredients) { + if ( + !pantry.fridgeContents.includes(element) && + !pantry.cupboardContents.includes(element) + ) { + shoppingItems.items.push(element); + } + } + + return shoppingItems; } /* ======= TESTS - DO NOT MODIFY ===== @@ -43,11 +60,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..9535e5e6 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,8 +20,24 @@ const MENU = { }; let cashRegister = { - // write code here -} + orderBurger: function (balance) { + let updatedBalance = balance; + if (balance - MENU.burger >= 0) { + updatedBalance = balance - MENU.burger; + } + + return updatedBalance; + }, + + orderFalafel: function (balance) { + let updatedBalance = balance; + if (balance - MENU.falafel >= 0) { + updatedBalance = balance - MENU.falafel; + } + + return updatedBalance; + }, +}; /* ======= 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..99e3e496 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,39 @@ 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 */ - -/* - 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 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..1a651ad7 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) { @@ -55,6 +55,16 @@ function formatCourseworkResult(trainee) { score: 63 } */ +test("first test", () => { + 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 +73,16 @@ function formatCourseworkResult(trainee) { score: 78 } */ +test("test 78", () => { + 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: @@ -74,6 +94,19 @@ function formatCourseworkResult(trainee) { } */ +test("test score 49", () => { + 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: { @@ -82,6 +115,15 @@ function formatCourseworkResult(trainee) { } */ +test("test object with no 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 +131,14 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ + +test("test object with no score", () => { + let trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"], + }; + + expect(formatCourseworkResult(trainee)).toEqual( + "Error: Coursework percent is not a number!" + ); +});