Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 75 additions & 4 deletions Exercise 1 (Objects).md
Original file line number Diff line number Diff line change
@@ -1,47 +1,118 @@
## Exercise 1 (Objects)
## Solutions Exercise 1

#### 1. Pick a penguin from Wikipedia's 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)
[List of fictional penguins - Wikipedia](https://en.wikipedia.org/wiki/List_of_fictional_penguins).

Solution
```var myPenguin = {
character: "Tootsie the Penguin",
origin: "Donald Duck",
notes: "A baby penguin in the classic 1939 cartoon \"Donald's 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]!"

Solution
```
console.log("Hello, I'm a penguin and my name is " + myPenguin.character + "!");
```
---
#### 3. Write another line of code that adds a new property to your penguin called canFly and set it to false. (Note: Don't modify your penguin-creation code that you wrote above! Do this step in a separate line of code.)


Solution
```
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?" (Note: Again, don't modify your previous code! Do this step by writing a new line of code.)

Solution
```
myPenguin.chirp = function() {
console.log("CHIRP CHIRP! Is this what penguins sound like?");
};
// TO TEST IT, RUN THIS CODE: myPenguin.chirp();
```
---
#### 5. Add another method to your penguin called sayHello that prints to the console the same message from step 20 above. But this time, be sure to use the mystical, magical, all-powerful this keyword to access your penguin's name, so that way if you change its name later, your method will still work!

Solution
```
myPenguin.sayHello = function() {
console.log("Hello, I'm a penguin and my name is " + this.character + "!");
};
```
---
#### 6. Next, call your penguin's sayHello() method and make sure that it works! (Hint: if you need an example of what it looks like when you call a method of an object, look at console.log() -- that's how you call the log() method of the console object!)

Solution
```
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.

Solution
```
myPenguin.character = "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.


Solution
```
myPenguin.fly = function() {
if (this.canFly) {
console.log("I can fly!");
} else {
console.log("No flying for me!");
}
};
```
---
#### 9. Call your penguin's fly() method and make sure it works!

Solution
```
myPenguin.fly();
```

---
#### 10. Change the canFly property to true -- again, without modifying any of your previous code!


Solution
```
myPenguin.canFly = true;
```
---
#### 11. Now call your penguin's fly() method again and make sure it works as expected!

Solution
```
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.
[for...in - JavaScript \| MDN](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Sentencias/for...in))

Solution
```
for (property in myPenguin) {
console.log(property);
}
```
---
#### 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!)

Solution
```
for (property in myPenguin) {
console.log(myPenguin[property]);
}
```


36 changes: 35 additions & 1 deletion Exercise 2 (Objects and Arrays).md
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
## Exercise 2 (Objects and Arrays)
## Solutions Exercise 2

#### 1. Add a new property to your penguin called favoriteFoods and set it equal to an array containing a list of three strings.

Solution
```
myPenguin.favoriteFoods = ["fish", "ice cream", "hot chocolate"];
```
---
#### 2. Access your penguin's second favorite food and print it to the console using console.log()

Solution
```
console.log(myPenguin.favoriteFoods[1]);
```
---
#### 3. Create a new variable called firstFavFood and set it equal to the first item in your penguin's array of favorite foods.

Solution
```
var firstFavFood = myPenguin.favoriteFoods[0];
```
---
#### 4. Add another food to the end of the list.

Solution
```
myPenguin.favoriteFoods.push("sushi");
```
---
#### 5. Print the length of your penguin's favoriteFoods array to the console with console.log()

Solution
```
console.log(myPenguin.favoriteFoods.length);
```
---
#### 6. Without modifying any of your previous code, write a new line of code that changes the value of the last item in the list to "pineapples" (overwriting the previous value).

Solution
```
myPenguin.favoriteFoods[3] = "pineapples";
```
---
#### 7. Create a new variable named lastFavFood that will always point to the last element of your penguin's favoriteFoods array, no matter how many items are in the list.

Solution
```
var lastFavFood = myPenguin.favoriteFoods[ myPenguin.favoriteFoods.length - 1 ];
```
---
#### 8. Write a for loop to iterate through every food in your penguin's favoriteFood property and print each one to the console.

Solution
```
for (var index = 0; index < myPenguin.favoriteFoods.length; index++) {
console.log(myPenguin.favoriteFoods[index]);
}
```

36 changes: 34 additions & 2 deletions Exercise 3 (Objects and Objects).md
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
## Exercise 3 (Objects and Objects)
## Solutions Exercise 3

#### 1. Add a new property to your penguin called outfit and set it equal to another object with the following properties: hat, shirt, pants, and shoes -- each property should have a string as its value! (I suggest you give it a baseball cap, Hawaiian shirt, cargo shorts, and flip-flops, because wouldn't that be ridiculous?)

Solution
```
myPenguin.outfit = {
hat: "baseball cap",
shirt: "Hawaiian shirt",
pants: "cargo shorts",
shoes: "flip-flops"
};
```
---
#### 2. Create a new variable called penguinHatType and set it equal to the value of the hat in your penguin's outfit! Then print your new variable to the console.

Solution
```
var penguinHatType = myPenguin.outfit.hat;
console.log(penguinHatType);
```
---
#### 3. Without modifying any of your previous code, write one new line of code to add an accessory property to your penguin's outfit and set it equal to the string "pocket watch" -- because penguins are classy like that!

Solution
```
myPenguin.outfit.accessory = "pocket watch";
```
---
#### 4. Write one more line of code to change the hat in your penguin's outfit to "top hat" and override the previous value. (Again, because penguins are classy!)

Solution
```
myPenguin.outfit.hat = "top hat";
```
---
#### 5. This penguin is a freelancer who always works from home, so it doesn't actually need to wear any pants! Let's delete the pants property from this penguin's outfit property. (Hint: see this page on the delete operator for examples.
[operador delete - JavaScript /| MDN](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Operadores/delete))
[operador delete - JavaScript \| MDN](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Operadores/delete))

Solution
```
delete myPenguin.outfit.pants;
```
---
#### 6. Write a for ... in loop to print the value of each piece of clothing in your penguin's outfit so you can see a list of clothing items in the console.

Solution
```
for (property in myPenguin.outfit) {
console.log(myPenguin.outfit[property]);
}
```

61 changes: 59 additions & 2 deletions Exercise 4 (Arrays and Objects).md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Exercise 4 (Arrays and Objects)
## Solutions Exercise 4
### For these last few challenges, I'll create three penguins for you to work with:
```
var gunter = {
Expand Down Expand Up @@ -31,37 +31,94 @@ var fred = {
---
#### 1. Create a new variable named penguins and set it equal to an array that lists these three penguins! (Hint: remember you can put variable names inside an array, not just hard-coded values! And remember that variable names don't have quotes around them.)

Solution
```
var penguins = [gunter, ramon, fred];
```
---
#### 2. Access the first penguin in the list and print it to the console using console.log() -- notice that you can see all the properties and methods of that object listed in the console! (Hint: remember that array indexes start counting at 0, not 1!)

Solution
```
console.log(penguins[0]);
```
---
#### 3. Create a new variable called secondPenguin and set it equal to the second penguin in your penguins array.

Solution
```
var secondPenguin = penguins[1];
```
---
#### 4. Print to the console the name of the last penguin in the list.

Solution
```
console.log(penguins[2]);
```
---
#### 5. Remember the penguin you created earlier, with the variable name of myPenguin? Add that penguin to the end of the penguins array!

Solution
```
penguins.push(myPenguin);
```
---
#### 6. Print the length of the penguins array to the console.

Solution
```
console.log(penguins.length);
```
---
#### 7. Write one more line of code to change the first penguin's canFly property to the value true (overriding its existing value).

Solution
```
penguins[0].canFly = true;
```
---
#### 8. Call the sayHello method of the first penguin in your penguins array!

Solution
```
penguins[0].sayHello();
```
---
#### 9. Write a for loop to iterate through every penguin in the array and print the value of each penguin's name property to the console.

Solution
```
for (var index = 0; index < penguins.length; index++) {
console.log(penguins[index].name);
}
```
---
#### 10. Write a for loop to call the sayHello method of every penguin in the array!

Solution
```
for (var index = 0; index < penguins.length; index++) {
penguins[index].sayHello();
}
```
---
#### 11. Write a for loop to iterate through every penguin in the array, and add a new property to each penguin called numberOfFeet with the value 2

Solution
```
for (var index = 0; index < penguins.length; index++) {
penguins[index].numberOfFeet = 2;
}
```
---
#### 12. Write another for loop to iterate through every penguin in the array, and for each penguin that can fly, print to the console a message containing the penguin's name and " can fly!" -- for example, "Gunter can fly!" or "Ramón can fly!" (Don't do anything for the penguins that cannot fly.)

Solution
```
for (var index = 0; index < penguins.length; index++) {
if (penguins[index].canFly) {
console.log(penguins[index].name + " can fly!");
}
}
```