diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d9b319b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}\\Week-1\\InClass\\E-arrays-of-objects\\exercise-3.js" + } + ] +} \ No newline at end of file diff --git a/Week-1/Homework/mandatory/1-writers.js b/Week-1/Homework/mandatory/1-writers.js index 82acf6f..dbcc686 100644 --- a/Week-1/Homework/mandatory/1-writers.js +++ b/Week-1/Homework/mandatory/1-writers.js @@ -42,3 +42,13 @@ let writers = [ /* If you want an extra challenge, only `console.log()` the writers that are alive. */ + +function getwriters(writers){ + if(writers.alive){ +return "Hi, my name is " + writers.firstName + " " + writers.lastName+". I am "+writers.age+" years old, and work as a "+writers.occupation+"."; +} else { + return false; +} +} + +console.log(writers.map(getwriters).filter(Boolean)); diff --git a/Week-1/Homework/mandatory/2-water-bottle.js b/Week-1/Homework/mandatory/2-water-bottle.js index 981d7e3..e0c1f03 100644 --- a/Week-1/Homework/mandatory/2-water-bottle.js +++ b/Week-1/Homework/mandatory/2-water-bottle.js @@ -11,14 +11,15 @@ We made a start on this for you here: let bottle = { volume: 0, fill: function() { - // calling this function should make you bottles volume = 100; + return this.volume =100;// calling this function should make you bottles volume = 100; }, drink: function() { - // calling this function should decrease your bottles volume by 10; + return this.volume=this.volume-10;// calling this function should decrease your bottles volume by 10; }, empty: function() { - // this function should return true if your bottles volume = 0 - } + if (this.volume===0){ + return true;// this function should return true if your bottles volume = 0 + }} }; /* diff --git a/Week-1/Homework/mandatory/3-groceries.js b/Week-1/Homework/mandatory/3-groceries.js index 2b34cdb..4a60057 100644 --- a/Week-1/Homework/mandatory/3-groceries.js +++ b/Week-1/Homework/mandatory/3-groceries.js @@ -10,3 +10,13 @@ let groceryList = { item2: "", item3: "" }; + +groceryList.item1 = "Potatoes"; +groceriesToBuy.push(groceryList.item1); +groceryList.item2 = "Orange Juice"; +groceriesToBuy.push(groceryList.item2); +groceryList.item3 = "Rice"; +groceriesToBuy.push(groceryList.item3); + + + console.log(groceriesToBuy); diff --git a/Week-1/Homework/projects/1-recipes.js b/Week-1/Homework/projects/1-recipes.js index 3ada67c..31eb615 100644 --- a/Week-1/Homework/projects/1-recipes.js +++ b/Week-1/Homework/projects/1-recipes.js @@ -22,4 +22,20 @@ cocoa **/ -let recipes = {}; +let recipes = { + Name : "Mole", + Serving: 2, + Ingredients: { + a:"cinnamon", + b:"cumin", + c:"cocoa" + } +}; + + +console.log( `${recipes.Name} +Serves: ${recipes.Serving} +Ingredients: +${recipes.Ingredients.a} +${recipes.Ingredients.b} +${recipes.Ingredients.c}`); diff --git a/Week-1/Homework/projects/2-reading-list.js b/Week-1/Homework/projects/2-reading-list.js index 939e3e2..161deb6 100644 --- a/Week-1/Homework/projects/2-reading-list.js +++ b/Week-1/Homework/projects/2-reading-list.js @@ -25,4 +25,28 @@ If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolki **/ -let books = []; +let books = [ +{ + title: "The Hobbit", + Author: "J.R.R Tolkien", + alreadyRead: true +}, +{ + title: "The Lord of Rings", + Author: "J.R.R Tolkien", + alreadyRead: false +} + +]; + +// function getTitleAuthor (books){ +// return books.title+" by "+books.Author; +// } +books.forEach(function getTitleAuthor(books) { + if(books.alreadyRead){ + console.log("You already read "+ books.title + " by " + books.Author); + } + else { + console.log("You still need to read "+books.title + " by " + books.Author); + } +}); diff --git a/Week-1/InClass/A-objects-intro/exercise-part-0.js b/Week-1/InClass/A-objects-intro/exercise-part-0.js index 433d27c..8ab3ced 100644 --- a/Week-1/InClass/A-objects-intro/exercise-part-0.js +++ b/Week-1/InClass/A-objects-intro/exercise-part-0.js @@ -4,4 +4,14 @@ 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 : "HP", + screenSize : 15, + isTouchscreen : false, + ramMemorysize : 8, + storageMemorysize : 250 +}; + +console.log(laptop); \ No newline at end of file diff --git a/Week-1/InClass/A-objects-intro/exercise-part-1.js b/Week-1/InClass/A-objects-intro/exercise-part-1.js index 49e1ed9..85c12b8 100644 --- a/Week-1/InClass/A-objects-intro/exercise-part-1.js +++ b/Week-1/InClass/A-objects-intro/exercise-part-1.js @@ -6,4 +6,55 @@ can describe with a JavaScript object Assign each of them to a separate variable */ +// 1 +let smartPhone ={ + brand : "Samsung", + color : "blue", + memory : 64, +}; +let phone = smartPhone; + +console.log(phone); +//2 +let newShirt = { + size : 15, + color : "blue", + type : "cotton" +}; + +let shirt = newShirt; + +console.log(shirt); +//3 +let whiteBoard = { + size : "4ft x 4ft", + color : "white", + type : "magnetic" +} + +let writingBoard = whiteBoard; + +console.log(writingBoard); +//4 + +let newDesk = { + height : 4, + width : 5, + type : "Oak", + color : "white" +} + +let desk = newDesk; + +console.log(desk); +//5 + +let newMonitor = { + brand : "samsung", + screenSize : 24, + type : "smart", +} +let monitor = newMonitor; + +console.log(monitor); \ No newline at end of file diff --git a/Week-1/InClass/A-objects-intro/exercise-part-2.js b/Week-1/InClass/A-objects-intro/exercise-part-2.js index 4e01403..d5d7cf5 100644 --- a/Week-1/InClass/A-objects-intro/exercise-part-2.js +++ b/Week-1/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 : "5GB" +}; let phone = { - operating system "iOS", + "operating system" : "iOS", hasStylus: true, - megapixels 12 - "batteryLife": "24 hours" \ No newline at end of file + megapixels : 12, + batteryLife: "24 hours" +}; \ No newline at end of file diff --git a/Week-1/InClass/B-objects-get-set/exercise-1.js b/Week-1/InClass/B-objects-get-set/exercise-1.js index 6591384..2771567 100644 --- a/Week-1/InClass/B-objects-get-set/exercise-1.js +++ b/Week-1/InClass/B-objects-get-set/exercise-1.js @@ -2,17 +2,11 @@ Console.log the values of each property of "kitten" */ -let kitten = { - ageMonths: 3, - isFemale: true, - furColour: "brown" -}; - +let kitten = { // YOUR CODE GOES BELOW HERE - - - - - - + isFurry : true, + color : "gray", +} + console.log(kitten.isFurry); + console.log(kitten.color); // YOUR CODE GOES ABOVE HERE \ No newline at end of file diff --git a/Week-1/InClass/B-objects-get-set/exercise-2.js b/Week-1/InClass/B-objects-get-set/exercise-2.js index c8b5e7b..d7a67fa 100644 --- a/Week-1/InClass/B-objects-get-set/exercise-2.js +++ b/Week-1/InClass/B-objects-get-set/exercise-2.js @@ -5,14 +5,14 @@ */ let phone = { - brand: 'iPhone, - model 'iPhone X' + brand: 'iPhone', + model :'iPhone X', launchYear: 2017, - is Unlocked: true -; + "is Unlocked": true +}; -let phoneBrand = phone.bbrand; -let phoneLaunchYear = phone[launchYear]; +let phoneBrand = phone.brand; +let phoneLaunchYear = phone["launchYear"]; // DO NOT MODIFY BELOW THIS LINE diff --git a/Week-1/InClass/B-objects-get-set/exercise-3.js b/Week-1/InClass/B-objects-get-set/exercise-3.js index f775c9a..ddd0be1 100644 --- a/Week-1/InClass/B-objects-get-set/exercise-3.js +++ b/Week-1/InClass/B-objects-get-set/exercise-3.js @@ -3,6 +3,9 @@ */ // WRITE CODE BELOW THIS +let kitten = { + name : "Gilbert" +}; // WRITE CODE ABOVE THIS diff --git a/Week-1/InClass/B-objects-get-set/exercise-4.js b/Week-1/InClass/B-objects-get-set/exercise-4.js index 763347e..100a6d9 100644 --- a/Week-1/InClass/B-objects-get-set/exercise-4.js +++ b/Week-1/InClass/B-objects-get-set/exercise-4.js @@ -8,7 +8,8 @@ let dog = { }; // WRITE CODE BELOW THIS LINE - + dog.name = "Rex"; + dog.wantsToPlay = true; // WRITE CODE ABOVE THIS LINE diff --git a/Week-1/InClass/C-more-complex-objects/exercise-1.js b/Week-1/InClass/C-more-complex-objects/exercise-1.js index 8ae3e82..cdd4f60 100644 --- a/Week-1/InClass/C-more-complex-objects/exercise-1.js +++ b/Week-1/InClass/C-more-complex-objects/exercise-1.js @@ -20,8 +20,11 @@ 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."; /* diff --git a/Week-1/InClass/C-more-complex-objects/exercise-2.js b/Week-1/InClass/C-more-complex-objects/exercise-2.js index 7ea0200..e166df5 100644 --- a/Week-1/InClass/C-more-complex-objects/exercise-2.js +++ b/Week-1/InClass/C-more-complex-objects/exercise-2.js @@ -26,8 +26,11 @@ 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'; diff --git a/Week-1/InClass/C-more-complex-objects/exercise-3.js b/Week-1/InClass/C-more-complex-objects/exercise-3.js index 4bfbfd3..c9892cb 100644 --- a/Week-1/InClass/C-more-complex-objects/exercise-3.js +++ b/Week-1/InClass/C-more-complex-objects/exercise-3.js @@ -32,17 +32,25 @@ let parkAvenueHouse = { // returns the full name (first name + last name) of the owner of the house function getOwnerFullName(house) { - + return house.currentOwner.firstName+ " "+house.currentOwner.lastName; } // returns an array of the owners' email addresses of the two houses function getEmailAddresses(house1, house2) { - + let arr = []; + arr.push(house1.currentOwner.email); + arr.push(house2.currentOwner.email); + return arr; } // returns the address for the cheapest house out of the two function getCheapestAddress(house1, house2) { - + if(house1.price>house2.price){ + return house2.address; + } + else { + return house1.address; + } } diff --git a/Week-1/InClass/D-methods/exercise-1.js b/Week-1/InClass/D-methods/exercise-1.js index 8de0f8c..2c3bb40 100644 --- a/Week-1/InClass/D-methods/exercise-1.js +++ b/Week-1/InClass/D-methods/exercise-1.js @@ -6,7 +6,10 @@ Add a method "greet" so this person can say hello. let person = { name: "Alice", - age: 25 + age: 25, + greet: function(){ + return "Hello everybody"; + } }; diff --git a/Week-1/InClass/D-methods/exercise-2.js b/Week-1/InClass/D-methods/exercise-2.js index 8e993fc..1420e29 100644 --- a/Week-1/InClass/D-methods/exercise-2.js +++ b/Week-1/InClass/D-methods/exercise-2.js @@ -7,7 +7,10 @@ Hint: use 'this' keyword to access the name property. let person = { name: "Alice", - age: 25 + age: 25, + sayName(){ + return "My name is "+this.name; + } }; diff --git a/Week-1/InClass/D-methods/exercise-3.js b/Week-1/InClass/D-methods/exercise-3.js index be23748..cb4e200 100644 --- a/Week-1/InClass/D-methods/exercise-3.js +++ b/Week-1/InClass/D-methods/exercise-3.js @@ -8,11 +8,11 @@ let person = { name: "Alice", age: 25, currentAddress: "Glasgow", - changeAddress: (newAddress) { - currentAddress = newAddress; + changeAddress: function (newAddress) { + person.currentAddress = newAddress; }, - celebrateBirthday: function { - that.age = that.age + 1; + celebrateBirthday: function () { + this.age = this.age + 1; } }; diff --git a/Week-1/InClass/D-methods/exercise-4.js b/Week-1/InClass/D-methods/exercise-4.js index d89214a..dbeabd9 100644 --- a/Week-1/InClass/D-methods/exercise-4.js +++ b/Week-1/InClass/D-methods/exercise-4.js @@ -6,7 +6,10 @@ Define a method "makeFriend" to add a new friend to her list. let person = { name: "Alice", - friends: ["John", "Nina"] + friends: ["John", "Nina"], + makeFriend(friend){ + return person.friends.push(friend); + } }; diff --git a/Week-1/InClass/D-methods/exercise-5.js b/Week-1/InClass/D-methods/exercise-5.js index dcd198c..33246c0 100644 --- a/Week-1/InClass/D-methods/exercise-5.js +++ b/Week-1/InClass/D-methods/exercise-5.js @@ -17,14 +17,22 @@ let coffeeMachine = { }, insertedAmount: 0, insertMoney: function (amount) { - + this.insertedAmount = amount; + return this.insertedAmount; }, 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 */ diff --git a/Week-1/InClass/E-arrays-of-objects/exercise-1.js b/Week-1/InClass/E-arrays-of-objects/exercise-1.js index 8d39a81..5e26ac9 100644 --- a/Week-1/InClass/E-arrays-of-objects/exercise-1.js +++ b/Week-1/InClass/E-arrays-of-objects/exercise-1.js @@ -25,11 +25,15 @@ WRITE YOUR CODE BELOW */ -var persons = // Complete here - -var personNames = // Complete here - -var personsYoungerThan28YearsOld = // Complete here +var persons = [person1,person2,person3];// Complete here + function getNames(person){ + return person.name; + } +var personNames = persons.map(getNames);// Complete here + function getpersonYoungerThan28YearsOld (person){ + return person.age < 28; + } +var personsYoungerThan28YearsOld = persons.filter(getpersonYoungerThan28YearsOld); // Complete here /* diff --git a/Week-1/InClass/E-arrays-of-objects/exercise-2.js b/Week-1/InClass/E-arrays-of-objects/exercise-2.js index c2259dd..cdd49fe 100644 --- a/Week-1/InClass/E-arrays-of-objects/exercise-2.js +++ b/Week-1/InClass/E-arrays-of-objects/exercise-2.js @@ -38,13 +38,27 @@ let travelDestinations = [destination1, destination2, destination3, destination4 DO NOT EDIT ANYTHING ABOVE THIS LINE WRITE YOUR CODE BELOW */ + function getdestinationNamesWithin500Kms(travelDestination){ + return travelDestination.distanceKms<=500; + + } + function getdestinationNames (travelDestination){ + return travelDestination.destinationName; + } +let destinationNamesWithin500Kms = travelDestinations.filter(getdestinationNamesWithin500Kms).map(getdestinationNames); // Complete here + function destinationReachableByFerry (travelDestination){ + return travelDestination.transportations.includes("ferry"); + } +let destinationNameReachableByFerry = travelDestinations.filter(destinationReachableByFerry).map(getdestinationNames);// Complete here +function destinationReachableByTrain(travelDestination) { + return travelDestination.transportations.includes("train"); +} +function getdestinationNamesMoreThan300Kms(travelDestination) { + return travelDestination.distanceKms >= 300; -let destinationNamesWithin500Kms = // Complete here - -let destinationNameReachableByFerry = // Complete here - -let destinationNamesMoreThan300KmsAwayByTrain = // Complete here (PRINT THE RESULT IN THE CONSOLE USING FOREACH) +} +let destinationNamesMoreThan300KmsAwayByTrain = travelDestinations.filter(getdestinationNamesMoreThan300Kms).filter(destinationReachableByTrain).map(getdestinationNames);// Complete here (PRINT THE RESULT IN THE CONSOLE USING FOREACH) /* diff --git a/Week-1/InClass/E-arrays-of-objects/exercise-3.js b/Week-1/InClass/E-arrays-of-objects/exercise-3.js index a1ec691..22ba930 100644 --- a/Week-1/InClass/E-arrays-of-objects/exercise-3.js +++ b/Week-1/InClass/E-arrays-of-objects/exercise-3.js @@ -53,24 +53,44 @@ let restaurants = [restaurant1, restaurant2, restaurant3]; DO NOT EDIT ANYTHING ABOVE THIS LINE WRITE YOUR CODE BELOW */ - + + let restaurantFinderApplication = { applicationName: "Restaurant Finder", applicationVersion: "1.0", restaurants: restaurants, findAvailableRestaurants: function (numberOfPeople) { + return restaurants.map(function(restaurant){ + if ((restaurant.totalSeats - restaurant.numberOfCustomers) > numberOfPeople){ + return restaurant.name; + } + }); // Complete here }, findRestaurantServingDish: function (dishName) { - // Complete here + return restaurants.map(function (restaurant) { + if (restaurant.menu.includes(dishName)) { + return restaurant.name; + } + }); // Complete here }, countNumberOfRestaurantsInArea: function (area) { - // Complete here + let count = 0; + restaurants.map(function (restaurant) { + if (restaurant.address.area.includes(area)) { + + return count=count+1; + } + + }); + return count; // Complete here } }; + + /* DO NOT EDIT ANYTHING BELOW THIS LINE */ diff --git a/Week-1/InClass/F-object-keys/exercise-part-0.js b/Week-1/InClass/F-object-keys/exercise-part-0.js index d9b1085..f6c4e02 100644 --- a/Week-1/InClass/F-object-keys/exercise-part-0.js +++ b/Week-1/InClass/F-object-keys/exercise-part-0.js @@ -20,8 +20,8 @@ let highScores = { // ONLY EDIT BELOW HERE -let capitalCitiesKeys = ; -let highScoresKeys; +let capitalCitiesKeys =Object.keys(capitalCities) ; +let highScoresKeys = Object.keys(highScores); // ONLY EDIT ABOVE HERE diff --git a/Week-1/InClass/F-object-keys/exercise-part-1.js b/Week-1/InClass/F-object-keys/exercise-part-1.js index b8d4be7..fe63a3e 100644 --- a/Week-1/InClass/F-object-keys/exercise-part-1.js +++ b/Week-1/InClass/F-object-keys/exercise-part-1.js @@ -15,9 +15,11 @@ let mentorsAges = { // ONLY EDIT BELOW THIS LINE -let mentorsNames = ; +let mentorsNames = Object.keys(mentorsAges); -let mentorsNamedUppercased = ; +let mentorsNamedUppercased = mentorsNames.map(function (arr){ + return arr.toUpperCase(); +}); // ONLY EDIT ABOVE THIS LINE diff --git a/Week-1/InClass/F-object-keys/exercise-part-2.js b/Week-1/InClass/F-object-keys/exercise-part-2.js index 6b6a1bb..d99dfa9 100644 --- a/Week-1/InClass/F-object-keys/exercise-part-2.js +++ b/Week-1/InClass/F-object-keys/exercise-part-2.js @@ -35,14 +35,13 @@ let storeBranches = { // # 1 // prints [ 'glasgow', 'edinburgh' ] -console.log() - +console.log(Object.keys(storeBranches)) // # 2 // prints [ 'manager', 'assistant', 'interns' ] -console.log() +console.log(Object.keys(storeBranches.glasgow)) // # 3 // prints [ 'head_intern', 'intern' ] -console.log() +console.log(Object.keys(storeBranches.glasgow.interns)) // ONLY EDIT ABOVE THIS LINE