diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..e12b02e3 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -1,25 +1,69 @@ -/* - The Recipe Card - Never forget another recipe! - Create an object to hold information on your favorite recipe. +const recipe1 = { + title: "burger", + servings: 2, + ingredients: ["beef", "bun", "cheese", "ketchup"] +}; - It should have properties for - - title (a string), - - servings (a number), and - - ingredients (an array of strings) +console.log(recipe1.title); +console.log(`Serves: ${recipe1.servings}`); +console.log("Ingredients:"); +recipe1.ingredients.forEach(ingredient => console.log(ingredient)); +console.log("\n"); - On separate lines (one console.log statement for each), log the recipe information so it looks like: - Mole - Serves: 2 - Ingredients: - cinnamon - cumin - cocoa +const recipe2 = { + title: "creamy Chicken pasta", + servings: 4, + ingredients: ["chicken breasts", "doubble cream", "chedar cheese", "mushroom", "onion", "pasta"] +}; - You should write and log at least 5 recipes -*/ -// write code here \ No newline at end of file +console.log(recipe2.title); +console.log(`Serves: ${recipe2.servings}`); +console.log("Ingredients:"); +recipe2.ingredients.forEach(ingredient => console.log(ingredient)); +console.log("\n"); + + +const recipe3 = { + title: "Spaghetti Bolognese", + servings: 6, + ingredients: ["spaghetti", "ground beef", "onion", "garlic", "tomato sauce", "tomato paste", "red wine", "olive oil"] +}; + + +console.log(recipe3.title); +console.log(`Serves: ${recipe3.servings}`); +console.log("Ingredients:"); +recipe3.ingredients.forEach(ingredient => console.log(ingredient)); +console.log("\n"); + + +const recipe4 = { + title: "Kuru Fasulye", + servings: 4, + ingredients: ["beans", "carrots", "beef diced", "cellerey", "onion", "spice", "peper paste"] +}; + + +console.log(recipe4.title); +console.log(`Serves: ${recipe4.servings}`); +console.log("Ingredients:"); +recipe4.ingredients.forEach(ingredient => console.log(ingredient)); +console.log("\n"); + + +const recipe5 = { + title: "brownies", + servings: 24, + ingredients: ["butter", "brown sugar", "white sugar", "eggs", "vanilla extract", "flour", "baking soda", "salt", "chocolate chips"] +}; + + +console.log(recipe5.title); +console.log(`Serves: ${recipe5.servings}`); +console.log("Ingredients:"); +recipe5.ingredients.forEach(ingredient => console.log(ingredient)); +console.log("\n"); \ No newline at end of file diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..3e1f2d8d 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,9 +18,20 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + const keys = {}; + + for (let i = 0; i < countryCurrencyCodes.length; i++) { + const [countryCode, currencyCode] = countryCurrencyCodes[i]; + keys[countryCode] = currencyCode; + } + + return keys; } +const countryCurrencyLookup = createLookup(COUNTRY_CURRENCY_CODES); +console.log(countryCurrencyLookup); + + /* ======= 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..3f7f0f9a 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,9 +19,29 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + + const missingIngredients = []; + for (let i = 0; i < recipe.ingredients.length; i++) { + const ingredient = recipe.ingredients[i]; + if (!pantry.fridgeContents.includes(ingredient) && !pantry.cupboardContents.includes(ingredient)) { + missingIngredients.push(ingredient); + } + } + return { + name: recipe.name, + items: missingIngredients + }; } +const recipe = { + name: "Carrot Cake", + ingredients: ["all-purpose flour", "baking powder", "baking soda", "ground cinnamon", "brown sugar", "vegetable oil", "eggs", "vanilla extract", "grated carrots", "chopped walnuts", "cream cheese", "unsalted butter"] +}; + +const shoppingList = createShoppingList(recipe); +console.log(shoppingList); + + /* ======= 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` diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..199442ae 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: function(balance) { + if (balance - MENU.burger >= 0) { + balance -= MENU.burger; + } + return balance; + }, + orderFalafel: function(balance) { + if (balance - MENU.falafel >= 0) { + 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/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..cee14abd 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)).toBe("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)).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 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"); +}); diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..df03bcda 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -55,7 +55,12 @@ function formatCourseworkResult(trainee) { score: 63 } */ - +it("returns the expected string for a trainee with a score of 63", () => { + const trainee = { name: "Xin", score: 63 }; + const expected = "Xin's coursework was marked as grade C."; + const result = formatCourseworkResult(trainee); + expect(result).toEqual(expected); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -63,7 +68,12 @@ function formatCourseworkResult(trainee) { score: 78 } */ - +it("returns the expected string for a trainee with a score of 78", () => { + const trainee = { name: "Mona", score: 78 }; + const expected = "Mona's coursework was marked as grade B."; + const result = formatCourseworkResult(trainee); + expect(result).toEqual(expected); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -74,6 +84,13 @@ function formatCourseworkResult(trainee) { } */ +it("returns an error message if the trainee's score is not a number", () => { + const trainee = { name: "Ali", score: "49%", age: 33, subjects: ["JavaScript", "React", "CSS"] }; + const expected = "Error: Coursework percent is not a number!"; + const result = formatCourseworkResult(trainee); + expect(result).toEqual(expected); +}); + /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -81,7 +98,12 @@ function formatCourseworkResult(trainee) { age: 29 } */ - +it("returns an error message if the trainee's name is missing", () => { + const trainee = { score: 90, age: 29 }; + const expected = "Error: No trainee name!"; + const result = formatCourseworkResult(trainee); + expect(result).toEqual(expected); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -89,3 +111,9 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +it("returns an error message if the trainee's score is missing", () => { + const trainee = { name: "Aman", subjects: ["HTML", "CSS", "Databases"] }; + const expected = "Error: Coursework percent is not a number!"; + const result = formatCourseworkResult(trainee); + expect(result).toEqual(expected); +}); \ No newline at end of file