From 5810c7c36c04fdbc874ff22710a87334e5fd1a6d Mon Sep 17 00:00:00 2001 From: RobCso Date: Tue, 13 Sep 2022 10:54:24 +0100 Subject: [PATCH 1/3] complete excercise and mandatory --- 1-exercises/A-accessing-values/exercise1.js | 12 ++++---- 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 3 ++ 1-exercises/B-setting-values/exercise1.js | 7 +++++ 1-exercises/B-setting-values/exercise2.js | 12 ++++++-- .../C-undefined-properties/exercise.js | 6 ++-- 1-exercises/D-object-methods/exercise.js | 7 +++-- 2-mandatory/1-recipes.js | 28 +++++++++++++++++-- 2-mandatory/2-currency-code-lookup.js | 6 +++- 2-mandatory/3-shopping-list.js | 6 +++- 2-mandatory/4-restaurant.js | 13 ++++++++- testFolder/test1.js | 17 +++++++++++ 12 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 testFolder/test1.js diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..a4d8edf1 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.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..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..eea8b6db 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,7 +21,10 @@ let basketballTeam = { */ // write code here +let players = basketballTeam.topPlayers.sort() +players.forEach(item=>console.log(item)) +// 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..aa4632b3 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -23,6 +23,13 @@ let capitalCities = { */ // write code here +capitalCities.UnitedKingdom.population = 8980000; +capitalCities.China.population = 21500000; +capitalCities.Peru ={name: "Lima", + population: 9750000}; +// capitalCities.Peru = {}; +// capitalCities.Peru.name = "Lima"; +// capitalCities.Peru.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..3a03b6ca 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -6,8 +6,8 @@ let student = { name: "Reshma Saujani", examScore: 65, - hasPassed: false -}; + hasPassed: false, + }; /* Using bracket notation @@ -16,6 +16,9 @@ let student = { */ // write code here +student["attendance"] = 90; + + /* - Write an "if" statement that changes the value of hasPassed to true @@ -27,6 +30,11 @@ let student = { // write code here + if (student.examScore > 60 && student.attendance >=90) { + 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..e4effaa6 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,11 +14,11 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]);//the colour property of car is not assigned // Example 2 function sayHelloToUser(user) { - console.log(`Hello ${user.firstName}`); + console.log(`Hello ${user.firstName}`);//firstName property of the user object is not defined } let user = { @@ -31,7 +31,7 @@ sayHelloToUser(user); let myPet = { animal: "Cat", getName: function() { - "My pet's name is Fluffy"; + "My pet's name is Fluffy";//the function doesnt have a return keyword }, }; diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..2101d12d 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,11 +8,14 @@ */ let student = { - // write code here + getName: function(name) { + this.name = name; + console.log(`Student name: ${this.name}`); + } } student.getName("Daniel"); - +// console.log(student) /* EXPECTED RESULT Student name: Daniel diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..fe6713e2 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -11,7 +11,7 @@ - ingredients (an array of strings) On separate lines (one console.log statement for each), log the recipe information so it looks like: - + Mole Serves: 2 Ingredients: @@ -22,4 +22,28 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here + +let RecipeCard = { + Mole: { + Serves: 2, + Ingredients: ["cinnamon", "cumin", "cocoa"], + } +} + +console.log(RecipeCard) + +let recipe = { + title: "Juice", + Serves: 4, + Ingredients: [ + "oranges", + "water", + "ice", + "sugar" + ] +} +console.log(recipe.title) +console.log("Serves: " + recipe.Serves) +console.log("Ingredients:") +recipe.Ingredients.forEach(ingredient=>console.log(ingredient)) \ 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..9efac78d 100644 --- a/2-mandatory/2-currency-code-lookup.js +++ b/2-mandatory/2-currency-code-lookup.js @@ -18,7 +18,11 @@ const COUNTRY_CURRENCY_CODES = [ ]; function createLookup(countryCurrencyCodes) { - // write code here + let currencyCodesObject = {}; + countryCurrencyCodes.forEach(element => { + currencyCodesObject[element[0]] = element[1] + }); + return currencyCodesObject; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..f5eab480 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -18,8 +18,12 @@ let pantry = { cupboardContents: ["salt", "tinned tomatoes", "oregano"], }; + + function createShoppingList(recipe) { - // write code here + let combinedPantry = pantry.fridgeContents.concat(pantry.cupboardContents) //[...pantry.fridgeContents, ...pantry.cupboardContents] + let list = recipe.ingredients.filter(ingredient => !combinedPantry.includes(ingredient)) + return {name : recipe.name, items: list} } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..e6122ac1 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) { + this.balance = balance; + if (balance >= MENU.burger) { + return balance = balance - MENU.burger; + } else return balance; + }, + orderFalafel: function(balance) { + this.balance = balance; + if (balance >= MENU.falafel) { + return balance = balance - MENU.falafel; + } else return balance; + } } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/testFolder/test1.js b/testFolder/test1.js new file mode 100644 index 00000000..c931c083 --- /dev/null +++ b/testFolder/test1.js @@ -0,0 +1,17 @@ +const MENU = { + burger: 6.5, + falafel: 7.25, +}; + +let cashRegister = { + orderBurger: function(balance) { + this.balance = balance; + //console.log(cashRegister) + if (balance >= MENU.burger) { + return balance = balance-MENU.burger + } + } +} + +//cashRegister +console.log(cashRegister.orderBurger(7)) \ No newline at end of file From b9ffaca121952cc1a9e1e9e728aa84064b7e0291 Mon Sep 17 00:00:00 2001 From: RobCso Date: Tue, 13 Sep 2022 11:11:22 +0100 Subject: [PATCH 2/3] complete all excercise and mandatory --- 2-mandatory/1-recipes.js | 100 ++++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 12 deletions(-) diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index fe6713e2..0964970a 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -24,26 +24,102 @@ // write code here -let RecipeCard = { - Mole: { - Serves: 2, - Ingredients: ["cinnamon", "cumin", "cocoa"], - } -} +// let RecipeCard = { +// Mole: { +// Serves: 2, +// Ingredients: ["cinnamon", "cumin", "cocoa"], +// } +// } -console.log(RecipeCard) +// console.log(RecipeCard) let recipe = { - title: "Juice", + title: "Gin&Tonic", Serves: 4, Ingredients: [ - "oranges", - "water", + "gin", + "tonic", "ice", - "sugar" + "lemon" ] } console.log(recipe.title) console.log("Serves: " + recipe.Serves) console.log("Ingredients:") -recipe.Ingredients.forEach(ingredient=>console.log(ingredient)) \ No newline at end of file +recipe.Ingredients.forEach(ingredient=>console.log(ingredient)) +console.log("") + + +let recipe2 = { + title: "Pizza", + Serves: 4, + Ingredients: [ + "flour", + "water", + "yeast", + "tomatoes", + "pepperoni" + ] +} +console.log(recipe2.title) +console.log("Serves: " + recipe2.Serves) +console.log("Ingredients:") +recipe2.Ingredients.forEach(ingredient=>console.log(ingredient)) +console.log("") + +let recipe3 = { + title: "Goulash", + Serves: 4, + Ingredients: [ + "Beef", + "tomatoes", + "pepepers", + "paprika", + "potatoes", + "lard", + "large onion", + "carrots", + "salt", + "pepper", + ] +} +console.log(recipe3.title) +console.log("Serves: " + recipe3.Serves) +console.log("Ingredients:") +recipe3.Ingredients.forEach(ingredient=>console.log(ingredient)) +console.log("") + +let recipe4 = { + title: "Pasta Bolognese", + Serves: 2, + Ingredients: [ + "pasta", + "water", + "minced meat", + "tomatoes", + "olive oil", + "garlic", + "oregano", + "red wine" + ] +} +console.log(recipe4.title) +console.log("Serves: " + recipe4.Serves) +console.log("Ingredients:") +recipe4.Ingredients.forEach(ingredient=>console.log(ingredient)) +console.log("") + +let recipe5 = { + title: "Bacon Butty", + Serves: 1, + Ingredients: [ + "crusty roll", + "bacon slices", + "butter" + ] +} +console.log(recipe5.title) +console.log("Serves: " + recipe5.Serves) +console.log("Ingredients:") +recipe5.Ingredients.forEach(ingredient=>console.log(ingredient)) +console.log("") \ No newline at end of file From c2efae1cebfe0237d5e57b74387ad3bc3cd7162e Mon Sep 17 00:00:00 2001 From: RobCso Date: Thu, 15 Sep 2022 18:34:50 +0100 Subject: [PATCH 3/3] complete extra --- 3-extra/1-count-words.js | 13 ++++++++++--- testFolder/test1.js | 3 ++- testFolder/test2.js | 36 ++++++++++++++++++++++++++++++++++++ testFolder/test3.js | 16 ++++++++++++++++ testFolder/test4.js | 22 ++++++++++++++++++++++ testFolder/test5.js | 5 +++++ 6 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 testFolder/test2.js create mode 100644 testFolder/test3.js create mode 100644 testFolder/test4.js create mode 100644 testFolder/test5.js diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..5beb4b46 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -25,12 +25,19 @@ function countWords(string) { const wordCount = {}; - + if (string === "") { + return wordCount; + } else { // write code here - + string.split(" ").forEach((word) => { + + // const numberOfOccurrences = string.match(new RegExp(word, "g")).length + if (wordCount[word]) { + wordCount[word] = wordCount[word] +1; + } else wordCount[word] = 1; + })} return wordCount; } - /* ======= 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` diff --git a/testFolder/test1.js b/testFolder/test1.js index c931c083..f17b5b4f 100644 --- a/testFolder/test1.js +++ b/testFolder/test1.js @@ -14,4 +14,5 @@ let cashRegister = { } //cashRegister -console.log(cashRegister.orderBurger(7)) \ No newline at end of file +console.log(cashRegister.orderBurger(7)) + diff --git a/testFolder/test2.js b/testFolder/test2.js new file mode 100644 index 00000000..ea7fec6b --- /dev/null +++ b/testFolder/test2.js @@ -0,0 +1,36 @@ +function countWords(string) { + const wordCount = {}; + if (string === "") { + return wordCount; + } else { + // write code here + string.split(" ").forEach((word) => { + + console.log(word) + const numberOfOccurrences = string.match(new RegExp(word, "g")).length + wordCount[word] = numberOfOccurrences + })} + return wordCount; +} + + +//console.log(myTextObject) + + +let string1 = "you're braver than you believe, stronger than you seem, and smarter than you think"; + +console.log(countWords(string1)) + + +// { +// "you're": 1, +// and: 1, +// "believe,": 1, +// braver: 1, +// "seem,": 1, +// smarter: 1, +// stronger: 1, +// than: 3, +// think: 1, +// you: 3, +// } \ No newline at end of file diff --git a/testFolder/test3.js b/testFolder/test3.js new file mode 100644 index 00000000..d32b7591 --- /dev/null +++ b/testFolder/test3.js @@ -0,0 +1,16 @@ +var firstName = {A: 'Alpha', B: 'Beta', C: 'Cache'} +var surname = {A: 'Analogue', B: 'Bomb', C: 'Catalyst'} + +function aliasGen(firname, sname){ + var hackerName; + var firstFirstName = firname.charAt(0).toUpperCase(); + var firstSurname = sname.charAt(0).toUpperCase(); + if (firstFirstName === firstFirstName.toLowerCase() || firstSurname === firstSurname.toLowerCase()) { + return "Your name must start with a letter from A - Z." + } else { + return hackerName = `${firstName[firstFirstName]} ${surname[firstSurname]}` + } + } + + +console.log(aliasGen("Alex", "Bex")) \ No newline at end of file diff --git a/testFolder/test4.js b/testFolder/test4.js new file mode 100644 index 00000000..e41cd1d1 --- /dev/null +++ b/testFolder/test4.js @@ -0,0 +1,22 @@ +let candidate = { + minSalary: 120000 +} + +let job = { + maxSalary: undefined +} + +function match(candidate, job) { + // is this job a valid match for the candidate? + + if (isNaN(candidate.minSalary) || isNaN(job.maxSalary) === true) { + console.log("error") + } + else if ((candidate.minSalary - (candidate.minSalary*0.1)) <= job.maxSalary) { + + return true + } else return false +} + +console.log(match(candidate, job)) + diff --git a/testFolder/test5.js b/testFolder/test5.js new file mode 100644 index 00000000..ef587e8f --- /dev/null +++ b/testFolder/test5.js @@ -0,0 +1,5 @@ +let string1 = "you're braver than you believe, stronger than you seem, and smarter than you think"; + +let string2= /\byou\b/ + +console.log(string1.match(new RegExp(string2,"g")).length) \ No newline at end of file