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..b034edc9 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["UnitedKingdom"]; console.log(myCapitalCity); diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..f1eb9a9e 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -20,7 +20,9 @@ let basketballTeam = { - console.logs the name of each player on a new line */ -// write code here +let sortedTeam = basketballTeam.topPlayers.sort(); + +console.log(sortedTeam); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..616ffc97 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -22,7 +22,12 @@ let capitalCities = { - Add a population of 9750000 to Peru's capital city. */ -// write code here +capitalCities.UnitedKingdom["population"] = 8980000; +capitalCities.China["population"] = 21500000; +capitalCities.peru = { + name: "Lima", + 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..eea2aa1e 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -15,7 +15,7 @@ let student = { - Set the value of attendance to 90 */ -// write code here +student["attendance"] = 90; /* - Write an "if" statement that changes the value of hasPassed to true @@ -25,7 +25,9 @@ let student = { - Use bracket notation to change the value of hasPassed */ -// write code here +if (student.attendance >= 90 && student.examScore >= 60) { + student["hasPassed"] = true; +} console.log(student); diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..ef7e2284 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -13,6 +13,7 @@ let car = { brand: "Ford", yearsOld: 8, }; +// the is no object for colour console.log(car["colour"]); @@ -20,6 +21,7 @@ console.log(car["colour"]); function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); } +// the object name not related to firstName to console user name and function working. let user = { name: "Mira" @@ -35,4 +37,6 @@ let myPet = { }, }; +// uncompleted function no return or doing any thing + console.log(myPet.getName()); diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..bfccc2f1 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,7 +8,9 @@ */ let student = { - // write code here + getname: function (name){ + console.log("Student name: "); + } } student.getName("Daniel"); diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..091fe928 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,26 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +let banoffee = { + title: "Banoffee", + servings: 7, + ingredients: [ + "bananas", + "biscuits", + "butter", + "condensed milk", + "sugar", + "cream", + ], +}; +console.log(banoffee.title); +console.log("Serves: " + banoffee.servings); +console.log("Ingredients:"); +// for (let i = 0; i < banoffee.ingredients.length; i++) { +// console.log(banoffee.ingredients[i]); +// } +// review solution + +banoffee.ingredients.forEach((el) => { + console.log(el); +}); diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..6d64b55b 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -17,10 +17,36 @@ const COUNTRY_CURRENCY_CODES = [ ["MX", "MXN"], ]; -function createLookup(countryCurrencyCodes) { - // write code here +function createLookup(countryCurrencyCodes){ + let resultLookup = {}; + countryCurrencyCodes.forEach(([el, curr]) => { + resultLookup[el] = curr + }); + return resultLookup } + +//------>>> another solution + +// function createLookup(countryCurrencyCodes) { +// let countryCurrencyCodesLookup = {}; +// for (let countryCurrencyCode of countryCurrencyCodes) { +// countryCurrencyCodesLookup[countryCurrencyCode[0]] = +// countryCurrencyCode[1]; +// } +// return countryCurrencyCodesLookup; +// } + +//------>>> another solution + +// function createLookup(COUNTRY_CURRENCY_CODES) { +// const country = new Map(COUNTRY_CURRENCY_CODES); +// const obj = Object.fromEntries(country); +// return obj +// } + + + /* ======= 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` @@ -34,4 +60,4 @@ test("creates country currency code lookup", () => { NG: "NGN", MX: "MXN", }); -}); \ No newline at end of file +}); diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..013d094b 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -18,9 +18,26 @@ let pantry = { cupboardContents: ["salt", "tinned tomatoes", "oregano"], }; + + function createShoppingList(recipe) { - // write code here -} + let shoppingList = {}; + shoppingList.items =[]; + shoppingList.name = recipe.name + // check ingredients + for (let ingredient of recipe.ingredients) + { + + if ( !pantry.fridgeContents.includes(ingredient) + && !pantry.cupboardContents.includes(ingredient)) { + + shoppingList.items.push(ingredient); + + } + } + return shoppingList; +}; + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js` diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..6388141b 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,9 +20,23 @@ const MENU = { }; let cashRegister = { - // write code here + orderBurger: function(balance){ + if(balance >= MENU.burger){ + return balance - MENU.burger + }else{ + return balance; + } + }, + orderFalafel: function(balance){ + if (balance >= MENU.falafel) { + return balance - MENU.falafel + }else{ + return balance; + } + } } + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` - To run all exercises/tests in the mandatory folder, run `npm test` diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..7a4ccbba 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,56 @@ 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)).toBe("B"); /* Remove the .skip above, then write the test body. */ }); -/* + +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toBe("C"); + /* Write a test that checks a score of 68 is grade C */ +}); +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toBe("D"); /* Write a test that checks a score of 55 is grade D */ +}); +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toBe("C"); /* Write a test that checks a score of 68 is grade C */ +}); +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toBe("D"); /* Write a test that checks a score of 55 is grade D */ +}); +test("a score of 49 is grade E", () => { + expect(convertScoreToGrade(49)).toBe("E"); /* Write a test that checks a score of 49 is grade E */ +}); +test("a score of 30 is grade E", () => { + expect(convertScoreToGrade(30)).toBe("E"); /* Write a test that checks a score of 30 is grade E */ +}); -/* +test("a score of 70 is grade B", () => { + expect(convertScoreToGrade(70)).toBe("B"); + /* Write a test that checks a score of 70 is grade B */ +}); diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..c68f1f38 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) { @@ -55,6 +55,16 @@ function formatCourseworkResult(trainee) { score: 63 } */ +test("Trainee Xin, score of 63",() =>{ + let trainee = { + name: "Xin", + score: 63, + }; + expect(formatCourseworkResult(trainee)).toEqual( + `${trainee.name}'s coursework was marked as grade C.` + ); +}); + /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -63,6 +73,12 @@ function formatCourseworkResult(trainee) { score: 78 } */ +test(`trainee Mona, score is 78`,() => { + let trainee ={ name:`Mona`, score: 78,} + expect(formatCourseworkResult(trainee)).toEqual( + `${trainee.name}'s coursework was marked as grade B.` + ); +}) /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -73,6 +89,17 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ +test(`trainee Ali , score is 49`, () => { + let trainee = { + name: "Ali", + score: 49, + age: 33, + subjects: ["JavaScript", "React", "CSS"], + }; + expect(formatCourseworkResult(trainee)).toEqual( + `${trainee.name}'s coursework was marked as grade E.` + ); +}) /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -81,6 +108,14 @@ function formatCourseworkResult(trainee) { age: 29 } */ +test("Trainee , score of 90", () => { + 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 +124,12 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("Trainee Aman, score of ", () => { + let trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"], + }; + expect(formatCourseworkResult(trainee)).toEqual( + `Error: Coursework percent is not a number!` + ); +});