diff --git a/1. objects/1.md b/1. objects/1.md index 5e2179d..5192476 100644 --- a/1. objects/1.md +++ b/1. objects/1.md @@ -1,73 +1,94 @@ 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: "Whiteblack", + origin: "Whiteblack the Penguin sees the world", + author: "H.A. Rey and Margaret Rey", +} + ``` 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 +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() { + console.log("CHIP 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 + + ``` 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(myPenguin.canFly == true) { + console.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.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. ```js -// your code goes here +for(var i=0;i ? -console.log(personObj2); // -> ? +console.log(personObj1); // -> name:Alex Age: 25 +console.log(personObj2); // -> ?name: John Age: 30 // Guess the output var oldArray = []; @@ -61,6 +62,7 @@ var object = {}; object.newArray = oldArray; oldArray.push(10); console.log(object.newArray === oldArray); +//true // Guess the output var a = 5; @@ -68,13 +70,16 @@ var b = a; a = 10; console.log(a); console.log(b); - +//10 +//5 // What's the output? var a = {}; var b = a; a.a = 1; console.log(a); console.log(b); +//1 +//1 // What's the output. var a = []; @@ -83,6 +88,7 @@ a.push(1); console.log(a); // [1] console.log(b); // [1] console.log(a === b); +//true // Clone the object person in clone var person = { @@ -95,6 +101,7 @@ var person = { }; var clone = {}; +clone = {...person}; // Output of the following let brothers = ["John", "Bran", "Robb"]; @@ -117,15 +124,15 @@ let user3 = { brothers: ["John", "Bran", "Robb"] }; // Output of the below code and why? -user.house === user2.house; // output: -user.house == user2.house; // output: -user.brothers === user2.brothers; // output: -user.brothers == user2.brothers; // output: -user.name == user2.name; // output: -user.name === user2.name; // output: -user.brothers == user3.brothers; // output: -user.brothers === user3.brothers; // output: -user.house === user2.house; // output -user.house === user3.house; // output -user.brothers[0] === user2.brothers[0]; // output -user.brothers[0] === user3.brothers[0]; // output +user.house === user2.house; // output: true +user.house == user2.house; // output: true +user.brothers === user2.brothers; // output: true +user.brothers == user2.brothers; // output: true +user.name == user2.name; // output: true +user.name === user2.name; // output: true +user.brothers == user3.brothers; // output: false +user.brothers === user3.brothers; // output: false +user.house === user2.house; // output: true +user.house === user3.house; // output: true +user.brothers[0] === user2.brothers[0]; // output: true +user.brothers[0] === user3.brothers[0]; // output: true diff --git a/4. array-and-objects/01/main.js b/4. array-and-objects/01/main.js index e69de29..00f146e 100644 --- a/4. array-and-objects/01/main.js +++ b/4. array-and-objects/01/main.js @@ -0,0 +1,15 @@ +var myPenguin ={ + name: "Whiteblack", + origin: "Whiteblack the Penguin sees the world", + author: "H.A. Rey and Margaret Rey", + favoriteFoods: ["eggs","apples","oranges"], +} +console.log(myPenguin.favoriteFoods[1]); +var firstFavFood = myPenguin.favoriteFoods[0]; +myPenguin.favoriteFoods.push("chocolate"); +console.log(myPenguin.favoriteFoods.length); +myPenguin.favoriteFoods[myPenguin.favoriteFoods.length-1] = "pineapples"; +var lastFavFood = myPenguin.favoriteFoods[myPenguin.favoriteFoods.length-1]; +for(var i=0;i