diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..9abcdf9 Binary files /dev/null and b/.DS_Store differ diff --git a/1. objects/1.md b/1. objects/1.md index 5110277..8fb3fc7 100644 --- a/1. objects/1.md +++ b/1. objects/1.md @@ -1,73 +1,142 @@ -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). + 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 : "Patrick", + canFly : false, + hello : function sayHello(){ + return (`Hello, I'm a penguin and my name is ${myPenguin.penguin.name}!`); + }, + chirp : function chrip(){ + return "CHIRP CHIRP! Is this what penguins sound like?"; + }, + fly : function fly(){ + if (canFly == true) { + consloe.log("I can fly!"); + } else { + console.log("No flying for me!"); + } + }, + 1 : { "Literature": + { + 1.1 : "comics" + } + }, + 2 : "Theatre", + 3 : {"in media": + { + 3.1 : "Animations", + 3.2 : "video games", + 3.3 : " Other media (toys, mascots, logos, etc.)" + } + }, + 4 : "See also", + 5 : "References", +}; +for(let key in myPenguin){ + console.log(Object.keys(myPenguin.penguin); +} + + + + ``` 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.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 + canFly : false +console.log(myPenguin["penguin"]["canFly"]) ``` 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 +chirp : chrip(){ + return "CHIRP CHIRP! Is this what penguins sound like?" + } +console.log(myPenguin.penguin.chrip) ``` 6. Next, call your penguin's sayHello() method and make sure that it works! ```js // your code goes here +hello : sayHello(){ + return `Hello, I'm a penguin and my name is ${this.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["penguin"]["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 +fly : function fly(){ + if (canFly == true) { + consloe.log("I can fly!"); + } else { + console.log("No flying for me!"); + } + } + ``` 9. Call your penguin's fly() method and make sure it works! ```js // your code goes here +myPenguin["penguin"]["fly"] ``` 10. Change the canFly property to true -- again, without modifying any of your previous code! ```js // your code goes here +myPenguin["penguin"]["canFly" = true ] ``` 11. Now call your penguin's fly() method again and make sure it works as expected! ```js // your code goes here +myPenguin["penguin"]["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 +153,13 @@ ```js // your code goes here +let favouriteRecepie = { + title: Mole, + serves: 2, + ingrediants: ["cinnamon","cumin","cocoa"] +} + + ``` 15. Keep track of which books you read and which books you want to read! @@ -94,4 +170,26 @@ ```js // your code goes here +let books = [ + { + title : "Eclipse", + author : "Stephenie Meyer", + alreadyRead : true, + }, + { + title : "Harry Potter", + author : "J.K Rowling", + alreadyRead : false, + } +]; + +for (let book in books) { + console.log(`${books[book]["title"]} by ${books[book]["author"]}`); + if (books[book]["alreadyRead"]) { + console.log(`You already read ${books[book]["title"]} by ${books[book]["author"]}`); + } + else { + console.log(`You still need to read ${books[book]["title"]} by ${books[book]["author"]}`); + } +} ``` diff --git a/1. objects/2.js b/1. objects/2.js index d5967e4..7c23edc 100644 --- a/1. objects/2.js +++ b/1. objects/2.js @@ -1,23 +1,56 @@ // Using the different way of accessing and assigning a value to the object using `.` or `[]` - +var obj = { + name:"sunny", + age:21, + qualification:"B.tech" +} +console.log(obj.name); +console.log(obj["age"]); // 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. +var user = { + userName:"Black Panther", + 10:batch, + 42:`The answer to the meaning of life 🧸` +} // 3. Using `console.log` log the value of `user name` key from the user object. - +console.log(user.userName); // 4. Add a key of the value of variable `batch` in the object with the value of 10. var batch = "myBatch"; +var user = { + userName:"Black Panther", + 10:batch, + 42:`The answer to the meaning of life 🧸` +} // 5. Using the alert function alert the value of the key added above. +alert(user[10]); // 6. Add a key of `42` to the object with a value of `The answer to the meaning of life 🧸`. - +var batch = "myBatch"; +var user = { + userName:"Black Panther", + 10:batch, + 42:`The answer to the meaning of life 🧸` +} // 7. Using the function console.log log the value of the key `42`. - +console.log(user[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."); +var batch = "myBatch"; +var user = { + userName:"Black Panther", + 10:batch, + 42:`The answer to the meaning of life 🧸`, + true:city, + let :"you", + var :"me" +} // 9. Uisng console.log log the value of the key defined above. - +console.log(user[true]) // 10. Can you define a key of `let or var` in any object? Reason. +yes we can take var or let as key because objects just understand key value pairs. but it will target all var inside the obj. \ No newline at end of file diff --git a/2. array/1.md b/2. array/1.md index 1a23aee..123f54d 100644 --- a/2. array/1.md +++ b/2. array/1.md @@ -2,70 +2,97 @@ ```js // your code goes here +let colors =["red","blue","green","cyan","magenta"] ``` 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" +console.log(colors[4]) ``` 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]; +console.log(forthColor); ``` 6.Add another color to the end of the list. ```js // your code goes here +colors.push("green") +console.log(colors) ``` 7.Add another color to the beginning of the list. ```js // your code goes here +colors.unshift("black") +console.log(colors) ``` 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) ``` 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 values in colors){ + console.log(colors[values]) +} ``` 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 (values = 0; values < colors.length ; values++){ + console.log(`the index of ${colors[values]}, is ${[values]}`); +} +OR // +for (let values in colors){ + console.log(`the index of ${colors[values]}, is ${values}`); +} ``` 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 diff --git a/3. copy-vs-reference/index.js b/3. copy-vs-reference/index.js index f4efbd0..2b71295 100644 --- a/3. copy-vs-reference/index.js +++ b/3. copy-vs-reference/index.js @@ -4,6 +4,10 @@ var x = 10; var y = "abc"; var a = x; var b = y; +//value of x = 10 +//value of y = "abc" +//value of a = 10 +//value of b = "abc" // Value of x, y, a, b var x = 10; @@ -12,20 +16,37 @@ var a = x; var b = y; a = 5; b = "def"; +//value of x = 10 +//value of y = "abc" +//value of a = 10 initially +//value of b = "abc" initially +//value of a = 5 +//value of b = "def" // Value of arr & arrCopy var arr = [1]; var arrCopy = arr; arr.push(2); +//value of arr = [1] initially +//value of arrCopy = [1] initially +// value of arr = [1,2] +//value of arrCopy = [1,2] + // Value of obj var obj = { name: "ryan" }; obj = { surname: "florance" }; +//value of obj = { name: "ryan" }; initially +//value of obj = { surname: "florance" }; + // What's the output. var arr = ["Hi"]; var arr2 = arr; console.log(arr === arr2); +// value of arr = ["Hi"]; +// value of arr2 = ["Hi"]; +console.log(arr === arr2)// true // What's the output. var arr1 = ["Hi!"]; @@ -33,8 +54,15 @@ var arr2 = ["Hi!"]; console.log(arr1 === arr2); console.log(arr == arr2); +// value of arr1 = ["Hi!"]; +// value of arr2 = ["Hi!"]; +console.log(arr1 === arr2);// false +console.log(arr == arr2); // false + //What's the output console.log([10] === [10]); +// false + // What's the output? @@ -47,13 +75,14 @@ function personDetails(person) { return person; } + var personObj1 = { name: "Alex", age: 30 }; var personObj2 = personDetails(personObj1); -console.log(personObj1); // -> ? -console.log(personObj2); // -> ? +console.log(personObj1); // -> {name: "Alex",age: 30} +console.log(personObj2); // -> {name: "John",age: 50} // Guess the output var oldArray = []; @@ -61,20 +90,21 @@ var object = {}; object.newArray = oldArray; oldArray.push(10); console.log(object.newArray === oldArray); +// 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 +112,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 +124,6 @@ var person = { } }; -var clone = {}; +var clone = {...person}; +//OR +clone = Object.assign(person); \ No newline at end of file diff --git a/4. array-and-objects/.DS_Store b/4. array-and-objects/.DS_Store new file mode 100644 index 0000000..9869cc1 Binary files /dev/null and b/4. array-and-objects/.DS_Store differ diff --git a/4. array-and-objects/01/main.js b/4. array-and-objects/01/main.js index e69de29..c34c817 100644 --- a/4. array-and-objects/01/main.js +++ b/4. array-and-objects/01/main.js @@ -0,0 +1,46 @@ + +var foods =["pizza","burger","pasta","takos"]; +var myPenguin = { + name : "Patrick", + canFly : false, + sayHello : function sayHello(){ + return (`Hello, I'm a penguin and my name is ${myPenguin.penguin.name}!`); + }, + chirp : function chrip(){ + return "CHIRP CHIRP! Is this what penguins sound like?"; + }, + fly : function fly(){ + if (canFly == true) { + consloe.log("I can fly!"); + } else { + console.log("No flying for me!"); + } + } , + favouriteFoods: foods, + firstFavFood : foods[1], + lastFavFood : favouriteFoods[favouriteFoods.length - 1], + 1 : { "Literature": + { + 1.1 : "comics" + } + }, + 2 : "Theatre", + 3 : {"in media": + { + 3.1 : "Animations", + 3.2 : "video games", + 3.3 : " Other media (toys, mascots, logos, etc.)" + } + }, + 4 : "See also", + 5 : "References", +}; +console.log(myPenguin["favouriteFoods"][1]); + myPenguin["favouriteFoods"].push("takos"); +console.log(myPenguin["favouriteFoods"].length); + +for (i = 0; i<(myPenguin["favouriteFoods"].length);i++){ + console.log(myPenguin["favouriteFoods"][i]); + } + + diff --git a/4. array-and-objects/02/main.js b/4. array-and-objects/02/main.js index e69de29..b5baa6b 100644 --- a/4. array-and-objects/02/main.js +++ b/4. array-and-objects/02/main.js @@ -0,0 +1,87 @@ +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]; + +var foods =["pizza","burger","pasta","takos"]; +var myPenguin = { + name : "Patrick", + canFly : false, + sayHello : function (){ + return (`Hello, I'm a penguin and my name is ${myPenguin.penguin.name}!`); + }, + chirp : function chrip(){ + return "CHIRP CHIRP! Is this what penguins sound like?"; + }, + fly : function fly(){ + if (canFly == true) { + consloe.log("I can fly!"); + } else { + console.log("No flying for me!"); + } + } , + favouriteFoods: foods, + firstFavFood : favouriteFoods[1], + lastFavFood : favouriteFoods[favouriteFoods.length - 1], + 1 : { "Literature": + { + 1.1 : "comics" + } + }, + 2 : "Theatre", + 3 : {"in media": + { + 3.1 : "Animations", + 3.2 : "video games", + 3.3 : " Other media (toys, mascots, logos, etc.)" + } + }, + 4 : "See also", + 5 : "References", +}; + console.log(penguins[0]); + var secondPenguin = penguins[1]; + console.log(penguins[2]); + penguins.push(myPenguin); + console.log(penguins.length); + gunter.canFly = true; + gunter.sayHello(); + for (let penguin in penguins){ + console.log (penguins[penguin]); + } + for (var penguin in penguins){ + console.log(penguins[penguin].sayHello()); + } + for (var penguin in penguins){ + console.log(penguins[penguin].noOfFeet = 2); + } + for (var penguin in penguins){ + if (penguins[penguin].canFly === true) { + console.log(`penguin ${penguins[penguin].name} can Fly`); + } + } + \ No newline at end of file diff --git a/4. array-and-objects/03/main.js b/4. array-and-objects/03/main.js index e69de29..3a67c4c 100644 --- a/4. array-and-objects/03/main.js +++ b/4. array-and-objects/03/main.js @@ -0,0 +1,110 @@ +var gunter = { + name: "Gunter", + origin: "Adventure Time", + canFly: false, + sayHello: function () { + console.log("QUACK!!!"); + }, + favouriteFoods: foods, + firstFavFood : favouriteFoods[1], + lastFavFood : favouriteFoods[favouriteFoods.length - 1], + outFit :outfit , + }; + + var ramon = { + name: "Ramón", + origin: "Happy Feet", + canFly: true, + sayHello: function () { + console.log("Estoy encantado de conocerle."); + }, + favouriteFoods: foods, + firstFavFood : favouriteFoods[1], + lastFavFood : favouriteFoods[favouriteFoods.length - 1], + outFit :outfit , + }; + + var fred = { + name: "Fred", + origin: "Sitting Ducks", + canFly: false, + sayHello: function () { + console.log("Hi there!"); + }, + favouriteFoods: foods, + firstFavFood : foods[1], + lastFavFood : foods[foods.length - 1], + outFit :outfit , + }; + +var myPenguin = { + name : "Patrick", + canFly : false, + sayHello : function (){ + return (`Hello, I'm a penguin and my name is ${myPenguin.penguin.name}!`); + }, + chirp : function chrip(){ + return "CHIRP CHIRP! Is this what penguins sound like?"; + }, + fly : function fly(){ + if (canFly == true) { + consloe.log("I can fly!"); + } else { + console.log("No flying for me!"); + } + } , + favouriteFoods: foods, + firstFavFood : foods[1], + lastFavFood : foods[foods.length - 1], + outFit :outfit , + + 1 : { "Literature": + { + 1.1 : "comics" + } + }, + 2 : "Theatre", + 3 : {"in media": + { + 3.1 : "Animations", + 3.2 : "video games", + 3.3 : " Other media (toys, mascots, logos, etc.)" + } + }, + 4 : "See also", + 5 : "References", +}; +var outfit = { + hat : "baseball cap", + shirt : "Hawaiian shirt", + pants : "cargo shorts", + shoes : "flip-flops" +} +for (var penguin in penguins) { + penguin["outFit"] = outfit; +} +var penguins = [gunter , ramon , fred, myPenguin]; +var foods =["pizza","burger","pasta","takos"]; +for (var penguin in penguins){ + penguinHatType = penguin["outFit"].hat; +} +for (var penguin in penguins) { + penguin["outFit"].accessory = "pocket watch"; +} +for (var penguin in penguins) { + penguin["outfit"].hat = "top hat"; +} +for (var penguin in penguins) { + delete penguin["outFit"].pants; +} +for (var penguin in penguins) { + console.log(`${penguins[penguin].outFit}`); +} + + + + + + + + \ No newline at end of file diff --git a/5. extra/.DS_Store b/5. extra/.DS_Store new file mode 100644 index 0000000..4018d07 Binary files /dev/null and b/5. extra/.DS_Store differ