diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..4f3c76cb 100644 --- a/1-exercises/A-accessing-values/exercise1.js +++ b/1-exercises/A-accessing-values/exercise1.js @@ -5,10 +5,10 @@ */ let dog = { - breed: "Dalmatian", - name: "Spot", - isHungry: true, - happiness: 6 + breed: "Dalmatian", //string + name: "Spot", //string + isHungry: true, //boolean + happiness: 6 //number }; /* @@ -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.breed; // complete the code +let dogBreed = dog.name; // 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..c7c8e188 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..62c0f4dc 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -19,9 +19,14 @@ let basketballTeam = { - sorts the top players in alphabetical order - console.logs the name of each player on a new line */ +function sortedPopPlayers (basketballTeam) { + const sortedPlayers = basketballTeam.topPlayers.sort() + return sortedPlayers.forEach(element => { + console.log(element) + }); +} -// write code here - +sortedPopPlayers(basketballTeam) /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..a6a002c7 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -22,8 +22,13 @@ 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 ={}; +capitalCities.Peru.name = "Lima"; +capitalCities.Peru.population = 9750000; +// capitalCities.peru = { name: "lima", population: 9750000} console.log(capitalCities); /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..7df39f81 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -15,8 +15,7 @@ let student = { - Set the value of attendance to 90 */ -// write code here - +student["attendence"] = 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 @@ -25,7 +24,9 @@ let student = { - Use bracket notation to change the value of hasPassed */ -// write code here +if(student.examScore > 60 && student.attendence >= 90){ + 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..81793aaf 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -15,6 +15,7 @@ let car = { }; console.log(car["colour"]); +// property "colur" doesn't exist // Example 2 function sayHelloToUser(user) { @@ -26,7 +27,7 @@ let user = { }; sayHelloToUser(user); - +//the function sayHelloToUser trying eccess to non-existent property in an object "user" // Example 3 let myPet = { animal: "Cat", @@ -36,3 +37,4 @@ let myPet = { }; console.log(myPet.getName()); +//the function getName does'n access to any prorerty inside of an object \ No newline at end of file diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..56518206 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,11 +8,13 @@ */ let student = { - // write code here + getName: function(name){ + console.log(`Student name: ${name}`) + + } } student.getName("Daniel"); - /* EXPECTED RESULT Student name: Daniel diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..10437567 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -21,5 +21,18 @@ You should write and log at least 5 recipes */ +let TheRecipeCard = {} +TheRecipeCard.title = "Items mix :D" +TheRecipeCard.Serves = 2; -// write code here \ No newline at end of file + +TheRecipeCard.Ingredients = [] + +let ingr = ["item1","item2","item3","item4"] + +ingr.forEach(element => { + TheRecipeCard.Ingredients.push(element) +}) +console.log(TheRecipeCard.title) +console.log(`Serves: ${TheRecipeCard.Serves}`) +TheRecipeCard.Ingredients.map(property => {console.log(property)}) diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..f76b6b23 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,9 +18,13 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + let obj = {}; + for(const [code, currency] of countryCurrencyCodes){ + obj[code] = currency; + } + return obj + // return Object.fromEntries(countryCurrencyCodes); } - /* ======= 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..bff115cd 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,9 +19,24 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + let missingIngredients = { + name: 0, + items: [], + } + + missingIngredients.items = recipe.ingredients.filter(element => !pantry.fridgeContents.includes(element) && !pantry.cupboardContents.includes(element)); + missingIngredients.name = recipe.name; + + return missingIngredients; } + +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` diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..ca277af6 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,7 +20,17 @@ const MENU = { }; let cashRegister = { - // write code here + orderBurger (balance){ + if(balance >= MENU.burger){ + return balance - MENU.burger + } else return balance + }, + orderFalafel(balance){ + if(balance >= MENU.falafel){ + return balance - MENU.falafel + } else return balance + + } } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..3d35459f 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -7,7 +7,7 @@ */ function convertScoreToGrade(score) { - let grade = null; + let grade = "null"; if (score >= 80) { grade = "A"; @@ -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,49 @@ test("a score of 83 is grade A", () => { write a matching test */ -test.skip("a score of 71 is grade B", () => { - /* Remove the .skip above, then write the test body. */ +test("a score of 71 is grade B", () => { + expect(convertScoreToGrade(71)).toBe("B") }); /* Write a test that checks a score of 68 is grade C */ - +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toBe("C") +}) /* Write a test that checks a score of 55 is grade 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 grase C", () => { + expect(convertScoreToGrade(68)).toBe("C") +}) /* Write a test that checks a score of 55 is grade D */ - +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toBe("D") +}) /* Write a test that checks a score of 49 is grade E */ - +test("a score of 49 is grade E", () => { + expect(convertScoreToGrade(49)).toBe("E") +}) /* Write a test that checks a score of 30 is grade E */ - +test("a score of 30 is grade E", () => { + expect(convertScoreToGrade(30)).toBe("E") +}) /* Write a test that checks a score of 70 is grade B */ +test("a score of 70 is grade B", () => { + expect(convertScoreToGrade(70)).toBe("B") +}) + diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..4e9c771e 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) { @@ -34,7 +34,7 @@ function formatCourseworkResult(trainee) { } let traineeName = trainee.name; - if (typeof trainee.score != "number") { + if (typeof trainee.score !== "number") { return "Error: Coursework percent is not a number!"; } let traineeGrade = convertScoreToGrade(trainee.score); @@ -55,7 +55,13 @@ function formatCourseworkResult(trainee) { score: 63 } */ - +test("trainee with score 63 has grade C", ()=>{ + const trainee = { + name: "Xin", + score: 63, + } + expect(formatCourseworkResult(trainee)).toBe("Xin's coursework was marked as grade C."); +}) /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -63,25 +69,48 @@ function formatCourseworkResult(trainee) { score: 78 } */ +test("trainee with score 78 has grade B", ()=>{ + const trainee = { + name: "Mona", + score: 78, + } + expect(formatCourseworkResult(trainee)).toBe("Mona's coursework was marked as grade B."); +}) /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { name: "Ali", - score: 49, + score: 63, age: 33, subjects: ["JavaScript", "React", "CSS"] } */ +test("trainee with score 63 has grade C", ()=>{ + const trainee = { + name: "Ali", + score: 63, + age: 33, + subjects: ["JavaScript", "React", "CSS"] + } + expect(formatCourseworkResult(trainee)).toBe("Ali's coursework was marked as grade C."); +}) /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { score: 90, - age: 29 + age: 29, } */ +test("trainee with score 90 has grade A", ()=>{ + const trainee = { + score: 90, + age: 29, + } + expect(formatCourseworkResult(trainee)).toBe("Error: No trainee name!"); +}) /* Write a test that checks the output of formatCourseworkResult when passed the following trainee: { @@ -89,3 +118,10 @@ function formatCourseworkResult(trainee) { subjects: ["HTML", "CSS", "Databases"] } */ +test("trainee with no score", ()=>{ + const trainee = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"] + } + expect(formatCourseworkResult(trainee)).toBe("Error: Coursework percent is not a number!"); +}); diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..cb41f7bc 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -25,11 +25,26 @@ function countWords(string) { const wordCount = {}; + const words = string.split(" ") - // write code here +if(string === ""){ + return wordCount + }else + for (let i = 0; i < words.length; i++) { + if (wordCount[words[i]] === undefined) { + wordCount[words[i]] = 1; + } else { + wordCount[words[i]]++; + } + } + + // words.forEach(element => { + // wordCount.element = + // }); return wordCount; } +// console.log("I love CodeYourFuture") /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm run extra-tests`