From 2b3e34057603e653fc1f73ab4e477073f3ad11d8 Mon Sep 17 00:00:00 2001 From: Shadi38 <113238379+Shadi38@users.noreply.github.com> Date: Wed, 22 Mar 2023 12:29:29 +0000 Subject: [PATCH] solvimg mandatory --- 1-exercises/A-accessing-values/exercise1.js | 4 +- 1-exercises/A-accessing-values/exercise2.js | 5 ++- 1-exercises/A-accessing-values/exercise3.js | 7 +++ 1-exercises/B-setting-values/exercise1.js | 7 ++- 1-exercises/B-setting-values/exercise2.js | 11 ++++- 1-exercises/D-object-methods/exercise.js | 6 ++- 2-mandatory/1-recipes.js | 13 +++++- 2-mandatory/2-currency-code-lookup.js | 9 +++- 2-mandatory/3-shopping-list.js | 16 ++++++- 2-mandatory/4-restaurant.js | 20 ++++++++- 2-mandatory/5-writing-tests.js | 22 +++++++-- 2-mandatory/6-writing-tests-advanced.js | 50 +++++++++++++++++++-- 12 files changed, 148 insertions(+), 22 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..5624523e 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 = dog.name; +let dogBreed = dog.breed; 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..9bee96c5 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -16,8 +16,9 @@ let capitalCities = { Do not use dot notation for this exercise! */ -let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCountry = "China"; +let myCapitalCity = capitalCities[myCountry]; + console.log(myCapitalCity); diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..47ed7561 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,6 +21,13 @@ let basketballTeam = { */ // write code here + + let newBasketballTeam = basketballTeam.topPlayers.sort(); + +for ( const players of newBasketballTeam){ +console.log(players); +} + /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..83ca1166 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -12,6 +12,12 @@ let capitalCities = { name: "Beijing", } }; +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCitie.peru = { + name: "Lima", + population: 9750000 +}; /* Using dot notation: @@ -22,7 +28,6 @@ let capitalCities = { - Add a population of 9750000 to Peru's capital city. */ -// write code here console.log(capitalCities); diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..ac9b3b36 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -17,6 +17,9 @@ let student = { // write code here + +student["attendance"] = 90; + /* - Write an "if" statement that changes the value of hasPassed to true if the student has attendance that is equal or greater than 90 @@ -27,7 +30,13 @@ let student = { // write code here -console.log(student); +if (student.attendance >= 90 && student.examScore > 60){ + student["hasPassed"] = true; +} + + + + console.log(student); /* EXPECTED RESULT diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..d313fc9f 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -7,8 +7,10 @@ - console.logs a message that says: "Student name: " followed by the name given as an argument */ -let student = { - // write code here +let student = { function getName(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..ad23ca11 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,15 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +let recipe = { + title : "kotlet", + serving : 2, + ingredients : ["onion", "potato", "meet", "spices"] + +}; +console.log(recipe.title); +console.log(recipe.serving); +for(const item of recipe.ingredients){ +console.log(item); +} diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..357c8911 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -16,9 +16,14 @@ const COUNTRY_CURRENCY_CODES = [ ["NG", "NGN"], ["MX", "MXN"], ]; - +let newObject = {}; function createLookup(countryCurrencyCodes) { - // write code here + for (let item of COUNTRY_CURRENCY_CODES) { + let countryCode = item[0]; + newObject[countryCode] = item[1]; + } + + return newObject; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..bfe3d02a 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -17,11 +17,23 @@ let pantry = { fridgeContents: ["butter", "milk"], cupboardContents: ["salt", "tinned tomatoes", "oregano"], }; - function createShoppingList(recipe) { - // write code here + const missingItems = { + name: recipe.name, + items: [], + }; + for (const ingredient of recipe.ingredients) { + if ( + !pantry.fridgeContents.includes(ingredient) && + !pantry.cupboardContents.includes(ingredient) + ) { + missingItems.items.push(ingredient); + } + } + return missingItems; } + /* ======= 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..26a5cd40 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 orderBurger(balance){ + if(balance - MENU.burger >= 0){ + newBalance = MENU.burger -balance; + return newBalance; + }else{ + return balance; + } + + }, + orderFalafel: function orderFalafel(balance){ + if (balance - MENU.falafel >= 0) { + newBalance = MENU.falafel - balance; + return newBalance; + }else{ + 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..97b2edd7 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,16 +43,23 @@ 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", () => { /* Remove the .skip above, then write the test body. */ + 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 */ +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 @@ -65,11 +72,18 @@ test.skip("a score of 71 is grade B", () => { /* 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..dd9494b3 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -2,7 +2,7 @@ For this exercise, the function has been written for you and you need to write the tests. - This function takes a trainee parameter that is an object. The object + This function takes a that is an object. The object contains: name (which represents the trainee's name) and score (which represents the mark given to the trainee's coursework). @@ -55,7 +55,15 @@ function formatCourseworkResult(trainee) { score: 63 } */ - +test("a score of 63 is grade C", () => { + const 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("a score of 78 is grade B", () => { + const 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,18 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ +test("the score of Ali is E", () => { + const 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 +108,14 @@ function formatCourseworkResult(trainee) { age: 29 } */ +test("there is 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 +123,13 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("there is no score", () => { +let trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"], +}; + + expect(formatCourseworkResult(trainee)).toEqual( + "Error: Coursework percent is not a number!" + ); +}); \ No newline at end of file