diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..694330ac 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; // complete the code +let dogBreed=dog.breed; // 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..4d32e617 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 // 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..a614a981 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -19,6 +19,12 @@ let basketballTeam = { - sorts the top players in alphabetical order - console.logs the name of each player on a new line */ +let topPlayers = basketballTeam.topPlayers; +topPlayers.sort(); + +for (let i = 0; i < topPlayers.length; i++) { + console.log(topPlayers[i]); +} // write code here diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..0fa560b8 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -13,6 +13,10 @@ let capitalCities = { } }; + +capitalCities.UnitedKingdom.population=898000; +capitalCities.China.population=21500000; +capitalCities.Peru = { name: "Lima", population: 9750000 }; /* Using dot notation: - Change the value of UnitedKingdom's capital city population to 8980000. diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..ab35d141 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -16,7 +16,7 @@ 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 @@ -26,7 +26,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..65c798ab 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -8,7 +8,7 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1( car object does not have a property called colour.) let car = { brand: "Ford", yearsOld: 8, @@ -16,7 +16,7 @@ let car = { console.log(car["colour"]); -// Example 2 +// Example 2( because the sayHelloToUser function tries to access the firstName property of the user object, which does not exist, so it returns undefined.) function sayHelloToUser(user) { console.log(`Hello ${user.firstName}`); } @@ -27,7 +27,7 @@ let user = { sayHelloToUser(user); -// Example 3 +// Example 3(getName method will return the string "My pet's name is Fluffy" when called, and that will be logged to the console.) 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..8022cfbf 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..52dac10c 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 +let recipe={ + title: "adana kebab", + + Serves: 2, + Ingredients:["cinnamon", "cumin" ,"cocoa"] + +} +console.log(recipe.title); +console.log(`Serves: ${recipe.Serves}`); +console.log(recipe.Ingredients.join("/n")); \ 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..fc2bd573 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -19,8 +19,14 @@ const COUNTRY_CURRENCY_CODES = [ function createLookup(countryCurrencyCodes) { // write code here + const result = {}; + for (let i = 0; i < countryCurrencyCodes.length; i++){ + result[countryCurrencyCodes[i][0]] = countryCurrencyCodes[i][1]; + + } + return result } - +//console.log(createLookup(COUNTRY_CURRENCY_CODES)) /* ======= 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..8e76e71f 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -20,15 +20,32 @@ let pantry = { function createShoppingList(recipe) { // write code here + const result = { + name: recipe.name, + items: [], + }; + for (let i = 0; i < recipe.ingredients.length; i++) { + if ( + !pantry.fridgeContents.includes(recipe.ingredients[i]) && + !pantry.cupboardContents.includes(recipe.ingredients[i]) + ) { + result.items.push(recipe.ingredients[i]); + } + } + return result; } - +/* let recipe1 = { + name: "pancakes", + ingredients: ["flour", "salt", "milk", "eggs", "vegetable oil"], +}; +console.log(createShoppingList(recipe1)); */ /* ======= 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` - (Reminder: You must have run `npm install` one time before this will work!) */ -test("createShoppingList works for pancakes recipe", () => { + test("createShoppingList works for pancakes recipe", () => { let recipe1 = { name: "pancakes", ingredients: ["flour", "salt", "milk", "eggs", "vegetable oil"], @@ -50,4 +67,4 @@ test("createShoppingList works for margherita pizza recipe", () => { name: "margherita pizza", items: ["flour", "yeast", "mozarella"] }); -}); \ No newline at end of file +}); \ No newline at end of file diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..589453fc 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -21,7 +21,9 @@ const MENU = { let cashRegister = { // write code here -} + orderBurger: (balance) => (balance >= MENU.burger ? balance - MENU.burger : balance), + orderFalafel: (balance) => (balance >= MENU.falafel ? balance - MENU.falafel : balance) +}; /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` @@ -47,4 +49,4 @@ test("orderBurger will not subtract from balance if balance is too low", () => { test("orderFalafel will not subtract from balance if balance is too low", () => { let balance = 7.24; expect(cashRegister.orderFalafel(balance)).toEqual(7.24); -}); +}); \ No newline at end of file diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..ac3dcd8f 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,55 @@ 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), "Z"); +//}); /* 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..bd7e202b 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) { @@ -31,6 +31,7 @@ function convertScoreToGrade() { function formatCourseworkResult(trainee) { if (!trainee.name) { return "Error: No trainee name!"; + // trainee.name = "Trainee"; // had to assign this because js and test dont match on purpose } let traineeName = trainee.name; @@ -55,6 +56,17 @@ function formatCourseworkResult(trainee) { score: 63 } */ +test("Trainee's score", () => { + let trainee1 = { + name: "Xin", + score: 63 + }; + + expect(formatCourseworkResult(trainee1)).toEqual( + "Xin's coursework was marked as grade C." + ); +}); + /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -63,6 +75,16 @@ function formatCourseworkResult(trainee) { score: 78 } */ +test("Trainee's score", () => { + let trainee1 = { + name: "Mona", + score: 78, + }; + + expect(formatCourseworkResult(trainee1)).toEqual( + "Mona's coursework was marked as grade B." + ); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -73,6 +95,16 @@ function formatCourseworkResult(trainee) { subjects: ["JavaScript", "React", "CSS"] } */ +test("Trainee's score", () => { + let trainee1 = { + name: "Ali", + score: 49, + }; + + expect(formatCourseworkResult(trainee1)).toEqual( + "Ali's coursework was marked as grade E." + ); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -81,6 +113,13 @@ function formatCourseworkResult(trainee) { age: 29 } */ +test("Trainee's score", () => { + let trainee1 = { + score: 90, + }; + + expect(formatCourseworkResult(trainee1)).toEqual("Error: No trainee name!"); +}); /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: @@ -89,3 +128,12 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("Trainee's score", () => { + let trainee1 = { + name: "Aman", + }; + + expect(formatCourseworkResult(trainee1)).toEqual( + "Error: Coursework percent is not a number!" + ); +}); \ No newline at end of file diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..11f918f5 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -25,12 +25,41 @@ function countWords(string) { const wordCount = {}; - // write code here + if (string == "") { // got error: ✕ Empty string, expect(countWords("")).toEqual({}); + //return wordCount; + //wordCount; + } else { + const strArr = string.split(" "); + const strArrLen = []; + for (let i = 0; i < strArr.length; i++) { + strArrLen[i] = 1; + for (let j = 0; j < strArr.length; j++) { + if (i !== j && strArr[i] == strArr[j]) { + strArrLen[i] += 1; + } + } + } + for (let i = 0; i < strArr.length; i++) { + wordCount[strArr[i]] = strArrLen[i]; + } + } return wordCount; } +/* if (!wordCount.hasOwnProperty(wordCount[strArr[i]])) { + wordCount[strArr[i]] = 1; + } else { + wordCount[strArr[i]] = wordCount[strArr[i]] + 1; + } */ + +// what i learnt from this +// objects dont dublicate key values that have same name + +//on every single steps of loop declare 1 the current property, +//then in a if statemant use hasownproperty and ++ to make it +1 if there is one more + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm run extra-tests` - To run all exercises/tests in the mandatory folder, run `npm test` @@ -46,9 +75,13 @@ test("Code works for a small string", () => { }); test("A string with, some punctuation", () => { - expect(countWords("A string with, some punctuation")).toEqual( - { A: 1, string: 1, "with,": 1, some: 1, punctuation: 1 } - ); + expect(countWords("A string with, some punctuation")).toEqual({ + A: 1, + string: 1, + "with,": 1, + some: 1, + punctuation: 1, + }); }); test("Empty string", () => { @@ -56,7 +89,11 @@ test("Empty string", () => { }); test("Example task string", () => { - expect(countWords("you're braver than you believe, stronger than you seem, and smarter than you think")).toEqual({ + expect( + countWords( + "you're braver than you believe, stronger than you seem, and smarter than you think" + ) + ).toEqual({ "you're": 1, and: 1, "believe,": 1, @@ -68,4 +105,4 @@ test("Example task string", () => { think: 1, you: 3, }); -}); +}); \ No newline at end of file