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
167 changes: 93 additions & 74 deletions 1. objects/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,115 @@

```js
// your code goes here
var myPenguin = {
penguinId: 01,
Name : "Penguins",
Origin: "W Penguin Island",
Author: "Anatole France",
Notes:"Satirical version of French history"
}
```

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

3. Write another line of code that adds a new property to your penguin called canFly and set it to false.

```js
console.log(`Hello, I'm a penguin and my name is ${myPenguin["name"]}`)
Write another line of code that adds a new property to your penguin called canFly and set it to false.
// your code goes here
```

4. Add a method to your penguin called chirp that prints to the console: "CHIRP CHIRP! Is this what penguins sound like?"

```js
myPenguin.canFly = false;
Add a method to your penguin called chirp that prints to the console: "CHIRP CHIRP! Is this what penguins sound like?"
// your code goes here
```

6. Next, call your penguin's sayHello() method and make sure that it works!

```js
myPenguin.chrip = function chrip() {
console.log("CHIRP CHIRP! Is this what penguins sound like?");
}
Next, call your penguin's sayHello() method and make sure that it works!
// 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
myPenguin.sayHello = function sayHello() {
console.log(`Hello, I'm a penguin and my name is ${myPenguin["name"]}`)
}
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.
// your code goes here
```

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
myPenguin["name"] = "Penguin McPenguinFace";
myPenguin.sayHello = function sayHello() {
console.log(`Hello, I'm a penguin and my name is ${myPenguin["name"]}`)
}
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.
// your code goes here
```

9. Call your penguin's fly() method and make sure it works!

```js
myPenguin.fly = function fly() {
if(myPenguin.canFly == true) {
console.log("I can fly!");
}else {
console.log("No flying for me!");
}
}
Call your penguin's fly() method and make sure it works!
// your code goes here
```

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

```js
myPenguin.fly();
// No flying for me!
Change the canFly property to true -- again, without modifying any of your previous code!
// your code goes here
```

11. Now call your penguin's fly() method again and make sure it works as expected!

```js
myPenguin["canFly"] = true;
Now call your penguin's fly() method again and make sure it works as expected!
// your code goes here
```

12. Write a for ... in loop to print each key to the console.

```js
myPenguin.fly();
// I can fly!
Write a for ... in loop to print each key to the console.
// your code goes here
```

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
for(let keys in myPenguin){
console.log(myPenguin[keys]);
}
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!)
// your code goes here
```

## Exercise 2
14. Create an object to hold information on your favorite recipe. It should have properties for title (a string), servings (a number), and ingredients (an array of strings).
On separate lines (one console.log statement for each), log the recipe information so it looks like:
```
Mole
Serves: 2
Ingredients:
cinnamon
cumin
cocoa
```

```js
for(let values in myPenguin){
console.log(myPenguin[values]);
}
Exercise 2
Create an object to hold information on your favorite recipe. It should have properties for title (a string), servings (a number), and ingredients (an array of strings). On separate lines (one console.log statement for each), log the recipe information so it looks like:
Mole
Serves: 2
Ingredients:
cinnamon
cumin
cocoa
// your code goes here
```
var recipe = {
title: "Butter Chiken",
servings:3,
ingredients:["Chicken", "Butter", "Yogurt", "Spice", "Green chillies", "Coriander"]
}
for(let keys in recipe){
console.log(recipe[keys]);
}
Keep track of which books you read and which books you want to read!
Create an array of objects, where each object describes a book and has properties for the title (a string), author (a string), and alreadyRead (a boolean indicating if you read it yet). Iterate through the array of books. For each book, log the book title and book author like so: "The Hobbit by J.R.R. Tolkien". Now use an if/else statement to change the output depending on whether you read it yet or not. If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolkien', and if not, log a string like 'You still need to read "The Lord of the Rings" by J.R.R. Tolkien.'

15. Keep track of which books you read and which books you want to read!

Create an array of objects, where each object describes a book and has properties for the title (a string), author (a string), and alreadyRead (a boolean indicating if you read it yet).
Iterate through the array of books. For each book, log the book title and book author like so: "The Hobbit by J.R.R. Tolkien".
Now use an if/else statement to change the output depending on whether you read it yet or not. If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolkien', and if not, log a string like 'You still need to read "The Lord of the Rings" by J.R.R. Tolkien.'

```js
// your code goes here
```
var arr = [
{
title: "The Hobbit",
author: "J.R.R Tolkein",
alreadyRead: false
},
{
title: "The Lord of the Rings",
author: "J.R.R. Tolkien",
alreadyRead: true
}
]

for(let i = 0 ; i < arr.length ; i++){
console.log(`${arr[i].title} by ${arr[i].author}`);
if(arr[i].alreadyRead == true){
console.log(`You already read ${arr[i].title} by ${arr[i].author}`);
}
else{
console.log(`You still need to read ${arr[i].title} by ${arr[i].author}`);
}

}


// arr.forEach(function (book){
// console.log(`${book.title} is ${book.author}`);
// })
12 changes: 12 additions & 0 deletions 1. objects/2.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
// Using the different way of accessing and assigning a value to the object using `.` or `[]`

// 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";
user["batch"] = 10;

// 5. Using the alert function alert the value of the key added above.
alert(user["batch"]);

// 6. Add a key of `42` to the object with a value of `The answer to the meaning of life 🧸`.
user["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.");
user.city = true;

// 9. Uisng console.log log the value of the key defined above.
console.log(`${user["city"]}`)


// 9. Uisng console.log log the value of the key defined above.

Expand Down
19 changes: 19 additions & 0 deletions 2. array/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,89 @@

```js
// your code goes here
var colors = ["red", "green", "blue", "magenta", "yellow"]
```

2. Access the first color in the array and print it to the console using console.log()

```js
// your code goes here
var firstColor = colors[0];
console.log(firstColor);
```

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 thirdColor = colors[2];
console.log(thirdColor);
```

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.splice(4,1,"yellow")
```

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("green")
```

7.Add another color to the beginning of the list.

```js
// your code goes here
colors.unshift("black");
```

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(colors[6]);
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
colors.forEach(function(color){
console.log(color);
})
```

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
colors.forEach(function(color){
console.log(color.indexOf(color), color);
})
```

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
var lastColor = colors[colors.length + 1]
```
Loading