From dae71a61225a142989bf5aa4a2882112da22f79b Mon Sep 17 00:00:00 2001 From: Kennedy Maling Date: Fri, 16 Aug 2019 12:33:58 +0530 Subject: [PATCH] update js_array-objects-1md --- 1. objects/1.md | 117 ++++++++++++++++++++- 1. objects/2.js | 34 +++++- 2. array/1.md | 42 ++++++++ 3. copy-vs-reference/index.js | 58 ++++++---- 4. array-and-objects/01/array-in-object.md | 2 +- 4. array-and-objects/01/main.js | 25 +++++ 4. array-and-objects/02/main.js | 69 ++++++++++++ 4. array-and-objects/02/object-in-array.md | 2 +- 4. array-and-objects/03/main.js | 26 +++++ 9 files changed, 349 insertions(+), 26 deletions(-) diff --git a/1. objects/1.md b/1. objects/1.md index 5110277..3642440 100644 --- a/1. objects/1.md +++ b/1. objects/1.md @@ -2,72 +2,127 @@ ```js // your code goes here +// Creating an object + +var myPenguin = { + "character name": "Captain Cook, Greta", + origin: "Mr. Popper's Penguin", + author: "Richard and Florence Atwater", + } + ``` 2. Use console.log() to print the penguin's name to the console as part of a welcome message, like "Hello, I'm a penguin and my name is [NAME HERE]!" ```js // your code goes here + +console.log(` "Hello, I'm a penguin and my name is, myPenguin ("character name")`; + ``` 3. Write another line of code that adds a new property to your penguin called canFly and set it to false. ```js // your code goes here + +var myPenguin = { + "character name": "Captain Cook, Greta", + origin: "Mr. Popper's Penguin", + author: "Richard and Florence Atwater", +} + +myPenguin .canFly = "false"; ``` 4. Add a method to your penguin called chirp that prints to the console: "CHIRP CHIRP! Is this what penguins sound like?" ```js // your code goes here +myPenguin.chirp = function() { + alert("Chirp Chirp! Is this what penguins sound like?") +} + +myPenguin.chirp() ``` 6. Next, call your penguin's sayHello() method and make sure that it works! ```js // your code goes here +myPenguin.sayHello = function() { + alert("Hello"); +} + +myPenguin.sayHello() + ``` -7. Without modifying any of your previous code, change the penguin's name to "Penguin McPenguinFace" and then call your penguin's sayHello() function one more time to make sure it still works. +7. Without modifying any of your previous code, change the penguin's name to "Penguin McPenguinFace" and then call your function sayHello() one more time to make sure it still works. ```js // your code goes here +myPenguin.name = "Penguin McPenguinFace"; + +myPenguin.sayHello() + + ``` 8. Write another method called fly, and inside that method, use an if / else statement to print "I can fly!" to the console if your penguin's canFly property is true, or "No flying for me!" if its canFly property is false. ```js // your code goes here +myPenguin.fly = function() { + if (myPenguin.canFly === true) { + alert("I can fly"); + } else { + alert("No flying for me"); + } +} + ``` 9. Call your penguin's fly() method and make sure it works! ```js // your code goes here + +myPenguin.fly() ``` 10. Change the canFly property to true -- again, without modifying any of your previous code! ```js // your code goes here +myPenguin.canFly = false; ``` 11. Now call your penguin's fly() method again and make sure it works as expected! ```js // your code goes here +myPenguin.fly() ``` 12. Write a for ... in loop to print each key to the console. (Hint: See this page for an example of this special type of loop.) ```js // your code goes here + +for (let key in myPenguin) { + console.log(key); + } ``` 13. Write another for ... in loop to print the value of each key to the console. (Hint: You'll need to use bracket notation to access the values this way, instead of dot notation!) ```js // your code goes here + +for (let key in myPenguin) { + console.log(myPenguin[key]); + } ``` ## Exercise 2 @@ -84,6 +139,15 @@ ```js // your code goes here +var favoriteRecipes = { + title: "chicken", + serves: 2, + ingredients: ["cinnamon, cumin, code"], + } + + console.log(favoriteRecipes.title); + console.log(favoriteRecipes.serves); + console.log(favoriteRecipes.ingredients); ``` 15. Keep track of which books you read and which books you want to read! @@ -94,4 +158,55 @@ ```js // your code goes here +var allBooks = []; + +allBooks.push(book1) +allBooks.push(book2) +allBooks.push(book3) +allBooks.push(book4) +allBooks.push(book5) + +var booksToRead = []; + +var book1 = { + title: "Ullyses", + author: "James Joyce", + alreadyRead: true, + } +var book2 = { + title: "one good deed", + author: "David Baldacci", + alreadyRead: true, +} + +var book3 = { + title: "Under Currents", + author: "Nora Roberts", + alreadyRead: true, +} + +var book4 = { + title: "Thin Air", + author: "Lisa Gray", + alreadyRead: false, +} + +var book5 = { + title: "The Last Widow", + author: "Karin Slaughter", + alreadyRead: false, +} + +var booksAlreadyRead = []; + +for (i = 0; i < allBooks.length; i++) { + + if (allBooks[i].alreadyRead) { + booksAlreadyRead.push(allBooks[i]); + } else { + booksToRead.push(allBooks[i]); + } +} + + ``` diff --git a/1. objects/2.js b/1. objects/2.js index d5967e4..09ea4b2 100644 --- a/1. objects/2.js +++ b/1. objects/2.js @@ -1,23 +1,53 @@ // Using the different way of accessing and assigning a value to the object using `.` or `[]` +var user = { + name: "John", + age: 40 +} + +user.age // 40 +user.name // John +user["Native Place"] = "Dhramshala"; +user["Native Place"] //Dhramshala // 1. Define a variable named `user` and assign a blank object to it. +var user = {}; + // 2. Add a key of `user name` and a value of `Black Panther` to that object. +user["user name"] = "Black Panther"; + // 3. Using `console.log` log the value of `user name` key from the user object. +console.log(user["user name"]) + // 4. Add a key of the value of variable `batch` in the object with the value of 10. -var batch = "myBatch"; + +var myBatch = { + batch: 10 +} // 5. Using the alert function alert the value of the key added above. +alert(myBatch.batch); + // 6. Add a key of `42` to the object with a value of `The answer to the meaning of life 🧸`. +myBatch[42] = "The answer to the meaning of life 🧸" + // 7. Using the function console.log log the value of the key `42`. +console.log([42]); + // 8. Add a key in your object with the value of the variable `city`. The value of the key should be `true`. -var city = prompt("Enter the city name you visited las time."); + + +myBatch.city = "true"; + // 9. Uisng console.log log the value of the key defined above. +console.log(myBatch.city); + // 10. Can you define a key of `let or var` in any object? Reason. +Yes, it can be defined becuase in object there's no restrictions like in the variables. \ No newline at end of file diff --git a/2. array/1.md b/2. array/1.md index 1a23aee..39119da 100644 --- a/2. array/1.md +++ b/2. array/1.md @@ -2,70 +2,112 @@ ```js // your code goes here + +var colors = ["Purple", "Red", "Pink", "Blue", "Green"] ``` 2. Access the first color in the array and print it to the console using console.log() ```js // your code goes here + +console.log(colors[0]); ``` 3.Now do the same with the third color in the list. (Remember that array indexes start at 0, not at 1!) ```js // your code goes here + +console.log(colors[2]); + ``` 4.Write one line of code that changes the value of the last color in the list to "ultraviolet" (overwriting the previous value). ```js // your code goes here + +colors[4] = "ultraviolet"; + ``` 5. Create a new variable called fourthColor and set it equal to the fourth color in the list. ```js // your code goes here + +var fourthColor = colors[3]; + ``` 6.Add another color to the end of the list. ```js // your code goes here + +colors.push("orange") + ``` 7.Add another color to the beginning of the list. ```js // your code goes here + +colors.unshift("dark blue") + ``` 8. Print the length of the array to the console with console.log() ```js // your code goes here + +console.log(colors.length); + ``` 9.Remove the last color from the end of list, and then print the length of the array to the console one more time. ```js // your code goes here + +colors.pop("brown") + +console.log(colors.length); + ``` 10.Write a for loop to iterate through every color in the array and print each color's value to the console. ```js // your code goes here + +for (i = 0; i >= colors.length; i++) { + console.log(colors.length); +} + ``` 11.Copying from that loop you just wrote, modify it to print every color's value and every color's index in this format: 3, purple or 0, blue etc. ```js // your code goes here + +for(i=0; i ? -console.log(personObj2); // -> ? +console.log(personObj1); // -> ? //ouput {name: "Alex", age: 25} +console.log(personObj2); // -> ?//output {name: "John", age: 50} + // Guess the output var oldArray = []; var object = {}; object.newArray = oldArray; oldArray.push(10); -console.log(object.newArray === oldArray); +console.log(object.newArray === oldArray); //output true + // Guess the output var a = 5; var b = a; a = 10; -console.log(a); -console.log(b); +console.log(a); // 10 +console.log(b); // 5 // What's the output? var a = {}; var b = a; a.a = 1; -console.log(a); -console.log(b); +console.log(a); // {a: 1} +console.log(b); // {a: 1} // What's the output. var a = []; @@ -82,7 +98,7 @@ var b = a; a.push(1); console.log(a); // [1] console.log(b); // [1] -console.log(a === b); +console.log(a === b); // true // Clone the object person in clone var person = { @@ -94,4 +110,4 @@ var person = { } }; -var clone = {}; +var clone = person; diff --git a/4. array-and-objects/01/array-in-object.md b/4. array-and-objects/01/array-in-object.md index 0b60db0..91b50bb 100644 --- a/4. array-and-objects/01/array-in-object.md +++ b/4. array-and-objects/01/array-in-object.md @@ -2,7 +2,7 @@ Write the solution in the `main.js` 1. Add a new property to your penguin called favoriteFoods and set it equal to an array containing a list of three strings. -2. Access your penguin's second favorite food and print it to the console using console.log() +2. Access your penguin's second favorite food and print it out to the console using console.log() 3. Create a new variable called firstFavFood and set it equal to the first item in your penguin's array of favorite foods. diff --git a/4. array-and-objects/01/main.js b/4. array-and-objects/01/main.js index e69de29..3d3c152 100644 --- a/4. array-and-objects/01/main.js +++ b/4. array-and-objects/01/main.js @@ -0,0 +1,25 @@ + +var myPenguin = { + "character name": "Captain Cook, Greta", + origin: "Mr. Popper's Penguin", + author: "Richard and Florence Atwater", +} + +//myPenguin .canFly = "false"; + +myPenguin.favouriteFoods = ["fishburger, taco, mullets"] + +console.log(myPenguin.favouriteFoods[1]); + +myPenguin.firstFaveFood = myPenguin.favouriteFoods[0]; + +myPenguin.favouriteFoods.push("greyfish"); + +console.log(myPenguin.favouriteFoods.length); + +myPenguin.favouriteFoods[4] = "pineapples" + +var lastFaveFood = myPenguin.favouriteFoods[myPenguin.favouriteFoods.length-1] +for (var i = 0; i <= myPenguin.favouriteFoods.length; i++) { + console.log(myPenguin.favouriteFoods[i]) +} \ No newline at end of file diff --git a/4. array-and-objects/02/main.js b/4. array-and-objects/02/main.js index e69de29..52424bf 100644 --- a/4. array-and-objects/02/main.js +++ b/4. array-and-objects/02/main.js @@ -0,0 +1,69 @@ +var gunter = { + name: "Gunter", + origin: "Adventure Time", + canFly: false, + sayHello: function () { + console.log("QUACK!!!"); + } +}; + + var ramon = { + name: "Ramón", + origin: "Happy Feet", + canFly: true, + sayHello: function () { + console.log("Estoy encantado de conocerle."); + } +}; + + var fred = { + name: "Fred", + origin: "Sitting Ducks", + canFly: false, + sayHello: function () { + console.log("Hi there!"); + } +}; + + var penguins = [gunter, ramon, fred]; + + console.log(penguins[0]); + + var secondPenguin = penguins[1]; + + console.log(penguins[penguins.length - 1].name); + + +var myPenguin = { + "character name": "Captain Cook, Greta", + origin: "Mr. Popper's Penguin", + author: "Richard and Florence Atwater", + canFly: false, + sayHello: () // console.log('CHIRP CHIRP!') +} + + penguins.push(myPenguin); + + console.log(penguins.length); + + penguins[0].canFly = false; + + penguins[0].sayHello(); + + for (var i = 0; i < penguins.length; ++i) { + console.log(penguins[i].name); +} + + for (var i = 0; i < penguins.length; ++i) { + penguins[i].sayHello(); +} + + for (var i = 0; i < penguins.length; ++i) { + penguins[i].numberOfFeet = 2; +} + + for (var i = 0; i < penguins.length; ++i) { + if (penguins[i].canFly) { + console.log(`${penguins[i].name} can fly!`); + } +} diff --git a/4. array-and-objects/02/object-in-array.md b/4. array-and-objects/02/object-in-array.md index 2381de0..a0c89ec 100644 --- a/4. array-and-objects/02/object-in-array.md +++ b/4. array-and-objects/02/object-in-array.md @@ -1,7 +1,7 @@ Write the solution in the `main.js` -For these challenges, I'll create three penguins for you to work with. Copu paste these to your `js` files. +For these challenges, I'll create three penguins for you to work with. Copy paste these to your `js` files. ```js var gunter = { diff --git a/4. array-and-objects/03/main.js b/4. array-and-objects/03/main.js index e69de29..32300b2 100644 --- a/4. array-and-objects/03/main.js +++ b/4. array-and-objects/03/main.js @@ -0,0 +1,26 @@ + +var myPenguin = { + "character name": "Captain Cook, Greta", + origin: "Mr. Popper's Penguin", + author: "Richard and Florence Atwater", + canFly: false, + sayHello: () // console.log('CHIRP CHIRP!') +} + +myPenguin.outfit = { + hat: 'Baseball cap', + shirt: 'Hawaiian shirt', + pants: 'Cargo shorts', + shoes: 'Flip flops' +} + + var penguinHatType = myPenguin.outfit.hat; + + myPenguin.outfit.accessory = 'Pocket watch'; + + myPenguin.outfit.hat = 'Top hat'; + + delete myPenguin.outfit.pants; + + for (key in myPenguin.outfit) { + console.log(myPenguin.outfit[key]); \ No newline at end of file