From 9f4523fefb83c24c4c78265bc26f1e5743f9183c Mon Sep 17 00:00:00 2001 From: Gangaprasad Mohite Date: Tue, 10 Sep 2019 20:13:46 +0530 Subject: [PATCH 1/3] solved exercise --- 1. objects/1.md | 52 ++++++++++++++++++++++++++++++++++++------------- 2. array/1.md | 37 ++++++++++++++--------------------- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/1. objects/1.md b/1. objects/1.md index 5110277..ea8e8aa 100644 --- a/1. objects/1.md +++ b/1. objects/1.md @@ -1,73 +1,95 @@ 1. Pick a penguin from Wikipedia's List of Fictional Penguins (https://en.wikipedia.org/wiki/List_of_fictional_penguins) and create an object named `myPenguin` with properties that represent the information listed in each column on that Wikipedia page (for example: the character's name, origin, and author). ```js -// your code goes here +var myPenguin = { + name : "Parker", + origin : "Love Birds", + author : "RObert J. Sherman", + sayHello : sayHello() { + console.log(`Hello, I'm a penguin and my name is ${myPenguin.name}!`) +} +} ``` 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 +function sayHello(){ + return(`Hello, I'm a penguin and my name is ${myPenguin.name}!`) +} +myPenguin.sayHello ``` 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 +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'] = console.log ("CHIRP CHIRP! Is this what penguins sound like?") ``` 6. Next, call your penguin's sayHello() method and make sure that it works! ```js -// your code goes here + myPenguin['sayHello'] = function () { + console.log (myPenguin.name) + } ``` 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. ```js -// your code goes here +myPenguin.name = "Penguin McPenguinFace" ``` 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(canFly == true){ + return " I can fly!" + }else { + return "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 = true; ``` 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 i in myPenguin){ + console.log( i , myPenguin[i]) +} ``` 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 i in myPenguin){ + console.log( i , myPenguin[i]) +} ``` ## Exercise 2 @@ -83,7 +105,11 @@ ``` ```js -// your code goes here +let favouriteReciepe = { + title : "Mole", + serve : 2, + ingredients : ["Cinnamon", "cumin", "cocoa"] +} ``` 15. Keep track of which books you read and which books you want to read! diff --git a/2. array/1.md b/2. array/1.md index 1a23aee..6ddc8ed 100644 --- a/2. array/1.md +++ b/2. array/1.md @@ -1,71 +1,64 @@ 1. Create an array named colors that contains five different names of colors as strings. ```js -// your code goes here -``` - -2. Access the first color in the array and print it to the console using console.log() - -```js -// your code goes here -``` - -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 +var colors = ["green", "purple", "violet", "yellow", "black"] ``` 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[colors.length - 1 ] = "ultraviolet" ``` 5. Create a new variable called fourthColor and set it equal to the fourth color in the list. ```js -// your code goes here +let fourthColor = colors[3]; ``` 6.Add another color to the end of the list. ```js -// your code goes here +colors.push("Goldenrod") ``` 7.Add another color to the beginning of the list. ```js -// your code goes here +colors.unshift("navy") ``` 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(); +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(let i = 1; i < colors.length; i++) { + console.log(colors[i]); +} ``` 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(let i = 1; i < colors.length; i++) { + console.log(i, colors[i]); +} ``` 12.Create a variable named lastColor that will always point to the last element of the colors array, no matter how many colors are in the list. (Hint: make use of the array's length property for this!) ```js -// your code goes here +let lastColor = colors.length - 1 ``` \ No newline at end of file From 1a1fb1f09925852b315f836548ff5fd2acf680e8 Mon Sep 17 00:00:00 2001 From: Gangaprasad Mohite Date: Sun, 15 Sep 2019 21:03:36 +0530 Subject: [PATCH 2/3] solved copy vs reference --- 3. copy-vs-reference/index.js | 40 +++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/3. copy-vs-reference/index.js b/3. copy-vs-reference/index.js index f4efbd0..06ff5ce 100644 --- a/3. copy-vs-reference/index.js +++ b/3. copy-vs-reference/index.js @@ -5,6 +5,11 @@ var y = "abc"; var a = x; var b = y; +x= 10 +y= "abc" +a= 10 +b= "abc" + // Value of x, y, a, b var x = 10; var y = "abc"; @@ -13,29 +18,44 @@ var b = y; a = 5; b = "def"; +x = 10 +y = "abc" +a = 5 +b = "def" + // Value of arr & arrCopy var arr = [1]; var arrCopy = arr; arr.push(2); +arr = [1,2] +arrCopy = [1, 2] + + // Value of obj var obj = { name: "ryan" }; obj = { surname: "florance" }; +obj = {surname: "florance"} + // What's the output. var arr = ["Hi"]; var arr2 = arr; console.log(arr === arr2); +true + // What's the output. var arr1 = ["Hi!"]; var arr2 = ["Hi!"]; console.log(arr1 === arr2); console.log(arr == arr2); +false + //What's the output console.log([10] === [10]); - +false // What's the output? function personDetails(person) { @@ -55,6 +75,10 @@ var personObj2 = personDetails(personObj1); console.log(personObj1); // -> ? console.log(personObj2); // -> ? +{name: "Alex", age: 25} +{name: "John", age: 50} + + // Guess the output var oldArray = []; var object = {}; @@ -62,6 +86,8 @@ object.newArray = oldArray; oldArray.push(10); console.log(object.newArray === oldArray); +true + // Guess the output var a = 5; var b = a; @@ -69,6 +95,9 @@ a = 10; console.log(a); console.log(b); +10 +5 + // What's the output? var a = {}; var b = a; @@ -76,6 +105,9 @@ a.a = 1; console.log(a); console.log(b); +{a: 1} +{a: 1} + // What's the output. var a = []; var b = a; @@ -84,6 +116,10 @@ console.log(a); // [1] console.log(b); // [1] console.log(a === b); +[1] +[1] +true + // Clone the object person in clone var person = { name: "Mark", @@ -94,4 +130,4 @@ var person = { } }; -var clone = {}; +var clone = {person}; From c324f65a002027d666cd5d7f506d5db0e6e2642c Mon Sep 17 00:00:00 2001 From: Gangaprasad Mohite Date: Sun, 15 Sep 2019 21:20:33 +0530 Subject: [PATCH 3/3] finished js-array-objects --- 4. array-and-objects/01/main.js | 33 +++++++++++++ 4. array-and-objects/02/main.js | 84 +++++++++++++++++++++++++++++++++ 4. array-and-objects/03/main.js | 31 ++++++++++++ 3 files changed, 148 insertions(+) diff --git a/4. array-and-objects/01/main.js b/4. array-and-objects/01/main.js index e69de29..296985a 100644 --- a/4. array-and-objects/01/main.js +++ b/4. array-and-objects/01/main.js @@ -0,0 +1,33 @@ +var myPenguin = { + name : "Humboldt", + origin : "Penguin Island", + author : "Kugane Maruyama", +} + +// 1 +myPenguin.favoriteFoods = ["Cookies", "Pinapple", "Strawberry"]; + +// 2 +var secondFavFood = myPenguin.favoriteFoods[1]; +console.log(secondFavFood); + +// 3 +var firstFavFood = myPenguin.favoriteFoods[0]; + +// 4 +myPenguin.favoriteFoods.push("Chocolates"); + +// 5 +var len = myPenguin.favoriteFoods.length; +console.log(len); + +// 6 +myPenguin.favoriteFoods[len - 1] = "Litchi"; + +// 7 +var lastFavFood = myPenguin.favoriteFoods[len - 1]; + +// 8 +for (var i = 0; i < len; i++) { + console.log(myPenguin.favoriteFoods[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..a04dde6 100644 --- a/4. array-and-objects/02/main.js +++ b/4. array-and-objects/02/main.js @@ -0,0 +1,84 @@ +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!"); + } + }; + + // 1 + var penguins = [gunter, ramon, fred]; + + // 2 + var firstPenguin = penguins[0]; + console.log(firstPenguin); + + // 3 + var secondPenguin = penguins[1]; + + // 4 + var len = penguins.length; + var lastPenguin = penguins[len - 1]; + console.log(lastPenguin); + + // 5 + var myPenguin = { + name : "Humboldt", + origin : "Penguin Island", + author : "Kugane Maruyama", + } + + penguins.push(myPenguin); + + // 6 + console.log(len); + + // 7 + penguins[0].canFly = true; + + // 8 + penguins[0].sayHello(); + + // 9 + for (var i = 0; i < len; i++) { + var name = penguins[i].name; + console.log(name); + } + + // 10 + for (var i = 0; i < len; i++) { + var hello = penguins[i].sayHello(); + console.log(hello); + } + + // 11 + for (var i = 0; i < len; i++) { + penguins[i].numberOfFeets = 2; + } + + // 12 + for (var i = 0; i < len; i++) { + var fly = penguins[i].canFly; + if (fly) { + console.log(`${penguins[i].name} can fly!`); + } + } diff --git a/4. array-and-objects/03/main.js b/4. array-and-objects/03/main.js index e69de29..50a4f9d 100644 --- a/4. array-and-objects/03/main.js +++ b/4. array-and-objects/03/main.js @@ -0,0 +1,31 @@ +var myPenguin = { + name : "Humboldt", + origin : "Penguin Island", + author : "Kugane Maruyama", +} + +// 1 +myPenguin.outfit = { + hat : "Fedora", + shirt : "T-shrt", + pants : "Jeans", + shoes : "Under Armour", +}; + +// 2 +var penguinsHatType = myPenguin.outfit.hat; +console.log(penguinsHatType); + +// 3 +myPenguin.outfit.accessory = "Sunglasses"; + +// 4 +myPenguin.outfit.hat = "Fedora"; + +// 5 +delete myPenguin.outfit.pants; + +// 6 +for (var i in myPenguin.outfit) { + console.log(i); +} \ No newline at end of file