From 7add93d19f148084018fe03c93e9eb99fa62d952 Mon Sep 17 00:00:00 2001 From: batuncer Date: Tue, 21 Mar 2023 18:35:54 +0000 Subject: [PATCH 1/3] coursework are completed --- 1-exercises/A-accessing-values/exercise1.js | 6 ++- 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 5 +- 1-exercises/B-setting-values/exercise1.js | 8 ++++ 1-exercises/B-setting-values/exercise2.js | 6 +++ .../C-undefined-properties/exercise.js | 12 ++--- 1-exercises/D-object-methods/exercise.js | 3 ++ 2-mandatory/1-recipes.js | 29 ++++++++++- 2-mandatory/2-currency-code-lookup.js | 11 ++++- 2-mandatory/3-shopping-list.js | 11 +++++ 2-mandatory/4-restaurant.js | 14 +++++- 2-mandatory/5-writing-tests.js | 32 +++++++++---- 2-mandatory/6-writing-tests-advanced.js | 48 ++++++++++++++++++- 13 files changed, 164 insertions(+), 23 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..a739df67 100644 --- a/1-exercises/A-accessing-values/exercise1.js +++ b/1-exercises/A-accessing-values/exercise1.js @@ -11,6 +11,10 @@ let dog = { happiness: 6 }; +console.log(typeof(dog.breed)) +console.log(typeof(dog.name)) +console.log(typeof(dog.isHungry)) +console.log(typeof(dog.happiness)) /* You can access the values of each property using dot notation. Log the name and breed of this dog using dot notation. @@ -19,7 +23,7 @@ let dog = { let dogName; // complete the code let dogBreed; // complete the code -console.log(`${dogName} is a ${dogBreed}`); +console.log(`${dog.name} is a ${dog.breed}`) /* EXPECTED RESULT diff --git a/1-exercises/A-accessing-values/exercise2.js b/1-exercises/A-accessing-values/exercise2.js index 5b523ace..7e97f354 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=capitalCities[myCountry] // 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..8bc9bcff 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,8 +21,9 @@ let basketballTeam = { */ // write code here - - +let topPlayerArray=basketballTeam.topPlayers +let sortPlayers= topPlayerArray.sort() +sortPlayers.forEach((_) => console.log(_)); /* EXPECTED RESULT Dennis Rodman diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..bc40a495 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -13,6 +13,9 @@ let capitalCities = { } }; + + + /* Using dot notation: - Change the value of UnitedKingdom's capital city population to 8980000. @@ -23,6 +26,11 @@ let capitalCities = { */ // write code here +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCities.Peru = {}; +capitalCities.Peru.name = "Lima"; +capitalCities.Peru.population = 9750000; console.log(capitalCities); diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..7dd73696 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -16,6 +16,7 @@ let student = { */ // write code here +student["attendance"]=90; /* - Write an "if" statement that changes the value of hasPassed to true @@ -27,6 +28,11 @@ 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..ce76087f 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -8,15 +8,15 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1 === car object does not colour property. when we try to acces car colour using bracket notation the value will be undefined. +// to solve this error we must add colour property with value before console.log let car = { brand: "Ford", yearsOld: 8, }; +console.log(car["colour"]); -console.log(car["colour"]); - -// Example 2 +// Example 2 === problem is user object doesnt has a property as firstName. function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); } @@ -25,9 +25,9 @@ let user = { name: "Mira" }; -sayHelloToUser(user); +sayHelloToUser(user); -// Example 3 +// Example 3 === problem is here we are not returning anything explicitly from function. to solve this we need to add return inside of the function let myPet = { animal: "Cat", getName: function() { diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..6795c00e 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,6 +9,9 @@ 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..96cac3b0 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,31 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +let recipe = { + title : "Omlette", + servings:4, + Ingredients:["eggs","butter","salt"] + +} + +let recipeTwo ={ + title: "Pasta", + Serving:2, + Ingredients:["pasta", "meat mince", "oil","water"] +} +console.log(recipe); +console.log(recipe["title"]) +console.log(`Serving: ${recipe.servings}`) +console.log("Ingredients:") +recipe.Ingredients.forEach(Ingredient => console.log(Ingredient)) + +// if we want to do it with one function// + +function recipeDetails(recipe){ + console.log(`${recipe.title}\nServices: ${recipe.servings}\nIngredients:\n${recipe.Ingredients.join('\n')}`); +} + +recipeDetails(recipe) +recipeDetails(recipeTwo) \ 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..11e8a523 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -17,10 +17,19 @@ const COUNTRY_CURRENCY_CODES = [ ["MX", "MXN"], ]; -function createLookup(countryCurrencyCodes) { +function createLookup(COUNTRY_CURRENCY_CODES) { // write code here + const countyObj = COUNTRY_CURRENCY_CODES.reduce((obj,[coutry,currency])=>{ + obj[coutry]=currency + return obj + },{}) + +return countyObj } + + + /* ======= 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..60ed1a80 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,6 +20,17 @@ let pantry = { function createShoppingList(recipe) { // write code here + let missingItem=[]; + recipe.ingredients.forEach((ingredient) => { + if (!pantry.fridgeContents.includes(ingredient) && !pantry.cupboardContents.includes(ingredient)){ + missingItem.push(ingredient) + } + }); + + return{ + name: recipe.name, + items: missingItem + } } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..8dac9193 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,7 +21,19 @@ const MENU = { let cashRegister = { // write code here -} + orderBurger(balance) { + if (balance >= MENU["burger"]) { + return balance - MENU["burger"]; + } + return balance + }, + orderFalafel(balance) { + if (balance >= MENU["falafel"]) { + return 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..5644bb48 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), "A"); }); /* @@ -43,33 +43,49 @@ 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), ("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), ("C")) +}) /* Write a test that checks a score of 55 is grade D */ - +test("a score of 68 is grade D ", ()=>{ + expect(convertScoreToGrade(55), ("D")) +} ) /* Write a test that checks a score of 68 is grade C */ - +test("a score of 68 is grade C", () =>{ + expect(convertScoreToGrade(68), ("C")) +}); /* Write a test that checks a score of 55 is grade D */ - +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68), "C"); +}); /* Write a test that checks a score of 49 is grade E */ - +test("a score of 68 is grade E", () => { + expect(convertScoreToGrade(49), "E"); +}); /* Write a test that checks a score of 30 is grade E */ - +test("a score of 68 is grade E", () => { + expect(convertScoreToGrade(30), "E"); +}); /* Write a test that checks a score of 70 is grade B */ +test("a score of 68 is grade B", () => { + expect(convertScoreToGrade(70), "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..f4b0c224 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -10,7 +10,9 @@ trainee has completed. */ -function convertScoreToGrade() { +const { TestScheduler } = require("jest"); + +function convertScoreToGrade(score) { let grade = null; if (score >= 80) { @@ -55,6 +57,14 @@ function formatCourseworkResult(trainee) { score: 63 } */ +test("returns correct coursework result string for trainee", () => { + const trainee = { + name: "Xin", + score: 63, + }; + const output = "Xin's coursework was marked as grade C."; + expect(formatCourseworkResult(trainee)).toBe(output); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -63,6 +73,15 @@ function formatCourseworkResult(trainee) { score: 78 } */ +test("returns correct coursework result string for trainee",()=>{ + const trainee = { + name: "Mona", + score: 78, + }; + const output = + `Mona's coursework was marked as grade B.`; + expect(formatCourseworkResult(trainee)).toBe(output) +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -73,6 +92,16 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ +test("returns correct coursework result string for trainee", () => { + const trainee = { + name: "Ali", + score: 49, + age: 33, + subjects: ["JavaScript", "React", "CSS"], + }; + const output = "Ali's coursework was marked as grade E."; + expect(formatCourseworkResult(trainee)).toBe(output); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -81,7 +110,14 @@ function formatCourseworkResult(trainee) { age: 29 } */ - +test("returns correct coursework result string for trainee", () => { + const trainee = { + score: 90, + age: 29, + }; + const output = "Error: No trainee name!"; + expect(formatCourseworkResult(trainee)).toBe(output); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -89,3 +125,11 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("returns error message when trainee score is missing", () => { + const trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"], + }; + const output = "Error: Coursework percent is not a number!"; + expect(formatCourseworkResult(trainee)).toBe(output); +}); \ No newline at end of file From 6a7be78fab217c36d6ef51a84b59dff577e23a45 Mon Sep 17 00:00:00 2001 From: batuncer Date: Wed, 22 Mar 2023 09:44:08 +0000 Subject: [PATCH 2/3] test editted --- 2-mandatory/6-writing-tests-advanced.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index f4b0c224..8d4cc477 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -110,7 +110,7 @@ test("returns correct coursework result string for trainee", () => { age: 29 } */ -test("returns correct coursework result string for trainee", () => { +test("returns an error string for trainee", () => { const trainee = { score: 90, age: 29, From c1cfda94ad3118b359a554660c482dec57a9709e Mon Sep 17 00:00:00 2001 From: batuncer Date: Wed, 22 Mar 2023 09:57:53 +0000 Subject: [PATCH 3/3] test errors are fixed --- 2-mandatory/3-shopping-list.js | 6 +++--- 2-mandatory/5-writing-tests.js | 22 +++++++++++----------- 2-mandatory/6-writing-tests-advanced.js | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index 60ed1a80..b2231617 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,16 +20,16 @@ let pantry = { function createShoppingList(recipe) { // write code here - let missingItem=[]; + let missingItems=[]; recipe.ingredients.forEach((ingredient) => { if (!pantry.fridgeContents.includes(ingredient) && !pantry.cupboardContents.includes(ingredient)){ - missingItem.push(ingredient) + missingItems.push(ingredient) } }); return{ name: recipe.name, - items: missingItem + items: missingItems } } diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 5644bb48..5aa69e19 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), "A"); + expect(convertScoreToGrade(83)).toBe("A"); }); /* @@ -44,48 +44,48 @@ test("a score of 83 is grade A", () => { */ test("a score of 71 is grade B", () => { - expect(convertScoreToGrade(71), ("B")) + expect(convertScoreToGrade(71)).toBe("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), ("C")) + expect(convertScoreToGrade(68)).toBe("C") }) /* Write a test that checks a score of 55 is grade D */ -test("a score of 68 is grade D ", ()=>{ - expect(convertScoreToGrade(55), ("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), ("C")) + expect(convertScoreToGrade(68)).toBe("C") }); /* Write a test that checks a score of 55 is grade D */ -test("a score of 68 is grade C", () => { - expect(convertScoreToGrade(68), "C"); +test("a score of 55 is grade C", () => { + expect(convertScoreToGrade(55)).toBe("D"); }); /* Write a test that checks a score of 49 is grade E */ test("a score of 68 is grade E", () => { - expect(convertScoreToGrade(49), "E"); + expect(convertScoreToGrade(49)).toBe("E"); }); /* Write a test that checks a score of 30 is grade E */ test("a score of 68 is grade E", () => { - expect(convertScoreToGrade(30), "E"); + expect(convertScoreToGrade(30)).toBe("E"); }); /* Write a test that checks a score of 70 is grade B */ test("a score of 68 is grade B", () => { - expect(convertScoreToGrade(70), "B"); + expect(convertScoreToGrade(70)).toBe("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 8d4cc477..0139b35e 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -110,7 +110,7 @@ test("returns correct coursework result string for trainee", () => { age: 29 } */ -test("returns an error string for trainee", () => { +test("returns an error string for missing name", () => { const trainee = { score: 90, age: 29,