diff --git a/week-4/Homework/mandatory/2-writers.js b/week-4/Homework/mandatory/2-writers.js index b1f5d006c..c3859c3da 100644 --- a/week-4/Homework/mandatory/2-writers.js +++ b/week-4/Homework/mandatory/2-writers.js @@ -22,29 +22,29 @@ let writers = [ lastName: "Woolf", occupation: "writer", age: 59, - alive: false + alive: false, }, { firstName: "Zadie", lastName: "Smith", occupation: "writer", age: 41, - alive: true + alive: true, }, { firstName: "Jane", lastName: "Austen", occupation: "writer", age: 41, - alive: false + alive: false, }, { firstName: "Bell", lastName: "Hooks", occupation: "writer", age: 64, - alive: true - } + alive: true, + }, ]; /* @@ -53,8 +53,10 @@ Exercise 1: insert corresponding values to the place holder that are indicated in curly braces: "Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}." */ - - +for (let key in writers) + console.log( + `Hi, my name is ${writers[key].firstName} ${writers[key].lastName}. I am ${writers[key].age} years old, and work as a ${writers[key].occupation}.` + ); /* Exercise 2: @@ -62,11 +64,31 @@ Exercise 2: and not alive anymore. Use the below sentence format: "Writer {firstName} {lastName} died at {age} years old." */ - - +for (let i = 0; i < writers.length; i++) { + if ( + writers[i].age >= 40 && + writers[i].age < 49 && + writers[i].alive === false + ) { + console.log( + `Writer ${writers[i].firstName} ${writers[i].lastName} died at ${writers[i].age} years old.` + ); + } +} /* Exercise 3: Only `console.log()` out alive writers who are in their 40s (meaning between 40 and 49): "Hi, my name is {firstName} {lastName}. I am {age} years old." */ +for (let i = 0; i < writers.length; i++) { + if ( + writers[i].age >= 40 && + writers[i].age < 49 && + writers[i].alive === true + ) { + console.log( + `Hi,my name is ${writers[i].firstName} ${writers[i].lastName}. I am ${writers[i].age} years old.` + ); + } +} diff --git a/week-4/Homework/mandatory/3-water-bottle.js b/week-4/Homework/mandatory/3-water-bottle.js index 4ad72fdd9..4973ff891 100644 --- a/week-4/Homework/mandatory/3-water-bottle.js +++ b/week-4/Homework/mandatory/3-water-bottle.js @@ -20,21 +20,37 @@ You have to implement the missing features according to the specification. // Here is your starting point: let bottle = { volume: 0, - fillUp: function() { + fillUp: function () { + this.volume = 100; // calling this function should pour your bottle full (volume = 100); }, pour: function () { + if (this.volume < 100) { + return (this.volume += 10); + } // calling this function should increase your bottle volume by 10 unit; }, - drink: function() { + drink: function () { + if (this.volume >= 10) { + return (this.volume -= 10); + } // calling this function should decrease your bottle volume by 10 unit; }, isFull: function () { + if (this.volume == 100) return true; + else { + return false; + } + // this function should return true if your bottle is full; + }, + isEmpty: function () { + if (this.volume == 0) { + return true; + } else { + return false; + } // this function should return true if your bottle is empty; }, - isEmpty: function() { - // this function should return true if your bottle is full; - } }; /* @@ -49,7 +65,7 @@ Extra question: Leave your answer below: */ - +// Because if we use `this` keyword and in the future the name of variable changes, it doesn't need to change the name in every single object. // HERE COMES YOUR ANSWER /* @@ -67,14 +83,23 @@ if (bottle.isFull()) console.log(`That's correct! Bottle is full.`); else console.warn(`Not quite right! Bottle should be full but it is not.`); if (!bottle.isEmpty()) console.log(`That's correct! Bottle isn't empty.`); -else console.warn(`Not quite right! Bottle should not be empty but it is already.`); +else + console.warn( + `Not quite right! Bottle should not be empty but it is already.` + ); // ACTIONS bottle.pour(); // CHECKS -if (bottle.volume === 100) console.log(`That's correct. Bottle is already full water volume cannot go beyond.`); -else console.warn(`Whoops!!! Looks like you've changed your bottle to a bigger one, it went beyond its maximum capacity up to ${bottle.volume} unit.`); +if (bottle.volume === 100) + console.log( + `That's correct. Bottle is already full water volume cannot go beyond.` + ); +else + console.warn( + `Whoops!!! Looks like you've changed your bottle to a bigger one, it went beyond its maximum capacity up to ${bottle.volume} unit.` + ); if (bottle.isFull()) console.log(`That's correct! Bottle is still full.`); else console.warn(`Not quite right! Bottle should be still full but is not.`); @@ -85,8 +110,12 @@ bottle.drink(); bottle.drink(); // CHECKS -if (bottle.volume === 70) console.log(`That's correct! Water volume is ${bottle.volume}.`); -else console.warn(`Not quite right! Water volume should be 70 unit instead of ${bottle.volume}.`); +if (bottle.volume === 70) + console.log(`That's correct! Water volume is ${bottle.volume}.`); +else + console.warn( + `Not quite right! Water volume should be 70 unit instead of ${bottle.volume}.` + ); // ACTIONS bottle.drink(); @@ -98,7 +127,10 @@ if (!bottle.isFull()) console.log(`That's correct! Bottle isn't full.`); else console.warn(`Not quite right! Bottle should not be full but it is.`); if (!bottle.isEmpty()) console.log(`That's correct! Bottle isn't empty yet.`); -else console.warn(`Not quite right! Bottle should not be still empty but it is already.`); +else + console.warn( + `Not quite right! Bottle should not be still empty but it is already.` + ); // ACTIONS bottle.drink(); @@ -108,17 +140,28 @@ bottle.drink(); // CHECKS if (bottle.isEmpty()) console.log(`That's correct! Bottle is finally emptied.`); -else console.warn(`Not quite right. Bottle should be already empty but it is not.`); - -if (bottle.volume === 0) console.log(`That's correct! Empty bottle volume is repesented as zero.`); -else console.warn(`Not quite right. Volume should be zero instead of ${bottle.volume}.`); +else + console.warn( + `Not quite right. Bottle should be already empty but it is not.` + ); + +if (bottle.volume === 0) + console.log(`That's correct! Empty bottle volume is repesented as zero.`); +else + console.warn( + `Not quite right. Volume should be zero instead of ${bottle.volume}.` + ); // ACTIONS bottle.drink(); // CHECKS -if (bottle.volume === 0) console.log(`That's correct! Water volume cannot go below zero.`); -else console.warn(`Whoops!!! Looks like your water volume went negative. Your water volume is ${bottle.volume} unit.`); +if (bottle.volume === 0) + console.log(`That's correct! Water volume cannot go below zero.`); +else + console.warn( + `Whoops!!! Looks like your water volume went negative. Your water volume is ${bottle.volume} unit.` + ); if (bottle.isEmpty()) console.log(`That's correct! Bottle is still empty.`); else console.warn(`Not quite right. Bottle should be empty but it is not.`); @@ -127,18 +170,28 @@ else console.warn(`Not quite right. Bottle should be empty but it is not.`); bottle.pour(); // CHECKS -if (bottle.volume === 10) console.log(`That's correct! Water volume is ${bottle.volume}.`); -else console.warn(`Not quite right! Water volume should be 10 unit instead of ${bottle.volume}.`); +if (bottle.volume === 10) + console.log(`That's correct! Water volume is ${bottle.volume}.`); +else + console.warn( + `Not quite right! Water volume should be 10 unit instead of ${bottle.volume}.` + ); if (!bottle.isFull()) console.log(`That's correct! Bottle isn't yet full.`); else console.warn(`Not quite right! Bottle should not be full but it is.`); -if (!bottle.isEmpty()) console.log(`That's correct! Bottle isn't empty anymore.`); -else console.warn(`Not quite right! Bottle should not be empty again but it is still.`); +if (!bottle.isEmpty()) + console.log(`That's correct! Bottle isn't empty anymore.`); +else + console.warn( + `Not quite right! Bottle should not be empty again but it is still.` + ); // ACTIONS bottle.drink(); // CHECKS -if (bottle.isEmpty()) console.log(`That's correct! Bottle is emptied once more.`); -else console.warn(`Not quite right. Bottle should be empty again but it is not.`); +if (bottle.isEmpty()) + console.log(`That's correct! Bottle is emptied once more.`); +else + console.warn(`Not quite right. Bottle should be empty again but it is not.`); diff --git a/week-4/Homework/mandatory/4-groceries.js b/week-4/Homework/mandatory/4-groceries.js index 09a69fee3..63347092e 100644 --- a/week-4/Homework/mandatory/4-groceries.js +++ b/week-4/Homework/mandatory/4-groceries.js @@ -11,7 +11,7 @@ that contains the missing ingredients to your menus. It is stored in the "weekly Complete the exercises below. */ -// Here is your +// Here is your let weeklyMealPlan = { monday: ["Cheese", "Eggs", "Tomato", "Paprika", "Leek"], tuesday: ["Wrap", "Tuna", "Canned beans", "Cheese", "Carrot", "Aubergine"], @@ -19,7 +19,7 @@ let weeklyMealPlan = { thursday: ["Lamb", "Salt", "Bulgur", "Potato"], fridray: ["Rice milk", "Blueberries", "Porridge", "Banana", "Cinnamon"], saturday: ["Olive oil", "Potato", "Salmon", "Asparagus"], - sunday: [] + sunday: [], }; /* @@ -29,8 +29,8 @@ Exercise 1: */ // Gather all week item names into this array let weeklyGroceriesToBuy = []; - - +weeklyGroceriesToBuy = Object.values(weeklyMealPlan); +console.log(weeklyGroceriesToBuy); /* Exercise 2: @@ -39,9 +39,11 @@ Exercise 2: */ // Gather weekend item names into this array let weekendGroceriesToBuy = []; - - - +weekendGroceriesToBuy = Object.values( + weeklyMealPlan.saturday, + weeklyMealPlan.sunday +); +console.log(weekendGroceriesToBuy); /* Exercise 2: Loop through your weekly meal plan: @@ -50,7 +52,8 @@ Exercise 2: Finally use console.log() to print out the Object. */ -// Gather weekend item names into this object +//Gather weekend item names into this array + let numberOfItemsPerWeak = { monday: 0, tuesday: 0, @@ -58,5 +61,9 @@ let numberOfItemsPerWeak = { thursday: 0, fridray: 0, saturday: 0, - sunday: 0 + sunday: 0, }; +for (let day in weeklyMealPlan) { + numberOfItemsPerWeak[day] = weeklyMealPlan[day].length; +} +console.log(numberOfItemsPerWeak); diff --git a/week-4/InClass/A-objects-intro/exercise-part-0.js b/week-4/InClass/A-objects-intro/exercise-part-0.js index 58d6d28e7..8cfc8b2f3 100644 --- a/week-4/InClass/A-objects-intro/exercise-part-0.js +++ b/week-4/InClass/A-objects-intro/exercise-part-0.js @@ -4,4 +4,13 @@ Describe your own laptop as a JavaScript object Try to think of as many properties as you can! -*/ \ No newline at end of file +*/ +let laptop = { + Brand: "Dell", + Os: "Linux", + screenSize: 14, + cpu: "Core I 5", + Hdd: 256, + color: "Black", +}; +console.log(laptop); diff --git a/week-4/InClass/A-objects-intro/exercise-part-1.js b/week-4/InClass/A-objects-intro/exercise-part-1.js index 44cdf0ee0..775bf62f7 100644 --- a/week-4/InClass/A-objects-intro/exercise-part-1.js +++ b/week-4/InClass/A-objects-intro/exercise-part-1.js @@ -7,3 +7,36 @@ Assign each of them to a separate variable */ +let mobilePhone = { + Brand: "Iphone", + screenSize: "5.8", + Model: "7 plus", + capacity: "128 GB", +}; +let tv = { + Brand: "Samsung", + screenSize: "55 inches", + color: "Silver", + smart: true, + preInstaldedApps: true, +}; +let sofa = { + Brand: "DFS", + "How Many Seaters": 7, + color: "Teal", + material: "Fabric", +}; +let house = { + "Number of Bedrooms": 2, + Flat: true, + house: false, + level: 2, + furnished: true, +}; +let fridge = { + Brand: "Hot Point", + color: "Black", + capacity: "230 Liter", + "Fridge Freezer": true, +}; +console.log(fridge); diff --git a/week-4/InClass/A-objects-intro/exercise-part-2.js b/week-4/InClass/A-objects-intro/exercise-part-2.js index 356828587..23a9f5a0b 100644 --- a/week-4/InClass/A-objects-intro/exercise-part-2.js +++ b/week-4/InClass/A-objects-intro/exercise-part-2.js @@ -5,17 +5,18 @@ The objects below have some syntax issues - try and fix them all! */ let kitten = { - fur colour: "orange", - age "23" + "fur colour": "orange", + age: 23, }; -let laptop = - brand: "Lenovo" - ram "5GB" -} +let laptop = { + brand: "Lenovo", + ram: "5 GB", +}; let phone = { - operating system "iOS", - hasStylus: true, - megapixels 12 - "batteryLife": "24 hours" \ No newline at end of file + "operating system": "iOS", + "has Stylus": true, + "mega pixels": 12, + "battery Life": "24 hours", +}; diff --git a/week-4/InClass/B-objects-get-set/exercise-1.js b/week-4/InClass/B-objects-get-set/exercise-1.js index 4fd71e60b..9bb642ceb 100644 --- a/week-4/InClass/B-objects-get-set/exercise-1.js +++ b/week-4/InClass/B-objects-get-set/exercise-1.js @@ -3,16 +3,15 @@ */ let kitten = { - ageMonths: 3, - isFemale: true, - furColour: "brown" + ageMonths: 3, + isFemale: true, + furColour: "brown", }; // YOUR CODE GOES BELOW HERE +console.log(kitten.ageMonths); +console.log(kitten.isFemale); +console.log(kitten.furColour); - - - - -// YOUR CODE GOES ABOVE HERE \ No newline at end of file +// YOUR CODE GOES ABOVE HERE diff --git a/week-4/InClass/B-objects-get-set/exercise-2.js b/week-4/InClass/B-objects-get-set/exercise-2.js index efdb092b1..588805729 100644 --- a/week-4/InClass/B-objects-get-set/exercise-2.js +++ b/week-4/InClass/B-objects-get-set/exercise-2.js @@ -5,14 +5,14 @@ */ let phone = { - brand: 'iPhone, - model 'iPhone X' - launchYear: 2017, - is Unlocked: true -; + brand: "iPhone", + model: "iPhone X", + launchYear: 2017, + isUnlocked: true, +}; -let phoneBrand = phone.bbrand; -let phoneLaunchYear = phone[launchYear]; +let phoneBrand = phone.brand; +let phoneLaunchYear = phone["launchYear"]; // DO NOT MODIFY BELOW THIS LINE @@ -21,4 +21,4 @@ console.log(phoneLaunchYear); // it should output: // iPhone -// 2017 \ No newline at end of file +// 2017 diff --git a/week-4/InClass/B-objects-get-set/exercise-3.js b/week-4/InClass/B-objects-get-set/exercise-3.js index 1f057083b..541c8c6f3 100644 --- a/week-4/InClass/B-objects-get-set/exercise-3.js +++ b/week-4/InClass/B-objects-get-set/exercise-3.js @@ -3,9 +3,11 @@ */ // WRITE CODE BELOW THIS - +let kitten = { + name: "Gilbert", +}; // WRITE CODE ABOVE THIS console.log(kitten.name); -// -> it should output: "Gilbert" \ No newline at end of file +// -> it should output: "Gilbert" diff --git a/week-4/InClass/B-objects-get-set/exercise-4.js b/week-4/InClass/B-objects-get-set/exercise-4.js index 4947cb50f..fac3fcce0 100644 --- a/week-4/InClass/B-objects-get-set/exercise-4.js +++ b/week-4/InClass/B-objects-get-set/exercise-4.js @@ -3,21 +3,21 @@ */ let dog = { - name: 'Billy', - wantsToPlay: false + name: "Billy", + wantsToPlay: false, }; // WRITE CODE BELOW THIS LINE - +dog.name = "Rex"; +dog.wantsToPlay = true; // WRITE CODE ABOVE THIS LINE - //DO NOT MODIFY BELOW console.log(dog.name); console.log(dog.wantsToPlay); // it should output: // Rex -// true \ No newline at end of file +// true diff --git a/week-4/InClass/C-more-complex-objects/exercise-1.js b/week-4/InClass/C-more-complex-objects/exercise-1.js index ae8fea487..fcf891992 100644 --- a/week-4/InClass/C-more-complex-objects/exercise-1.js +++ b/week-4/InClass/C-more-complex-objects/exercise-1.js @@ -5,12 +5,12 @@ */ let house = { - address: "1 Kinning Park", - previousOwners: ["Claire M.", "John A."], - currentOwner: { - firstName: "Margaret", - lastName: "Conway" - } + address: "1 Kinning Park", + previousOwners: ["Claire M.", "John A."], + currentOwner: { + firstName: "Margaret", + lastName: "Conway", + }, }; /* @@ -20,14 +20,23 @@ let house = { */ // - change the address of "house" to '51 Berkley Road' +house.address = "51 Berkley Road"; // - change the previous owners of "house" to ["Brian M.", "Fiona S."] +house.previousOwners = ["Brian M.", "Fiona S."]; // - change the last name of the current owner of "house" to "Montgomery" - +house.currentOwner.lastName = "Montgomery"; /* DO NOT EDIT ANYTHING BELOW THIS LINE */ -console.log("Expected result: 51 Berkley Road. Actual result: " + house.address); -console.log("Expected result: Brian M., Fiona S. Actual result: " + house.previousOwners.toString()); -console.log("Expected result: Montgomery. Actual result: " + house.currentOwner.lastName); \ No newline at end of file +console.log( + "Expected result: 51 Berkley Road. Actual result: " + house.address +); +console.log( + "Expected result: Brian M., Fiona S. Actual result: " + + house.previousOwners.toString() +); +console.log( + "Expected result: Montgomery. Actual result: " + house.currentOwner.lastName +); diff --git a/week-4/InClass/C-more-complex-objects/exercise-2.js b/week-4/InClass/C-more-complex-objects/exercise-2.js index 69b3768aa..3677b6b98 100644 --- a/week-4/InClass/C-more-complex-objects/exercise-2.js +++ b/week-4/InClass/C-more-complex-objects/exercise-2.js @@ -6,17 +6,17 @@ */ let house = { - address: "1 Kinning Park", - previousOwners: ["Claire M.", "John A."], - currentOwner: { - firstName: "Margaret", - lastName: "Conway" - } + address: "1 Kinning Park", + previousOwners: ["Claire M.", "John A."], + currentOwner: { + firstName: "Margaret", + lastName: "Conway", + }, }; let newCurrentOwner = { - firstName: "Georgina", - lastName: "Hernandez" + firstName: "Georgina", + lastName: "Hernandez", }; /* @@ -26,17 +26,22 @@ let newCurrentOwner = { */ // - assign the value of the variable 'newCurrentOwner' as the value to the house's "currentOwner" +house.currentOwner = newCurrentOwner; // - from the list of previous owners, replace only "John A." with "Stephen B." +house.previousOwners[1] = "Stephen B."; // - give the house a new property called 'isForSale' with the value 'false' - - - +house.isForSale = false; /* DO NOT EDIT ANYTHING BELOW THIS LINE */ console.log( - "Did you correctly assign the new owner using the given variable?", - `Expected result: true. Actual result: ${(house.currentOwner === newCurrentOwner)}`); -console.log(`Expected result: Claire M., Stephen B.Actual result: ${house.previousOwners.toString()}`); -console.log(`Expected result: false.Actual result: ${house.isForSale}`); \ No newline at end of file + "Did you correctly assign the new owner using the given variable?", + `Expected result: true. Actual result: ${ + house.currentOwner === newCurrentOwner + }` +); +console.log( + `Expected result: Claire M., Stephen B.Actual result: ${house.previousOwners.toString()}` +); +console.log(`Expected result: false.Actual result: ${house.isForSale}`); diff --git a/week-4/InClass/C-more-complex-objects/exercise-3.js b/week-4/InClass/C-more-complex-objects/exercise-3.js index f461e7d64..fd7fdccae 100644 --- a/week-4/InClass/C-more-complex-objects/exercise-3.js +++ b/week-4/InClass/C-more-complex-objects/exercise-3.js @@ -5,23 +5,23 @@ */ let kinningParkHouse = { - address: "1 Kinning Park", - price: 180000, - currentOwner: { - firstName: "Margaret", - lastName: "Conway", - email: "margaret@fake-emails.com" - } + address: "1 Kinning Park", + price: 180000, + currentOwner: { + firstName: "Margaret", + lastName: "Conway", + email: "margaret@fake-emails.com", + }, }; let parkAvenueHouse = { - address: "50 Park Avenue", - price: 195000, - currentOwner: { - firstName: "Marie", - lastName: "McDonald", - email: "marie.m@real-emails.com" - } + address: "50 Park Avenue", + price: 195000, + currentOwner: { + firstName: "Marie", + lastName: "McDonald", + email: "marie.m@real-emails.com", + }, }; /* @@ -32,23 +32,39 @@ let parkAvenueHouse = { // returns the full name (first name + last name) of the owner of the house function getOwnerFullName(house) { - + return `${kinningParkHouse.currentOwner.firstName} ${kinningParkHouse.currentOwner.lastName}`; } - // returns an array of the owners' email addresses of the two houses function getEmailAddresses(house1, house2) { - + return `${kinningParkHouse.currentOwner.email} , ${parkAvenueHouse.currentOwner.email}`; } // returns the address for the cheapest house out of the two function getCheapestAddress(house1, house2) { - + if (kinningParkHouse.price < parkAvenueHouse.price) { + return kinningParkHouse.address; + } else { + return parkAvenueHouse.address; + } } - /* DO NOT EDIT ANYTHING BELOW THIS LINE */ -console.log(`Expected result: Margaret Conway. Actual result: ${getOwnerFullName(kinningParkHouse)}`); -console.log(`Expected result: margaret@fake-emails.com, marie.m@real-emails.com. Actual result: ${getEmailAddresses(kinningParkHouse, parkAvenueHouse)}`); -console.log(`Expected result: 1 Kinning Park. Actual result: ${getCheapestAddress(parkAvenueHouse, kinningParkHouse)}`); \ No newline at end of file +console.log( + `Expected result: Margaret Conway. Actual result: ${getOwnerFullName( + kinningParkHouse + )}` +); +console.log( + `Expected result: margaret@fake-emails.com, marie.m@real-emails.com. Actual result: ${getEmailAddresses( + kinningParkHouse, + parkAvenueHouse + )}` +); +console.log( + `Expected result: 1 Kinning Park. Actual result: ${getCheapestAddress( + parkAvenueHouse, + kinningParkHouse + )}` +); diff --git a/week-4/InClass/D-methods/exercise-1.js b/week-4/InClass/D-methods/exercise-1.js index 8de0f8c30..30a7521a9 100644 --- a/week-4/InClass/D-methods/exercise-1.js +++ b/week-4/InClass/D-methods/exercise-1.js @@ -3,16 +3,18 @@ A person named Alice is defined below. Add a method "greet" so this person can say hello. */ - let person = { - name: "Alice", - age: 25 + name: "Alice", + age: 25, }; - - +function greet() { + return "say hello"; +} +person.greet = greet; /* DO NOT EDIT ANYTHING BELOW THIS LINE */ - -console.log(`Expected result: Hello everybody. Actual result: ${person.greet()}`); \ No newline at end of file +console.log( + `Expected result: Hello everybody. Actual result: ${person.greet()}` +); diff --git a/week-4/InClass/D-methods/exercise-2.js b/week-4/InClass/D-methods/exercise-2.js index 8e993fc69..f5141e5bb 100644 --- a/week-4/InClass/D-methods/exercise-2.js +++ b/week-4/InClass/D-methods/exercise-2.js @@ -4,15 +4,18 @@ Add a method "sayName" so this person can say their own name. Hint: use 'this' keyword to access the name property. */ - let person = { - name: "Alice", - age: 25 + name: "Alice", + age: 25, }; - - +function sayName() { + return `My name is ${this.name}`; +} +person.sayName = sayName; /* DO NOT EDIT ANYTHING BELOW THIS LINE */ -console.log(`Expected result: 'My name is Alice'. Actual result: ${person.sayName()}`); \ No newline at end of file +console.log( + `Expected result: 'My name is Alice'. Actual result: ${person.sayName()}` +); diff --git a/week-4/InClass/D-methods/exercise-3.js b/week-4/InClass/D-methods/exercise-3.js index be237483b..7aa196127 100644 --- a/week-4/InClass/D-methods/exercise-3.js +++ b/week-4/InClass/D-methods/exercise-3.js @@ -3,26 +3,26 @@ The following code contains syntax errors - try and fix them! Once you fix them, run this file, it should output the correct values! */ - let person = { - name: "Alice", - age: 25, - currentAddress: "Glasgow", - changeAddress: (newAddress) { - currentAddress = newAddress; - }, - celebrateBirthday: function { - that.age = that.age + 1; - } + name: "Alice", + age: 25, + currentAddress: "Glasgow", + changeAddress: function (newAddress) { + return (this.currentAddress = newAddress); + }, + celebrateBirthday: function () { + this.age = this.age + 1; + }, }; - /* DO NOT EDIT ANYTHING BELOW THIS LINE */ person.changeAddress("Edinburgh"); -console.log(`Expected result: Edinburgh. Actual result: ${person.currentAddress}`); +console.log( + `Expected result: Edinburgh. Actual result: ${person.currentAddress}` +); person.celebrateBirthday(); console.log(`Expected result: 26. Actual result: ${person.age}`); diff --git a/week-4/InClass/D-methods/exercise-4.js b/week-4/InClass/D-methods/exercise-4.js index d89214a72..77b9335a9 100644 --- a/week-4/InClass/D-methods/exercise-4.js +++ b/week-4/InClass/D-methods/exercise-4.js @@ -3,17 +3,21 @@ Alice has a list of good friends. Define a method "makeFriend" to add a new friend to her list. */ - let person = { - name: "Alice", - friends: ["John", "Nina"] + name: "Alice", + friends: ["John", "Nina"], }; - +function makeFriend(newFriend) { + return this.friends.push(newFriend); +} +person.makeFriend = makeFriend; /* DO NOT EDIT ANYTHING BELOW THIS LINE */ person.makeFriend("Bob"); -console.log(`Expected result: 'John,Nina,Bob'. Actual result: ${person.friends}`); \ No newline at end of file +console.log( + `Expected result: 'John,Nina,Bob'. Actual result: ${person.friends}` +); diff --git a/week-4/InClass/D-methods/exercise-5.js b/week-4/InClass/D-methods/exercise-5.js index dcd198c47..70950cb86 100644 --- a/week-4/InClass/D-methods/exercise-5.js +++ b/week-4/InClass/D-methods/exercise-5.js @@ -1,6 +1,6 @@ /* A coffee machine is defined below. -One can buy three different coffees. +One person can buy three different coffees. Complete the methods "insertMoney" and "getCoffee" to match the expected result. insertMoney takes an amount in parameter to add money in the coffee machine. @@ -9,35 +9,53 @@ only if the inserted amount is greater or equal than the price of the coffee! */ let coffeeMachine = { - brand: "Super Coffee", - prices: { - cappuccino: 2.40, - blackCoffee: 1.50, - flatWhite: 3.00 - }, - insertedAmount: 0, - insertMoney: function (amount) { - - }, - getCoffee: function (coffee) { - + brand: "Super Coffee", + prices: { + cappuccino: 2.4, + blackCoffee: 1.5, + flatWhite: 3.0, + }, + insertedAmount: 0, + insertMoney: function (amount) { + this.insertedAmount = amount; + }, + getCoffee: function (coffee) { + if (this.insertedAmount >= coffeeMachine.prices[coffee]) { + return `Please take your ${coffee}`; + } else { + return `Sorry you don't have enough money for a ${coffee}`; } + }, }; - /* DO NOT EDIT ANYTHING BELOW THIS LINE */ -coffeeMachine.insertMoney(2.40); -console.log(`Expected result: 'Please take your cappuccino'. Actual result: ${coffeeMachine.getCoffee('cappuccino')}`); - -coffeeMachine.insertMoney(1.50); -console.log(`Expected result: 'Please take your blackCoffee'. Actual result: ${coffeeMachine.getCoffee('blackCoffee')}`); - -coffeeMachine.insertMoney(4.00); -console.log(`Expected result: 'Please take your flatWhite'. Actual result: ${coffeeMachine.getCoffee('flatWhite')}`); - -coffeeMachine.insertMoney(2.40); -console.log(`Expected result: 'Sorry you don't have enough money for a flatWhite'. Actual result: ${coffeeMachine.getCoffee('flatWhite')}`); - +coffeeMachine.insertMoney(2.4); +console.log( + `Expected result: 'Please take your cappuccino'. Actual result: ${coffeeMachine.getCoffee( + "cappuccino" + )}` +); + +coffeeMachine.insertMoney(1.5); +console.log( + `Expected result: 'Please take your blackCoffee'. Actual result: ${coffeeMachine.getCoffee( + "blackCoffee" + )}` +); + +coffeeMachine.insertMoney(4.0); +console.log( + `Expected result: 'Please take your flatWhite'. Actual result: ${coffeeMachine.getCoffee( + "flatWhite" + )}` +); + +coffeeMachine.insertMoney(2.4); +console.log( + `Expected result: 'Sorry you don't have enough money for a flatWhite'. Actual result: ${coffeeMachine.getCoffee( + "flatWhite" + )}` +); diff --git a/week-4/InClass/E-arrays-of-objects/exercise-1.js b/week-4/InClass/E-arrays-of-objects/exercise-1.js index 8d39a8154..7ce7e4985 100644 --- a/week-4/InClass/E-arrays-of-objects/exercise-1.js +++ b/week-4/InClass/E-arrays-of-objects/exercise-1.js @@ -5,18 +5,18 @@ */ var person1 = { - name: "Alice", - age: 25 + name: "Alice", + age: 25, }; var person2 = { - name: "Bob", - age: 30 + name: "Bob", + age: 30, }; var person3 = { - name: "John", - age: 20 + name: "John", + age: 20, }; /* @@ -24,23 +24,37 @@ DO NOT EDIT ANYTHING ABOVE THIS LINE WRITE YOUR CODE BELOW */ +var persons = [person1, person2, person3]; // Complete here -var persons = // Complete here - -var personNames = // Complete here - -var personsYoungerThan28YearsOld = // Complete here - +var personNames = persons.map((names) => names.name); // Complete here +var personsYoungerThan28YearsOld = persons.filter( + (personsYoungerThan28YearsOld) => personsYoungerThan28YearsOld.age < 28 +); // Complete here /* DO NOT EDIT ANYTHING BELOW THIS LINE */ -console.log("Question 1: array defined with 3 persons -> ", - (persons[0] === person1 && persons[1] === person2 && persons[2] === person3) ? 'Passed :)' : 'Not yet :('); - -console.log("Question 2: array containing the person names -> ", - (personNames[0] === "Alice" && personNames[1] === "Bob" && personNames[2] === "John") ? 'Passed :)' : 'Not yet :('); - -console.log("Question 3: array containing the persons younger than 28 years old -> ", - (personsYoungerThan28YearsOld[0] === person1 && personsYoungerThan28YearsOld[1] === person3) ? 'Passed :)' : 'Not yet :('); +console.log( + "Question 1: array defined with 3 persons -> ", + persons[0] === person1 && persons[1] === person2 && persons[2] === person3 + ? "Passed :)" + : "Not yet :(" +); + +console.log( + "Question 2: array containing the person names -> ", + personNames[0] === "Alice" && + personNames[1] === "Bob" && + personNames[2] === "John" + ? "Passed :)" + : "Not yet :(" +); + +console.log( + "Question 3: array containing the persons younger than 28 years old -> ", + personsYoungerThan28YearsOld[0] === person1 && + personsYoungerThan28YearsOld[1] === person3 + ? "Passed :)" + : "Not yet :(" +); diff --git a/week-4/InClass/E-arrays-of-objects/exercise-2.js b/week-4/InClass/E-arrays-of-objects/exercise-2.js index c2259dd23..f11395b4b 100644 --- a/week-4/InClass/E-arrays-of-objects/exercise-2.js +++ b/week-4/InClass/E-arrays-of-objects/exercise-2.js @@ -7,50 +7,63 @@ Each destination has a name, a distance from Glasgow, and a list of transportati 3) Find all the destination names that are both more than 300 kms far away and reachable by train. */ - let destination1 = { - destinationName: "Edinburgh", - distanceKms: 80, - transportations: ["car", "bus", "train"] + destinationName: "Edinburgh", + distanceKms: 80, + transportations: ["car", "bus", "train"], }; let destination2 = { - destinationName: "London", - distanceKms: 650, - transportations: ["car", "bus", "train"] + destinationName: "London", + distanceKms: 650, + transportations: ["car", "bus", "train"], }; let destination3 = { - destinationName: "Paris", - distanceKms: 900, - transportations: ["train", "plane"] + destinationName: "Paris", + distanceKms: 900, + transportations: ["train", "plane"], }; let destination4 = { - destinationName: "Dublin", - distanceKms: 350, - transportations: ["plane", "ferry"] + destinationName: "Dublin", + distanceKms: 350, + transportations: ["plane", "ferry"], }; -let travelDestinations = [destination1, destination2, destination3, destination4]; +let travelDestinations = [ + destination1, + destination2, + destination3, + destination4, +]; /* DO NOT EDIT ANYTHING ABOVE THIS LINE WRITE YOUR CODE BELOW */ +let destinationNamesWithin500Kms = travelDestinations + .filter((destination) => destination.distanceKms < 500) + .map((a) => a.destinationName); // Complete here -let destinationNamesWithin500Kms = // Complete here - -let destinationNameReachableByFerry = // Complete here - -let destinationNamesMoreThan300KmsAwayByTrain = // Complete here (PRINT THE RESULT IN THE CONSOLE USING FOREACH) - +let destinationNameReachableByFerry = travelDestinations + .filter((destination) => destination.transportations.includes("ferry")) + .map((a) => a.destinationName); // Complete here +let destinationNamesMoreThan300KmsAwayByTrain = travelDestinations + .filter((a) => a.distanceKms > 300 && a.transportations.includes("train")) + .map((b) => b.destinationName); // Complete here (PRINT THE RESULT IN THE CONSOLE USING FOREACH) /* DO NOT EDIT ANYTHING BELOW THIS LINE */ -console.log(`Question 1) Expected result: Edinburgh,Dublin, actual result: ${destinationNamesWithin500Kms}`); -console.log(`Question 2) Expected result: Dublin, actual result: ${destinationNameReachableByFerry}`); -console.log(`Question 3) Expected result: London,Paris, actual result: ${destinationNamesMoreThan300KmsAwayByTrain}`); +console.log( + `Question 1) Expected result: Edinburgh,Dublin, actual result: ${destinationNamesWithin500Kms}` +); +console.log( + `Question 2) Expected result: Dublin, actual result: ${destinationNameReachableByFerry}` +); +console.log( + `Question 3) Expected result: London,Paris, actual result: ${destinationNamesMoreThan300KmsAwayByTrain}` +); diff --git a/week-4/InClass/E-arrays-of-objects/exercise-3.js b/week-4/InClass/E-arrays-of-objects/exercise-3.js index a1ec6916f..9564e0a87 100644 --- a/week-4/InClass/E-arrays-of-objects/exercise-3.js +++ b/week-4/InClass/E-arrays-of-objects/exercise-3.js @@ -15,36 +15,36 @@ and returns the number of restaurants in this area. */ let restaurant1 = { - name: "Paesano", - totalSeats: 10, - numberOfCustomers: 8, - address: { - city: "Glasgow", - area: "center" - }, - menu: ["pizza", "calzone", "salad"] + name: "Paesano", + totalSeats: 10, + numberOfCustomers: 8, + address: { + city: "Glasgow", + area: "center", + }, + menu: ["pizza", "calzone", "salad"], }; let restaurant2 = { - name: "Ubiquitous Chip", - totalSeats: 20, - numberOfCustomers: 10, - address: { - city: "Glasgow", - area: "west" - }, - menu: ["salad", "chocolate cake", "roast lamb"] + name: "Ubiquitous Chip", + totalSeats: 20, + numberOfCustomers: 10, + address: { + city: "Glasgow", + area: "west", + }, + menu: ["salad", "chocolate cake", "roast lamb"], }; let restaurant3 = { - name: "Monkeyz", - totalSeats: 15, - numberOfCustomers: 8, - address: { - city: "Glasgow", - area: "center" - }, - menu: ["stew", "chocolate cake", "panini"] + name: "Monkeyz", + totalSeats: 15, + numberOfCustomers: 8, + address: { + city: "Glasgow", + area: "center", + }, + menu: ["stew", "chocolate cake", "panini"], }; let restaurants = [restaurant1, restaurant2, restaurant3]; @@ -54,32 +54,48 @@ DO NOT EDIT ANYTHING ABOVE THIS LINE WRITE YOUR CODE BELOW */ - let restaurantFinderApplication = { - applicationName: "Restaurant Finder", - applicationVersion: "1.0", - restaurants: restaurants, - findAvailableRestaurants: function (numberOfPeople) { - // Complete here - }, - findRestaurantServingDish: function (dishName) { - // Complete here - }, - countNumberOfRestaurantsInArea: function (area) { - // Complete here - } + applicationName: "Restaurant Finder", + applicationVersion: "1.0", + restaurants: restaurants, + findAvailableRestaurants: function (numberOfPeople) { + let findResturant = restaurants + .filter((b) => b.totalSeats - b.numberOfCustomers >= numberOfPeople) + .map((a) => a.name); // Complete here + return findResturant; + }, + findRestaurantServingDish: function (dishName) { + return restaurants + .filter((a) => a.menu.includes(dishName)) + .map((b) => b.name); + // Complete here + }, + countNumberOfRestaurantsInArea: function (area) { + return restaurants.filter((a) => a.address.area.includes(area)).length; // Complete here + }, }; - /* DO NOT EDIT ANYTHING BELOW THIS LINE */ -let restaurantsAvailableFor5People = restaurantFinderApplication.findAvailableRestaurants(5); -console.log(`Find available restaurants for 5 people: Expected result: Ubiquitous Chip,Monkeyz, actual result: ${restaurantsAvailableFor5People}`); - -let restaurantsServingSalad = restaurantFinderApplication.findRestaurantServingDish("salad"); -console.log(`Find restaurants serving salad: Expected result: Paesano,Ubiquitous Chip, actual result: ${restaurantsServingSalad}`); - -let numberOfRestaurantsInCityCentre = restaurantFinderApplication.countNumberOfRestaurantsInArea("center"); -console.log(`Number of restaurants in city centre: Expected result: 2, actual result: ${numberOfRestaurantsInCityCentre}`); +let restaurantsAvailableFor5People = restaurantFinderApplication.findAvailableRestaurants( + 5 +); +console.log( + `Find available restaurants for 5 people: Expected result: Ubiquitous Chip,Monkeyz, actual result: ${restaurantsAvailableFor5People}` +); + +let restaurantsServingSalad = restaurantFinderApplication.findRestaurantServingDish( + "salad" +); +console.log( + `Find restaurants serving salad: Expected result: Paesano,Ubiquitous Chip, actual result: ${restaurantsServingSalad}` +); + +let numberOfRestaurantsInCityCentre = restaurantFinderApplication.countNumberOfRestaurantsInArea( + "center" +); +console.log( + `Number of restaurants in city centre: Expected result: 2, actual result: ${numberOfRestaurantsInCityCentre}` +); diff --git a/week-4/InClass/F-object-keys/exercise-part-0.js b/week-4/InClass/F-object-keys/exercise-part-0.js index d9b10855b..e38b55d32 100644 --- a/week-4/InClass/F-object-keys/exercise-part-0.js +++ b/week-4/InClass/F-object-keys/exercise-part-0.js @@ -5,28 +5,28 @@ Return the keys of the following object */ let capitalCities = { - scotland: 'Edinburgh', - kenya: 'Nairobi', - australia: 'Canberra', - canada: 'Ottawa' + scotland: "Edinburgh", + kenya: "Nairobi", + australia: "Canberra", + canada: "Ottawa", }; let highScores = { - 55: 'Alistair', - 100: 'David', - 89: 'Hannah', - 34: ['Sergi', 'Frank',] + 55: "Alistair", + 100: "David", + 89: "Hannah", + 34: ["Sergi", "Frank"], }; // ONLY EDIT BELOW HERE -let capitalCitiesKeys = ; -let highScoresKeys; +let capitalCitiesKeys = Object.keys(capitalCities); +let highScoresKeys = Object.keys(highScores); // ONLY EDIT ABOVE HERE console.log(capitalCitiesKeys); // prints [ 'scotland', 'kenya', 'australia', 'canada' ] -console.log(highScoresKeys) +console.log(highScoresKeys); // prints ['34, '55', '89', '100'] diff --git a/week-4/InClass/F-object-keys/exercise-part-1.js b/week-4/InClass/F-object-keys/exercise-part-1.js index b8d4be714..7b58171cd 100644 --- a/week-4/InClass/F-object-keys/exercise-part-1.js +++ b/week-4/InClass/F-object-keys/exercise-part-1.js @@ -10,14 +10,14 @@ let mentorsAges = { james: 29, JOSH: 35, JAMIE: 25, - Mozafar: 30 + Mozafar: 30, }; // ONLY EDIT BELOW THIS LINE -let mentorsNames = ; +let mentorsNames = Object.keys(mentorsAges); -let mentorsNamedUppercased = ; +let mentorsNamedUppercased = mentorsNames.map((a) => a.toUpperCase()); // ONLY EDIT ABOVE THIS LINE diff --git a/week-4/InClass/F-object-keys/exercise-part-2.js b/week-4/InClass/F-object-keys/exercise-part-2.js index 0c1571253..23f5c919a 100644 --- a/week-4/InClass/F-object-keys/exercise-part-2.js +++ b/week-4/InClass/F-object-keys/exercise-part-2.js @@ -9,40 +9,38 @@ Use the provided console.log statements below and follow the instructions above */ let storeBranches = { - glasgow: { - manager: 'Andrew', - assistant: 'Laura', + manager: "Andrew", + assistant: "Laura", interns: { - head_intern: 'Mozafar', - intern: 'James' - } + head_intern: "Mozafar", + intern: "James", + }, }, edinburgh: { - director: 'Kelly', - manager: 'Sally', - assistant: 'Derek', + director: "Kelly", + manager: "Sally", + assistant: "Derek", interns: { - head_intern: 'John', - intern: 'Sarah' - } - } -} - + head_intern: "John", + intern: "Sarah", + }, + }, +}; // ONLY EDIT BELOW THIS LINE -// # 1 get the names of the cities +// # 1 get the names of the cities // prints [ 'glasgow', 'edinburgh' ] -console.log() +console.log(Object.keys(storeBranches)); // # 2 get the positions in Glasgow // prints [ 'manager', 'assistant', 'interns' ] -console.log() +console.log(Object.keys(storeBranches.glasgow)); // # 3 get the positions for internt in Glasgow // prints [ 'head_intern', 'intern' ] -console.log() +console.log(Object.keys(storeBranches.glasgow.interns)); // ONLY EDIT ABOVE THIS LINE diff --git a/week-4/InClass/forinLoop.js b/week-4/InClass/forinLoop.js index 1083c9e84..e065035f2 100644 --- a/week-4/InClass/forinLoop.js +++ b/week-4/InClass/forinLoop.js @@ -16,8 +16,12 @@ var UKBigCitiesInMillions = { //1- We discovered a small error in the calculations, we need to add 200 thousdands to each city under 1 million //create a loop that write the names of the city over 1 million only to the console // Example : "The city of x has a popluation of 1.5 million" -for (let city in UKBigCitiesInMillions) { -} +// for (let city in UKBigCitiesInMillions) { +// if (UKBigCitiesInMillions[city] < 1) return UKBigCitiesInMillions[city] + 0.2; +// console.log( +// `The city of ${city} has a popluation of ${UKBigCitiesInMillions[city]} million` +// ); +// } //2-We need to know in which area each city is //we looking for an output like "x is in Scotland and has population of y millions" @@ -27,4 +31,17 @@ var England = ["Manchester", "Birmingham", "London", "Newcastle"]; var Wales = ["Cardiff", "Swansea"]; for (let city in UKBigCitiesInMillions) { + if (Scotland.includes(city)) { + console.log( + `${city} is in Scotland and has population of ${UKBigCitiesInMillions[city]} millions` + ); + } else if (England.includes(city)) { + console.log( + `${city} is in England and has population of ${UKBigCitiesInMillions[city]} millions` + ); + } else { + console.log( + `${city} is in Wales and has population of ${UKBigCitiesInMillions[city]} millions` + ); + } } diff --git a/week-5/Homework/mandatory/2-exercises/exercises.js b/week-5/Homework/mandatory/2-exercises/exercises.js index cb8c0f42d..30e91a3df 100644 --- a/week-5/Homework/mandatory/2-exercises/exercises.js +++ b/week-5/Homework/mandatory/2-exercises/exercises.js @@ -37,9 +37,16 @@ *
Providing donated bikes and accessories to refugees and asylum seekers in Scotland.