From e42cc33197a9f9d660250da07cafc4171c780745 Mon Sep 17 00:00:00 2001 From: mabalal <95362819+mabalal@users.noreply.github.com> Date: Tue, 21 Mar 2023 14:21:52 +0000 Subject: [PATCH 1/2] 1st-edit --- 2-mandatory/1-recipes.js | 12 +++++++- 2-mandatory/2-currency-code-lookup.js | 7 +++-- 2-mandatory/3-shopping-list.js | 15 ++++++++-- 2-mandatory/4-restaurant.js | 14 ++++++++- 2-mandatory/5-writing-tests.js | 43 +++++++++++++++++++++++---- 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..4c615560 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,14 @@ 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); +console.log(dalRecipe.ingredients); \ 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..830c7cde 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 ===== diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..7edbb7e9 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -17,11 +17,20 @@ let pantry = { fridgeContents: ["butter", "milk"], cupboardContents: ["salt", "tinned tomatoes", "oregano"], }; - function createShoppingList(recipe) { - // write code here -} + const missingIngredients = []; + + recipe.ingredients.forEach(ingredient => { + if (!pantry.fridge.includes(ingredient) && !pantry.cupboards.includes(ingredient)) { + missingIngredients.push(ingredient); + } + }); + return { + name: recipe.name, + items: missingIngredients + }; +} /* ======= 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..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..21eed805 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -34,21 +34,24 @@ 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), "Z"); -}); +// test("a score of 83 is grade A", () => { +// expect(convertScoreToGrade(83).toBe"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", () => { - /* Remove the .skip above, then write the test body. */ -}); +// test("a score of 71 is grade B", (B) => { +// expect(convertScoreToGrade(71), "B") +// }); /* Write a test that checks a score of 68 is grade C */ +// test("a score of 68 is grade C", (C) => { +// expect(convertScoreToGrade(68), "C") +// }); /* Write a test that checks a score of 55 is grade D @@ -73,3 +76,31 @@ test.skip("a score of 71 is grade B", () => { /* Write a test that checks a score of 70 is grade B */ + +test("a score of 83 is grade A", () => { + expect(convertScoreToGrade(83)).toBe("A"); +}); + +test("a score of 71 is grade B", () => { + expect(convertScoreToGrade(71)).toBe("B"); +}); + +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toBe("C"); +}); + +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toBe("D"); +}); + +test("a score of 49 is grade E", () => { + expect(convertScoreToGrade(49)).toBe("E"); +}); + +test("a score of 30 is grade E", () => { + expect(convertScoreToGrade(30)).toBe("E"); +}); + +test("a score of 70 is grade B", () => { + expect(convertScoreToGrade(70)).toBe("B"); +}); From e09b0eeced0eab748ee6907fdadcb321f5cb28a8 Mon Sep 17 00:00:00 2001 From: mabalal <95362819+mabalal@users.noreply.github.com> Date: Wed, 29 Mar 2023 19:52:39 +0100 Subject: [PATCH 2/2] finalEdit --- 1-exercises/A-accessing-values/exercise2.js | 4 +- 1-exercises/D-object-methods/exercise.js | 6 +- 2-mandatory/1-recipes.js | 10 ++- 2-mandatory/2-currency-code-lookup.js | 6 +- 2-mandatory/3-shopping-list.js | 26 ++++---- 2-mandatory/5-writing-tests.js | 68 +++++++-------------- 2-mandatory/6-writing-tests-advanced.js | 48 ++++++++++++--- 7 files changed, 92 insertions(+), 76 deletions(-) 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 4c615560..88c87cfb 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -31,5 +31,11 @@ const dalRecipe = { } -console.log(dalRecipe); -console.log(dalRecipe.ingredients); \ No newline at end of file + +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 830c7cde..a65a4a45 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -37,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 7edbb7e9..9c1f40aa 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -10,27 +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) { - const missingIngredients = []; + 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' ] } - recipe.ingredients.forEach(ingredient => { - if (!pantry.fridge.includes(ingredient) && !pantry.cupboards.includes(ingredient)) { - missingIngredients.push(ingredient); - } - }); +// Output: { name: "Chocolate Cake", items: ["cocoa powder", "baking powder"] } - return { - name: recipe.name, - items: missingIngredients - }; -} + /* ======= 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` @@ -59,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/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 21eed805..78867491 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -35,72 +35,46 @@ function convertScoreToGrade(score) { passes. */ // test("a score of 83 is grade A", () => { -// expect(convertScoreToGrade(83).toBe"A") +// expect(convertScoreToGrade(83).toEqual("A")); // }); - +test("a score of 83 is grade A", () => { + 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("a score of 71 is grade B", (B) => { -// expect(convertScoreToGrade(71), "B") -// }); -/* - Write a test that checks a score of 68 is grade C -*/ -// test("a score of 68 is grade C", (C) => { -// expect(convertScoreToGrade(68), "C") -// }); - -/* - Write a test that checks a score of 55 is grade D -*/ - +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 */ +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 */ - -test("a score of 83 is grade A", () => { - expect(convertScoreToGrade(83)).toBe("A"); -}); - -test("a score of 71 is grade B", () => { - expect(convertScoreToGrade(71)).toBe("B"); -}); - -test("a score of 68 is grade C", () => { - expect(convertScoreToGrade(68)).toBe("C"); -}); - -test("a score of 55 is grade D", () => { - expect(convertScoreToGrade(55)).toBe("D"); -}); - -test("a score of 49 is grade E", () => { - expect(convertScoreToGrade(49)).toBe("E"); -}); - -test("a score of 30 is grade E", () => { - expect(convertScoreToGrade(30)).toBe("E"); -}); - -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..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!") +}); + + + + +