From dc66ec6fc8afa9ef76b5c3208e097b440a3cbcaf Mon Sep 17 00:00:00 2001 From: Stella Del Mar <113214552+Stelladelmar@users.noreply.github.com> Date: Thu, 27 Apr 2023 21:57:09 +0100 Subject: [PATCH 1/2] exercises A-B --- 1-exercises/A-accessing-values/exercise1.js | 4 ++-- 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 5 +++++ 1-exercises/B-setting-values/exercise1.js | 2 +- 2-mandatory/1-recipes.js | 17 +++++++++++++++++ 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..f75a1e2d 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= "Spot"; // complete the code +let dogBreed= "Dalmatian"; // complete the code 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..2f83cb93 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= "London"; // complete the code console.log(myCapitalCity); diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..01aa4e5c 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,6 +21,11 @@ let basketballTeam = { */ // write code here +//let sortedTeam = (basketballTeam.topPlayers.sort()); + let sorted = basketballTeam.topPlayers.sort(); +console.log(sorted[0]); +console.log(sorted[1]); +console.log(sorted[2]); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..2ff5d05b 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -23,7 +23,7 @@ let capitalCities = { */ // write code here - +capitalCities.UnitedKingdom.population = 8980000, console.log(capitalCities); /* EXPECTED RESULT diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..6ed16588 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -21,5 +21,22 @@ You should write and log at least 5 recipes */ +//three is enough XD +let recipeCereal = { + title:"Cereal", + serves: 2, + ingredients : [ "milk","cereal"], +} +let recipeSpanishOmelette = { + tittle: "SpanishOmelette", + serves: 2, + ingredients : ["onion", "potato","egg","olive oil", "salt"], +} +let recipeSmoothie ={ + tittle: "Smoothie", + serves: 2, + ingredients: ["banana","Strawberry","oat milk","ice"], +} +console.log(recipeSmoothie) // write code here \ No newline at end of file From c52c751b6b46663e6a418ba57cadf42d226724a6 Mon Sep 17 00:00:00 2001 From: Stella Del Mar <113214552+Stelladelmar@users.noreply.github.com> Date: Mon, 1 May 2023 19:24:21 +0100 Subject: [PATCH 2/2] =?UTF-8?q?exer=20and=20manda=F0=9F=99=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-exercises/A-accessing-values/exercise3.js | 12 ++-- 1-exercises/B-setting-values/exercise1.js | 9 +-- 1-exercises/B-setting-values/exercise2.js | 4 ++ .../C-undefined-properties/exercise.js | 3 + 1-exercises/D-object-methods/exercise.js | 5 +- 2-mandatory/1-recipes.js | 2 +- 2-mandatory/2-currency-code-lookup.js | 9 +++ 2-mandatory/3-shopping-list.js | 22 +++++++- 2-mandatory/4-restaurant.js | 24 ++++++-- 2-mandatory/5-writing-tests.js | 30 ++++++++-- 2-mandatory/6-writing-tests-advanced.js | 55 +++++++++++++++++-- 11 files changed, 146 insertions(+), 29 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 01aa4e5c..5259eea8 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -22,11 +22,15 @@ let basketballTeam = { // write code here //let sortedTeam = (basketballTeam.topPlayers.sort()); - let sorted = basketballTeam.topPlayers.sort(); -console.log(sorted[0]); -console.log(sorted[1]); -console.log(sorted[2]); +let sorted = basketballTeam.topPlayers.sort(); +//console.log(sorted[0]); +//console.log(sorted[1]); +//console.log(sorted[2]); +for (let i = 0; i < sorted.length; i++) { + console.log(sorted[i]); + +} /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 2ff5d05b..0543ddd9 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -10,7 +10,9 @@ let capitalCities = { }, China: { name: "Beijing", - } + population: 21500000, + }, + Peru: { name: "Lima", population: 9750000 }, }; /* @@ -23,8 +25,7 @@ let capitalCities = { */ // write code here -capitalCities.UnitedKingdom.population = 8980000, -console.log(capitalCities); +(capitalCities.UnitedKingdom.population = 8980000), console.log(capitalCities); /* EXPECTED RESULT @@ -34,4 +35,4 @@ console.log(capitalCities); Peru: { name: "Lima", population: 9750000 } } -*/ \ No newline at end of file +*/ diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..1e66a3d2 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -16,6 +16,8 @@ let student = { */ // write code here +student['attendance'] = 90; // Bracket notation +// student.attendance = 90; Dot notation /* - Write an "if" statement that changes the value of hasPassed to true @@ -26,7 +28,9 @@ let student = { */ // write code here +if (student["attendance"] >= 90 && student["examScore"] > 60) { student["hasPassed"] = true +} console.log(student); /* EXPECTED RESULT diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..a4adbf18 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -15,10 +15,12 @@ let car = { }; console.log(car["colour"]); +//we don't have any colour key. // Example 2 function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); + //we don't have the firstName key - that's why it is undefined. } let user = { @@ -34,5 +36,6 @@ let myPet = { "My pet's name is Fluffy"; }, }; +//It is undefined because the method hasn't taken any argument console.log(myPet.getName()); diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..94a4c11b 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,6 +8,10 @@ */ let student = { + getName: (name) => { + console.log(`Student name: ${name}`); + + } // write code here } @@ -16,5 +20,4 @@ student.getName("Daniel"); /* EXPECTED RESULT Student name: Daniel - */ \ No newline at end of file diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6ed16588..71c72783 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -37,6 +37,6 @@ let recipeSmoothie ={ serves: 2, ingredients: ["banana","Strawberry","oat milk","ice"], } -console.log(recipeSmoothie) + // write code here \ 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..9d08dce9 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,6 +18,15 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { +const obj = {} +for (let i = 0; i < countryCurrencyCodes.length; i++) { + const element = countryCurrencyCodes[i]; + const country = element[0]; + const currency = element[1]; + obj[country]= currency; +} +return obj; +//console.log(obj); // write code here } diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..afb20641 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,8 +20,17 @@ let pantry = { function createShoppingList(recipe) { // write code here + //const allIngredients = pantry.fridgeContents.concat(pantry.cupboardContents); + const missingIng = recipe.ingredients.filter( + (item) => !pantry.fridgeContents.includes(item) && !pantry.cupboardContents.includes(item) + ); + return { + name: recipe.name, + items: missingIng, + }; } + /* ======= 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` @@ -43,11 +52,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..a7b99506 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -7,11 +7,14 @@ Add a method to the cashRegister object - the method name should be orderBurger - the method should take one argument called balance, which is a number - - the method should check if there is enough money in the balance to order a burger (i.e. will the balance be greater than or equal to 0 if the burger is ordered) - - if there is enough money in the balance to order the burger, then the price of the burger should be subtracted from the balance + - the method should check if there is enough money in the balance to order a burger (i.e. will the balance be greater + than or equal to 0 if the burger is ordered) + - if there is enough money in the balance to order the burger, then the price of the burger should be subtracted from + the balance - the method should return the new balance - Add another method to the cashRegister object which is called orderFalafel and handles ordering a falafel, in the same way as ordering a burger. + Add another method to the cashRegister object which is called orderFalafel and handles ordering a falafel, in the same way + as ordering a burger. */ const MENU = { @@ -20,8 +23,21 @@ const MENU = { }; let cashRegister = { + orderBurger: (balance) => { + if (balance >= MENU.burger) { + return balance - MENU.burger; + } else return balance; + }, + + orderFalafel: (balance) => { + if (balance >= MENU.falafel) { + return balance - MENU.falafel; + } else if (balance < MENU.falafel) { + return balance; + } + }, // write code here -} +}; /* ======= 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..5f1cf30e 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,51 @@ test("a score of 83 is grade A", () => { 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 */ +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"); +}); \ No newline at end of file diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..c5310cae 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) { @@ -39,7 +39,7 @@ function formatCourseworkResult(trainee) { } let traineeGrade = convertScoreToGrade(trainee.score); - return `${traineeName}'s coursework was marked as grade ${traineeGrade}.`; + return `${traineeName}'s coursework was marked as grade ${traineeGrade}`; } /* ======= TESTS - FOR THIS EXERCISE YOU SHOULD MODIFY THEM! ===== @@ -55,7 +55,15 @@ function formatCourseworkResult(trainee) { score: 63 } */ - +test("Xin with score 63", () => { + 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,7 +71,15 @@ function formatCourseworkResult(trainee) { score: 78 } */ - +test("Mona with score 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: { @@ -73,7 +89,17 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ - +test("Ali with score 49 age 33 and Javascript, React and CSS", () => { + 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,7 +107,15 @@ function formatCourseworkResult(trainee) { age: 29 } */ - +test("With score of 90 and age of 20", () => { + let trainee = { + score: 90, + age: 20, +}; +expect(formatCourseworkResult(trainee)).toEqual( + "Error: No trainee name!" +); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -89,3 +123,12 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("Aman with subjects of HTML, CSS and Databases", () =>{ + let trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases",] + }; + expect(formatCourseworkResult(trainee)).toEqual( + "Aman with HTML, CSS and Databases" + ); +}); \ No newline at end of file