diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6f3a291 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/Week-1/Homework/extra/extra-homework.md b/Week-1/Homework/extra/extra-homework.md index 4aa5066..719496a 100644 --- a/Week-1/Homework/extra/extra-homework.md +++ b/Week-1/Homework/extra/extra-homework.md @@ -7,6 +7,17 @@ Click "ATTEMPT" to test your solution. Exercises: - [Fix my Method](https://www.codewars.com/kata/558710234f02dcc4a8000005) +function myFunction() { + var myObject = { + objProperty: "string", + objMethod: function() { + return myObject.objProperty; + } + } + + return myObject; +}; + - [Regular Ball Super Ball](https://www.codewars.com/kata/53f0f358b9cb376eca001079/train/javascript) ## Reading diff --git a/Week-1/Homework/mandatory/1-writers.js b/Week-1/Homework/mandatory/1-writers.js index 82acf6f..84ad51a 100644 --- a/Week-1/Homework/mandatory/1-writers.js +++ b/Week-1/Homework/mandatory/1-writers.js @@ -39,6 +39,29 @@ let writers = [ } ]; +// https://reactgo.com/javascript-loop-through-array-of-objects/#:~:text=In%20es6%20we%20have%20a,over%20the%20array%20of%20objects.&text=forEach%20methods%20takes%20the%20callback,object%20present%20in%20the%20array. +// writers.forEach((writer) => console.log(writer.firstName, writer.lastName)); --> to test if this forEach method works + +// METHOD1: forEach methods takes the callback function as an argument and runs on each object present in the array. +writers.forEach((writer) => console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`)); + +// METHOD2: in this for of loop, on each iteration different object is assigned to the writer variable. +for(let writer of writers) { + console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`); +} + /* If you want an extra challenge, only `console.log()` the writers that are alive. */ + +// https://alligator.io/js/filter-array-method/ +function aliveWriter(writer) { + if (writer.alive === true) { + return writer; + } +} + +let aliveWriters = writers.filter(aliveWriter); +// console.log(aliveWriters); ---> to check if the output is correct. + +aliveWriters.forEach((writer) => console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`)); \ No newline at end of file diff --git a/Week-1/Homework/mandatory/2-water-bottle.js b/Week-1/Homework/mandatory/2-water-bottle.js index 981d7e3..745ebd7 100644 --- a/Week-1/Homework/mandatory/2-water-bottle.js +++ b/Week-1/Homework/mandatory/2-water-bottle.js @@ -11,13 +11,20 @@ We made a start on this for you here: let bottle = { volume: 0, fill: function() { + 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; + if (this.volume > 10) { + return this.volume -= 10; + } return this.volume = 0; }, empty: function() { // this function should return true if your bottles volume = 0 + if (this.volume === 0) { + return true; + } } }; diff --git a/Week-1/Homework/mandatory/3-groceries.js b/Week-1/Homework/mandatory/3-groceries.js index 2b34cdb..dc617cf 100644 --- a/Week-1/Homework/mandatory/3-groceries.js +++ b/Week-1/Homework/mandatory/3-groceries.js @@ -6,7 +6,13 @@ let groceriesToBuy = []; let groceryList = { - item1: "", - item2: "", - item3: "" + item1: "Potatoes", + item2: "Orange Juice", + item3: "Rice" }; + +// https://www.javascripttutorial.net/object/convert-an-object-to-an-array-in-javascript/#:~:text=To%20convert%20an%20object%20to%20an%20array%20you%20use%20one,entries()%20. + +groceriesToBuy = Object.values(groceryList); + +console.log(groceriesToBuy); \ No newline at end of file diff --git a/Week-1/Homework/mandatory/4-codewars.md b/Week-1/Homework/mandatory/4-codewars.md index bac0d95..3a00f41 100644 --- a/Week-1/Homework/mandatory/4-codewars.md +++ b/Week-1/Homework/mandatory/4-codewars.md @@ -11,7 +11,72 @@ Click "ATTEMPT" to test your solution. Exercises: - [Training JS #5: Basic data types--Object](https://www.codewars.com/kata/571f1eb77e8954a812000837/train/javascript) +function animal(obj){ + return `This ${obj.color} ${obj.name} has ${obj.legs} legs.` +} + - [Welcome!](https://www.codewars.com/kata/welcome/train/javascript) +function greet(language) { + var database = { + english: 'Welcome', + czech: 'Vitejte', + danish: 'Velkomst', + dutch: 'Welkom', + estonian: 'Tere tulemast', + finnish: 'Tervetuloa', + flemish: 'Welgekomen', + french: 'Bienvenue', + german: 'Willkommen', + irish: 'Failte', + italian: 'Benvenuto', + latvian: 'Gaidits', + lithuanian: 'Laukiamas', + polish: 'Witamy', + spanish: 'Bienvenido', + swedish: 'Valkommen', + welsh: 'Croeso' + }; + for (var key in database) { + if(key == language) { + return database[key]; + } + } + return database['english']; +} + +function greet(language) { + return languages[language] || languages['english']; +} + +var languages = { + 'english': 'Welcome', + 'czech': 'Vitejte', + 'danish': 'Velkomst', + 'dutch': 'Welkom', + 'estonian': 'Tere tulemast', + 'finnish': 'Tervetuloa', + 'flemish': 'Welgekomen', + 'french': 'Bienvenue', + 'german': 'Willkommen', + 'irish': 'Failte', + 'italian': 'Benvenuto', + 'latvian': 'Gaidits', + 'lithuanian': 'Laukiamas', + 'polish': 'Witamy', + 'spanish': 'Bienvenido', + 'swedish': 'Valkommen', + 'welsh': 'Croeso' +} + - [Crash Override](https://www.codewars.com/kata/crash-override/train/javascript) +function aliasGen(first, last){ + first = first[0].toUpperCase() + last = last[0].toUpperCase() + if (!first.match(/[A-Z]/) || !last.match(/[A-Z]/)){ + return "Your name must start with a letter from A - Z." + } + return `${firstName[first]} ${surname[last]}` +} + - [Job Matching #1](https://www.codewars.com/kata/56c22c5ae8b139416c00175d/train/javascript) - [Split the Bill](https://www.codewars.com/kata/5641275f07335295f10000d0/train/javascript) diff --git a/Week-1/Homework/projects/1-recipes.js b/Week-1/Homework/projects/1-recipes.js index 3ada67c..7d059fb 100644 --- a/Week-1/Homework/projects/1-recipes.js +++ b/Week-1/Homework/projects/1-recipes.js @@ -21,5 +21,18 @@ cumin cocoa **/ - -let recipes = {}; +//https://stackoverflow.com/questions/54851645/how-to-display-both-key-and-value-in-object-using-javascript/54851680 + +let recipes = { + Title: 'Mole', + Serves: 2, + Ingredients: [ + 'cinnamon', + 'cumin', + 'cocoa', + ] +}; + +for (let key in recipes) { + console.log(key, recipes[key]); + } \ No newline at end of file diff --git a/Week-1/Homework/projects/2-reading-list.js b/Week-1/Homework/projects/2-reading-list.js index 939e3e2..9c4c2a2 100644 --- a/Week-1/Homework/projects/2-reading-list.js +++ b/Week-1/Homework/projects/2-reading-list.js @@ -25,4 +25,44 @@ If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolki **/ -let books = []; +//Exercise 1 -> the question asks to loop through the array. My solution only targets index 0. How would I loop should the array have more than 1 object? + +let books = [ + {title: 'The Hobbit', + author: 'J.R.R. Tolkien', + alreadyRead: false + } +]; +let exercise1 - books.map(x => `${x.title} by ${x.author}`).toString(); +let bookAndAuthor = `"${books[0].title}" by ${books[0].author}`; + +console.log(bookAndAuthor); + +//Exercise 2 -> I tried to create a function to insert into the forEach function but not sure how to continue with it. Or if there's a better way to formulate my function? + +function hasAlreadyRead() { + if (books.alreadyRead === true) { + return `You already read ${bookAndAuthor}`; + } else { + return `You still need to read "The Lord of the Rings" by J.R.R. Tolkien.`; + } +} + +books.forEach(hasAlreadyRead()); + + +let exercise2 = books.map(x => x.alreadyRead === true ? `You already read ${x.title} by ${x.author}` : `You still need to read $${x.title} by ${x.author}`).toString(); + + + + + + + + + + + + + + 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..69e51f8 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,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 = { + name: 'Toshiba', + color: 'black', + OS: 'Windows 10', + numberOfUSB: 2, + webcam: true, + touchscreen: false +}; \ 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..72491bd 100644 --- a/Week-1/InClass/A-objects-intro/exercise-part-1.js +++ b/Week-1/InClass/A-objects-intro/exercise-part-1.js @@ -7,3 +7,40 @@ Assign each of them to a separate variable */ +let house = { + number: 88, + wall: 'brick', + roof: 'tiles', + yearBuilt: 1930, + driveway: true, + garden: true +}; + +let pet = { + species: 'mouse', + name: 'Blacky', + age: 1.5, + sex: 'male', + hasOffsprings: false +} + +let car = { + make: 'Toyota', + model: 'Yaris', + year: 2015, + color: 'white' + validMOT: true +} + +let plant = { + type: 'tomato', + color: 'green', + heightInCm: 50, +} + +let artClass = { + courseLeader: 'Mrs Smith' + teacher: 'Mr Murphy', + classSize: 20, + includesFieldTrips: true +} \ 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..ba56211 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" + furColour: "orange", + age: "23" }; -let laptop = - brand: "Lenovo" - ram "5GB" +let laptop = { + brand: "Lenovo", + ram: "5GB" } let phone = { - operating system "iOS", + operatingSystem: "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/A-objects-intro/exercise-part-3.js b/Week-1/InClass/A-objects-intro/exercise-part-3.js index 83d2575..b3e7904 100644 --- a/Week-1/InClass/A-objects-intro/exercise-part-3.js +++ b/Week-1/InClass/A-objects-intro/exercise-part-3.js @@ -13,4 +13,17 @@ Make sure you use words like: - declaring a variable - etc. -*/ \ No newline at end of file +*/ + +/* +1. Below is a variable with an object literal. +2. The variable is laptop. +2. There are 3 properties in the object literal: brand, ram and hasTouchscreen. +3. Values are Lenovo [a string], 5GB [a string], false [a boolean] respectively. +*/ + +let laptop = { + brand: "Lenovo", + ram: "5GB", + hasTouchscreen: false +} \ 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..ee48a05 100644 --- a/Week-1/InClass/B-objects-get-set/exercise-1.js +++ b/Week-1/InClass/B-objects-get-set/exercise-1.js @@ -10,7 +10,7 @@ let kitten = { // YOUR CODE GOES BELOW HERE - +console.log(kitten); 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..831bc85 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 -; + isUnlocked: 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..7935c05 100644 --- a/Week-1/InClass/B-objects-get-set/exercise-3.js +++ b/Week-1/InClass/B-objects-get-set/exercise-3.js @@ -3,7 +3,9 @@ */ // WRITE CODE BELOW THIS - +let kitten = { + name: 'Gilbert' +} // WRITE CODE ABOVE THIS console.log(kitten.name); 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..db0b223 100644 --- a/Week-1/InClass/B-objects-get-set/exercise-4.js +++ b/Week-1/InClass/B-objects-get-set/exercise-4.js @@ -9,7 +9,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..f40bbda 100644 --- a/Week-1/InClass/C-more-complex-objects/exercise-1.js +++ b/Week-1/InClass/C-more-complex-objects/exercise-1.js @@ -18,6 +18,13 @@ let house = { WRITE YOUR CODE BELOW */ +house.address = '51 Berkley Road', +house.previousOwners = ['Brian M.', 'Fiona S.'], +house.currentOwner.lastName = 'Montgomery'; + +console.log(house.address); +console.log(house.previousOwners); +console.log(house.currentOwner.lastName); // - change the address of "house" to '51 Berkley Road' // - change the previous owners of "house" to ["Brian M.", "Fiona S."] 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..5e1ef52 100644 --- a/Week-1/InClass/C-more-complex-objects/exercise-2.js +++ b/Week-1/InClass/C-more-complex-objects/exercise-2.js @@ -25,6 +25,14 @@ let newCurrentOwner = { WRITE YOUR CODE BELOW */ +house.currentOwner = newCurrentOwner; +house.previousOwners[1] = 'Stephen B.'; +house.isForSale = false; + +console.log(house.currentOwner); +console.log(house.previousOwners); +console.log(house.isForSale); + // - assign the value of the variable 'newCurrentOwner' as the value to the house's "currentOwner" // - from the list of previous owners, replace only "John A." with "Stephen B." // - give the house a new property called 'isForSale' with the value '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..da09da0 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,19 @@ 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) { - + return [house1.currentOwner.email, house2.currentOwner.email]; } // returns the address for the cheapest house out of the two function getCheapestAddress(house1, house2) { - + if (house1.price < house2.price) { + return house1.address; + } return house2.address; } diff --git a/Week-1/InClass/D-methods/exercise-1.js b/Week-1/InClass/D-methods/exercise-1.js index 8de0f8c..6c9f106 100644 --- a/Week-1/InClass/D-methods/exercise-1.js +++ b/Week-1/InClass/D-methods/exercise-1.js @@ -6,9 +6,16 @@ Add a method "greet" so this person can say hello. let person = { name: "Alice", - age: 25 + age: 25, + greet: function() { + return 'Hello everybody'; + } }; +let greetings = person.greet(); + +console.log(greetings); + /* DO NOT EDIT ANYTHING BELOW THIS LINE diff --git a/Week-1/InClass/D-methods/exercise-2.js b/Week-1/InClass/D-methods/exercise-2.js index 8e993fc..57cea34 100644 --- a/Week-1/InClass/D-methods/exercise-2.js +++ b/Week-1/InClass/D-methods/exercise-2.js @@ -7,9 +7,15 @@ Hint: use 'this' keyword to access the name property. let person = { name: "Alice", - age: 25 + age: 25, + sayName: function() { + return `My name is ${this.name}` + } }; +let aliceGreets = person.sayName(); + +console.log(aliceGreets); /* DO NOT EDIT ANYTHING BELOW THIS LINE diff --git a/Week-1/InClass/D-methods/exercise-3.js b/Week-1/InClass/D-methods/exercise-3.js index be23748..61c9fbd 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) { + changeAddress: function(newAddress) { 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..d6c48e3 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: function(name) { + return friends = this.friends.push(name); + } }; diff --git a/Week-1/InClass/D-methods/exercise-5.js b/Week-1/InClass/D-methods/exercise-5.js index dcd198c..5dc03aa 100644 --- a/Week-1/InClass/D-methods/exercise-5.js +++ b/Week-1/InClass/D-methods/exercise-5.js @@ -7,7 +7,7 @@ insertMoney takes an amount in parameter to add money in the coffee machine. getCoffee takes a coffee type in parameter and dispends the selected coffee only if the inserted amount is greater or equal than the price of the coffee! */ - +// GG's version let coffeeMachine = { brand: "Super Coffee", prices: { @@ -17,12 +17,18 @@ let coffeeMachine = { }, insertedAmount: 0, insertMoney: function (amount) { - + this.insertedAmount += amount; }, getCoffee: function (coffee) { - + if (this.insertedAmount >= this.prices[coffee]) { + this.insertedAmount = 0; + return `Please take your ${coffee}.`; + } + else { + return `Sorry you don't have enough money for a ${coffee}.` + } } -}; + /* 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..508625c 100644 --- a/Week-1/InClass/E-arrays-of-objects/exercise-1.js +++ b/Week-1/InClass/E-arrays-of-objects/exercise-1.js @@ -25,13 +25,19 @@ WRITE YOUR CODE BELOW */ -var persons = // Complete here +var persons = [person1, person2, person3];// Complete here -var personNames = // Complete here +var personNames = var personNames = persons.map(x => x.name);// Complete here -var personsYoungerThan28YearsOld = // Complete here +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions +// altnertive +var personNames = persons.map(function getNames(names){ + return x.names; + }); +var personsYoungerThan28YearsOld = = persons.filter(x => x.age < 28); +persons.filter(personsYoungerThan28YearsOld); /* DO NOT EDIT ANYTHING BELOW THIS LINE */ 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..961a169 100644 --- a/Week-1/InClass/E-arrays-of-objects/exercise-2.js +++ b/Week-1/InClass/E-arrays-of-objects/exercise-2.js @@ -39,12 +39,23 @@ DO NOT EDIT ANYTHING ABOVE THIS LINE WRITE YOUR CODE BELOW */ +function checkDestinationNameWithin500Kms(destination) { + return destination.distanceKms <= 500; +} -let destinationNamesWithin500Kms = // Complete here +function getDestinationNameWithin500Kms(destination) { + return destination.destinationName; +} -let destinationNameReachableByFerry = // Complete here +// Complete here +let destinationNamesWithin500Kms = travelDestinations.filter(destination => destination.distanceKms <= 500).map(destination => destination.destinationName); +/*---------------------------*/ +//first, filter if destination includes ferry - true/false -let destinationNamesMoreThan300KmsAwayByTrain = // Complete here (PRINT THE RESULT IN THE CONSOLE USING FOREACH) +// second, if true, map out all array items +let destinationNameReachableByFerry = travelDestinations.find(destination => destination.transportations.includes('ferry')).destinationName;// Complete here + +let destinationNamesMoreThan300KmsAwayByTrain = travelDestinations.filter(destination => destination.distanceKms > 300).filter(destination => destination.transportations.includes('train')).map(destination => destination.destinationName);// 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..101f0cd 100644 --- a/Week-1/InClass/E-arrays-of-objects/exercise-3.js +++ b/Week-1/InClass/E-arrays-of-objects/exercise-3.js @@ -54,19 +54,19 @@ DO NOT EDIT ANYTHING ABOVE THIS LINE WRITE YOUR CODE BELOW */ - +//MA's version let restaurantFinderApplication = { applicationName: "Restaurant Finder", applicationVersion: "1.0", restaurants: restaurants, findAvailableRestaurants: function (numberOfPeople) { - // Complete here + return this.restaurants.filter(x => x.totalSeats - x.numberOfCustomers >= numberOfPeople).map(x => x.name)// Complete here }, findRestaurantServingDish: function (dishName) { - // Complete here + return this.restaurants.filter(x => x.menu.includes(dishName)).map(x => x.name)// Complete here }, - countNumberOfRestaurantsInArea: function (area) { - // Complete here + countNumberOfRestaurantsInArea: function (location) { + return this.restaurants.filter(x => x.address.area === location).length;// Complete here } }; 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..2588f93 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..3dad6e0 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,17 @@ let mentorsAges = { // ONLY EDIT BELOW THIS LINE -let mentorsNames = ; +let mentorsNames = Object.keys(mentorsAges); -let mentorsNamedUppercased = ; +// error, see below Aman's solution -> let mentorsNamedUppercased = mentorsNames.toUpperCase(); + +let Uppercased = function(x){ + return x.toUpperCase(); +}; + mentorsNamedUppercased = mentorsNames.map(Uppercased); + + // OR Mursel's solution +let mentorsNamedUppercased = mentorsNames.map(x => x.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..151e443 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,14 @@ 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.edinburgh.interns)) // ONLY EDIT ABOVE THIS LINE diff --git a/Week-2/Homework/mandatory/0-khanacademy/khan - createASolarSystem.html b/Week-2/Homework/mandatory/0-khanacademy/khan - createASolarSystem.html new file mode 100644 index 0000000..eb4d288 --- /dev/null +++ b/Week-2/Homework/mandatory/0-khanacademy/khan - createASolarSystem.html @@ -0,0 +1,47 @@ + + + + +
+ +
+
+
+
+
+
+
diff --git a/Week-2/Homework/mandatory/0-khanacademy/khan-getElementByID.html b/Week-2/Homework/mandatory/0-khanacademy/khan-getElementByID.html
new file mode 100644
index 0000000..1a4b062
--- /dev/null
+++ b/Week-2/Homework/mandatory/0-khanacademy/khan-getElementByID.html
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+ The domestic dog is known as man's best friend. The dog was the first domesticated animal and has been widely kept as a working, hunting, and pet companion. According to recent coarse estimates, there are currently between 700 million and one billion dogs, making them the most abundant predators in the world. Read more on Wikipedia.
+ +
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Week-2/Homework/mandatory/0-khanacademy/khan-value.html b/Week-2/Homework/mandatory/0-khanacademy/khan-value.html
new file mode 100644
index 0000000..9bb7f25
--- /dev/null
+++ b/Week-2/Homework/mandatory/0-khanacademy/khan-value.html
@@ -0,0 +1,45 @@
+
+
+
+
+
+
+ Generated story: + +
+ + + + + \ No newline at end of file diff --git a/Week-2/Homework/mandatory/2-exercises/exercises.js b/Week-2/Homework/mandatory/2-exercises/exercises.js index 174c5db..2b48fb9 100644 --- a/Week-2/Homework/mandatory/2-exercises/exercises.js +++ b/Week-2/Homework/mandatory/2-exercises/exercises.js @@ -15,8 +15,42 @@ */ function exerciseOne(arrayOfPeople) { let content = document.querySelector("#content"); + for (let i = 0; i < arrayOfPeople.length; i++) { + content[i] = content[i].createElement('h1'); + content[i] = content[i].createElement('h2'); + } } +// SA's version +function exerciseOne(arrayOfPeople) { + let content = document.querySelector("#content"); + for(let i=0; i < arrayOfPeople.length; i++){ + //create h1 element + let h1Els = document.createElement('h1'); + let h2Els = document.createElement('h2'); + //add text with name from arrayOfPeople + h1Els.textContent = arrayOfPeople[i].name; + h2Els.textContent = arrayOfPeople[i].job; + //add to parent node + content.appendChild(h1Els); + content.appendChild(h2Els); + } + +//homework club - Martin Armstrong +function exerciseOne(arrayOfPeople) { + let content = document.querySelector("#content"); + + arrayOfPeople.forEach(person => { + h1 = document.createElement('h1'); + h2 = document.createElement('h2'); + + h1.textContent = person.name; + h2.textContent = person.job; + + content.append.Child(h1); + content.append.Child(h2); + }); +} /** * * Create a list of shopping items. You should use an unordered list. @@ -25,9 +59,37 @@ function exerciseOne(arrayOfPeople) { * */ function exerciseTwo(shopping) { - //Write your code in here + // create a div element and assign it to a variable "shoppingList" + //let shoppingList = document.createElement('div'); + + // give the div an ID 'content' + //shoppingList.setAttribute('id', 'content'); + + let contentDiv = document.getElementById('content'); + // create Element of unordered list inside div + let list = document.createElement('ul'); + contentDiv.appendChild(list); + + shopping.forEach(shoppingItem => { + let li = document.createElement('li'); + li.textContent = shoppingItem; // alt version to lines 76-77 + //let arrItem = document.createTextNode(shoppingItem); + //li.appendChild(arrItem); + list.appendChild(li); + } ) } + // FOR LOOP - create Element of list items inside ul, times the array length + /*for (let i = 0; i < shopping.length; i++) { + let li = document.createElement('li'); + let arrItem = document.createTextNode(shopping[i]); + li.appendChild(arrItem); + document.getElementsByTagName('ul').appendChild(li); + } + return shoppingList; +}*/ + + /** I'd like to display my three favorite books inside a nice webpage! @@ -58,9 +120,31 @@ function exerciseTwo(shopping) { The end result should look something like this: https://hyf-js2-week1-makeme-ex1-demo.herokuapp.com/ **/ function exerciseThree(books) { - //Write your code in here -} + books[0].src = "https://www.google.com/aclk?sa=l&ai=DChcSEwispaDaveHqAhVxgFAGHTSdAEUYABAFGgJkZw&sig=AOD64_1azV9IPe6WEzVXOxoeLptI5aprfQ&adurl&ctype=5&ved=2ahUKEwj80JbaveHqAhWhgXMKHfWCDy4Qvhd6BAgBEDM"; +books[1].src = "https://www.google.com/aclk?sa=L&ai=DChcSEwjUjY3HvuHqAhUG-FEKHXVLDpwYABAHGgJ3cw&sig=AOD64_24Xuw_dw8fpfKPysyJKm5IlNdttQ&adurl&ctype=5&ved=2ahUKEwjzr4PHvuHqAhUClRoKHY3KD38Qvhd6BAgBEEA"; +books[2].src = "https://www.google.com/aclk?sa=L&ai=DChcSEwjUjY3HvuHqAhUG-FEKHXVLDpwYABAHGgJ3cw&sig=AOD64_24Xuw_dw8fpfKPysyJKm5IlNdttQ&adurl&ctype=5&ved=2ahUKEwjzr4PHvuHqAhUClRoKHY3KD38Qvhd6BAgBEEA" +let contentDiv = document.getElementById('content'); +let ul = document.createElement('ul'); +contentDiv.appendChild(ul); +books.forEach(book => { + // create aelement + //Assign textContent with the book title and author + // append it to the page. + let li = document.createElement('li'); + let p = document.createElement('p'); + let img = document.createElement('img'); + p.textContent = book.title + " - " + book.author; + li.appendChild(p); + li.appendChild(img); + ul.appendChild(li); + if(alreadyRead === true) { + backgroundColor = 'green'; + } else { + backgroundColor = 'red'; + } + }) +} // // // diff --git a/Week-2/Homework/mandatory/3-project/js/main.js b/Week-2/Homework/mandatory/3-project/js/main.js index e69de29..f6ab783 100644 --- a/Week-2/Homework/mandatory/3-project/js/main.js +++ b/Week-2/Homework/mandatory/3-project/js/main.js @@ -0,0 +1,60 @@ +// When clicking **blue** it should change: + +let blueBtnEl = document.querySelector('#blueBtn'); + +let onBlueBtnClick = function() { + document.querySelector('.jumbotron').style.backgroundColor = '#588fbd'; + document.querySelector('.btn-primary.btn-lrg').style.backgroundColor = '#ffa500'; + document.querySelector('.btn-secondary.btn-lrg').style.backgroundColor = '#000000'; + document.querySelector('.btn-secondary.btn-lrg').style.color = '#ffffff'; +} + +blueBtnEl.addEventListener('click', onBlueBtnClick); + +// When clicking **orange** it should change: + +let orangeBtnEL = document.querySelector('#orangeBtn'); + +let onOrangeBtnClick = function() { + document.querySelector('.jumbotron').style.backgroundColor = '#f0ad4e'; + document.querySelector('.btn-primary.btn-lrg').style.backgroundColor = '#5751fd'; + document.querySelector('.btn-secondary.btn-lrg').style.backgroundColor = '#31b0d5'; + document.querySelector('.btn-secondary.btn-lrg').style.color = '#ffffff'; +} + +orangeBtnEL.addEventListener('click', onOrangeBtnClick); + +// When clicking **green** it should change: + +let greenBtnEL = document.querySelector('#greenBtn'); + +let onGreenBtnClick = function() { + document.querySelector('.jumbotron').style.backgroundColor = '#87ca8a'; + document.querySelector('.btn-primary.btn-lrg').style.backgroundColor = '#000000'; + document.querySelector('.btn-secondary.btn-lrg').style.backgroundColor = '#8c9c08'; +} + +greenBtnEL.addEventListener('click', onGreenBtnClick); + +// validate form https://www.tutorialspoint.com/javascript/javascript_form_validations.htm + +let submitBtnEl = document.querySelector('form .btn-primary'); + +let onSubmitBtnClick = function(event) { + let emailField = document.querySelector('#exampleInputEmail1'); + let nameField = document.querySelector('#example-text-input').value; + let describeField = document.querySelector('#exampleTextarea').value; + + if (emailField.value.length < 0) { + document.querySelector('#exampleInputEmail1').style.backgroundColor = '#FF0000'; + } + if (nameField.length < 0) { + document.querySelector('#example-text-input').style.backgroundColor = '#FF0000'; + } + if (describeField.length < 0) { + document.querySelector('#exampleTextarea').style.backgroundColor = '#FF0000'; + } + event.preventDefault(); +} + +submitBtnEl.addEventListener('click', onSubmitBtnClick); \ No newline at end of file diff --git a/Week-2/Homework/mandatory/4-practice/practice-codewars.md b/Week-2/Homework/mandatory/4-practice/practice-codewars.md index d27d4a5..034aa61 100644 --- a/Week-2/Homework/mandatory/4-practice/practice-codewars.md +++ b/Week-2/Homework/mandatory/4-practice/practice-codewars.md @@ -3,7 +3,15 @@ Complete the following CodeWars on JavaScript concepts you learned in previous w # CodeWars - [Is it even?](https://www.codewars.com/kata/555a67db74814aa4ee0001b5/train/javascript) +function testEven(n) { + return n % 2 === 0 ? true : false; +} + - [Will you make it?](https://www.codewars.com/kata/5861d28f124b35723e00005e/train/javascript) +const zeroFuel = (distanceToPump, mpg, fuelLeft) => { + return fuelLeft * mpg >= distanceToPump ? true : false; +}; + - [Removing elements](https://www.codewars.com/kata/5769b3802ae6f8e4890009d2/train/javascript) - [Grasshopper - Summation](https://www.codewars.com/kata/55d24f55d7dd296eb9000030/train/javascript) - [Find the Difference in Age between Oldest and Youngest Family Members](https://www.codewars.com/kata/5720a1cb65a504fdff0003e2/train/javascript) diff --git a/Week-3/Homework/mandatory/1-alarmclock/alarmclock.js b/Week-3/Homework/mandatory/1-alarmclock/alarmclock.js index 6ca81cd..6c1c64b 100644 --- a/Week-3/Homework/mandatory/1-alarmclock/alarmclock.js +++ b/Week-3/Homework/mandatory/1-alarmclock/alarmclock.js @@ -1,4 +1,37 @@ -function setAlarm() {} +// digital clock https://www.youtube.com/watch?v=qvypCd2Vl4s +// timer https://www.youtube.com/watch?v=NJVJRFF-Y6U +// watch this >> timer https://www.youtube.com/watch?v=4_o3wO6aawg + +function setAlarm() { + let alarmSet = document.getElementById("alarmSet"); + let timer = alarmSet.value; + let timeRemaining = document.getElementById("timeRemaining"); + let stopBtn = document.querySelector("#stop"); + + let clock = setInterval(countDown, 1000); + + function countDown() { + if (timer < 0) { + alarmSet.value = ""; + clearInterval(clock); + playAlarm(); + } else { + if (timer >= 10) { + timeRemaining.textContent = `Time Remaining: 00:${timer}`; + timer--; + } + } else { + timeRemaining.textContent = `Time Remaining: 00:0${timer}`; + timer--; + } + } + + stopBtn.addEventListener("click", () => { + clearInterval(clock); + pauseAlarm(); + }); +} + // DO NOT EDIT BELOW HERE diff --git a/Week-3/Homework/mandatory/2-quotegenerator/index.html b/Week-3/Homework/mandatory/2-quotegenerator/index.html index b6115be..d6e73bc 100644 --- a/Week-3/Homework/mandatory/2-quotegenerator/index.html +++ b/Week-3/Homework/mandatory/2-quotegenerator/index.html @@ -2,7 +2,7 @@
+
+
+
+
+
+
+