From 68b82fad060b9d3be76cf84d30bd86b9431b6020 Mon Sep 17 00:00:00 2001 From: Saliha Popal Date: Thu, 23 Mar 2023 21:57:22 +0000 Subject: [PATCH] All done. --- 1-exercises/A-accessing-values/exercise1.js | 12 ++-- 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 2 +- 1-exercises/B-setting-values/exercise1.js | 7 ++ 1-exercises/B-setting-values/exercise2.js | 7 ++ .../C-undefined-properties/exercise.js | 6 +- 1-exercises/D-object-methods/exercise.js | 4 +- 2-mandatory/1-recipes.js | 70 ++++++++++++++++++- 2-mandatory/2-currency-code-lookup.js | 7 +- 2-mandatory/3-shopping-list.js | 10 ++- 2-mandatory/4-restaurant.js | 13 +++- 2-mandatory/5-writing-tests.js | 32 +++++++-- 2-mandatory/6-writing-tests-advanced.js | 49 +++++++++++-- 3-extra/1-count-words.js | 24 +++++-- 14 files changed, 211 insertions(+), 34 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..da3d87d8 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 = "Spot"; // complete the code +let dogBreed = "Dalmatian"; // 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..b9b58113 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 = "London"; // 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..8e98204a 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,7 +21,7 @@ let basketballTeam = { */ // write code here - +console.log(basketballTeam.topPlayers) /* EXPECTED RESULT diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..9cee1d9a 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -24,6 +24,13 @@ 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("After:") + 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..13894a62 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -17,6 +17,8 @@ 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,6 +29,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..39dc7c58 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,7 +14,7 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]); /// The car object does not have color property // Example 2 function sayHelloToUser(user) { @@ -22,7 +22,7 @@ function sayHelloToUser(user) { } let user = { - name: "Mira" + name: "Mira" ////. The object has only name property it does not have firstname property }; sayHelloToUser(user); @@ -31,7 +31,7 @@ sayHelloToUser(user); let myPet = { animal: "Cat", getName: function() { - "My pet's name is Fluffy"; + return "My pet's name is Fluffy"; //. It should have return }, }; diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..ce1b3ec6 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: ${name}`); + } } student.getName("Daniel"); diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..bf4f5af2 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,72 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// Recipe 1 +let recipe1 = { + title: "Mole", + servings: 2, + ingredients: ["cinnamon", "cumin", "cocoa"] +}; + +console.log(recipe1.title); +console.log(`Serves: ${recipe1.servings}`); +console.log("Ingredients:"); +for (let i = 0; i < recipe1.ingredients.length; i++) { + console.log(recipe1.ingredients[i]); +} + +// Recipe 2 +let recipe2 = { + title: "Bolani", + servings: 4, + ingredients: ["Gandana", "Dough", "oil", "salt", "green pepper"] +}; + +console.log(recipe2.title); +console.log(`Serves: ${recipe2.servings}`); +console.log("Ingredients:"); +for (let i = 0; i < recipe2.ingredients.length; i++) { + console.log(recipe2.ingredients[i]); +} + +// Recipe 3 +let recipe3 = { + title: "Borani Badinjan", + servings: 6, + ingredients: ["Ege plant", "tomato sauce", "oil", "black pepper", "turmeric", "garlic", "yogurt"] +}; + +console.log(recipe3.title); +console.log(`Serves: ${recipe3.servings}`); +console.log("Ingredients:"); +for (let i = 0; i < recipe3.ingredients.length; i++) { + console.log(recipe3.ingredients[i]); +} + +// Recipe 4 +let recipe4 = { + title: "Aye khanum", + servings: 8, + ingredients: ["beef mans", "carrot", "potatoes", "onion", "garlic", "oil", "tomato paste", "yogurt", "fresh coriander"] +}; + +console.log(recipe4.title); +console.log(`Serves: ${recipe4.servings}`); +console.log("Ingredients:"); +for (let i = 0; i < recipe4.ingredients.length; i++) { + console.log(recipe4.ingredients[i]); +} + +// Recipe 5 +let recipe5 = { + title: "Qabili", + servings: 6, + ingredients: ["rice", "oil", "onion", "carrots", "raisin", "salt", "lamb meat", "char masala"] +}; + +console.log(recipe5.title); +console.log(`Serves: ${recipe5.servings}`); +console.log("Ingredients:"); +for (let i = 0; i < recipe5.ingredients.length; i++) { + console.log(recipe5.ingredients[i]); +} diff --git a/2-mandatory/2-currency-code-lookup.js b/2-mandatory/2-currency-code-lookup.js index 5fde14f1..212a4629 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,8 +18,13 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + const lookup = {}; + for(const [countryCode, currencyCode] of countryCurrencyCodes){ + lookup[countryCode] = currencyCode; + } + return lookup } +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` diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..f7eb8fb4 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -10,7 +10,7 @@ The createShoppingList function should return an object with two properties: - "name" of the recipe, which is a string, - - "items", which is an arry of the missing ingredients that need to be on the shopping list + - "items", which is an array of the missing ingredients that need to be on the shopping list */ let pantry = { @@ -19,7 +19,13 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + const missingItems = recipe.ingredients.filter(ingredient =>{ + return !pantry.fridgeContents.includes(ingredient)&& !pantry.cupboardContents.includes(ingredient); + }); + return { + name: recipe.name, + items: missingItems, + } } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..98eb576c 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,7 +20,18 @@ const MENU = { }; let cashRegister = { - // write code here + orderBurger: function(balance){ + if(balance >= MENU.burger){ + balance -= MENU.burger; + } + return balance; + }, + orderFalafel: function(balance){ + if(balance >= MENU.falafel){ + balance -= MENU.falafel + } + return balance; + } } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..bb032d07 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -8,7 +8,7 @@ function convertScoreToGrade(score) { let grade = null; - + if (score >= 80) { grade = "A"; } else if (score >= 70) { @@ -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,53 @@ 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("b score of 71 is grade B", () => { + expect(convertScoreToGrade(71)).toBe("B") }); /* - Write a test that checks a score of 68 is grade C + Write a test that checks a score of 68 is grade C */ +test("c score of 68 is grade C", () => { + expect(convertScoreToGrade(68)).toBe("C") +}) /* Write a test that checks a score of 55 is grade D */ +test("d score of 55 is grade D", () => { + expect(convertScoreToGrade(55)).toBe("D") +}); /* Write a test that checks a score of 68 is grade C */ +test("c score of 68 is grade C", () =>{ + expect(convertScoreToGrade(68)).toBe("C") +}); /* Write a test that checks a score of 55 is grade D */ - +test("d of score 55 is grade D", () =>{ + expect(convertScoreToGrade(55)).toBe("D") +}); /* Write a test that checks a score of 49 is grade E */ +test("e of score 49 is grade E", ()=>{ + expect(convertScoreToGrade(49)).toBe("E"); +}); /* Write a test that checks a score of 30 is grade E */ +test("e score of 30 is grade E", ()=>{ + expect(convertScoreToGrade(30)).toBe("E"); +}); /* Write a test that checks a score of 70 is grade B */ +test("b 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..53028f3a 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -10,12 +10,12 @@ trainee has completed. */ -function convertScoreToGrade() { +function convertScoreToGrade(score) { let grade = null; - if (score >= 80) { + if(score >= 80) { grade = "A"; - } else if (score >= 70) { + }else if (score >= 70) { grade = "B"; } else if (score >= 60) { grade = "C"; @@ -24,12 +24,11 @@ function convertScoreToGrade() { } else { grade = "E"; } - return grade; } -function formatCourseworkResult(trainee) { - if (!trainee.name) { +function formatCourseworkResult(trainee) { + if (!trainee.name){ return "Error: No trainee name!"; } let traineeName = trainee.name; @@ -56,6 +55,13 @@ function formatCourseworkResult(trainee) { } */ +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,6 +69,13 @@ 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: @@ -73,6 +86,15 @@ function formatCourseworkResult(trainee) { 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: @@ -81,7 +103,13 @@ function formatCourseworkResult(trainee) { 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 +117,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..d229461a 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -25,11 +25,27 @@ function countWords(string) { const wordCount = {}; + // - Strings and string manipulation - // write code here - - return wordCount; -} + // Split the input string into an array of words + const words = string.split(" "); + + // Loop through the array of words + words.forEach((word) => { + + if(string == ""){ + return wordCount + } + else if(wordCount[word]) { + wordCount[word]++; + } + else { + wordCount[word] = 1; + } + }); + return wordCount; + } + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm run extra-tests`