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
117 changes: 116 additions & 1 deletion 1. objects/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,127 @@

```js
// your code goes here
// Creating an object

var myPenguin = {
"character name": "Captain Cook, Greta",
origin: "Mr. Popper's Penguin",
author: "Richard and Florence Atwater",
}

```

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 ("character 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

var myPenguin = {
"character name": "Captain Cook, Greta",
origin: "Mr. Popper's Penguin",
author: "Richard and Florence Atwater",
}

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() {
alert("Chirp Chirp! Is this what penguins sound like?")
}

myPenguin.chirp()
```

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

```js
// your code goes here
myPenguin.sayHello = function() {
alert("Hello");
}

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.
7. Without modifying any of your previous code, change the penguin's name to "Penguin McPenguinFace" and then call your function sayHello() one more time to make sure it still works.

```js
// your code goes here
myPenguin.name = "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.

```js
// your code goes here
myPenguin.fly = function() {
if (myPenguin.canFly === true) {
alert("I can fly");
} else {
alert("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 = false;
```

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 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
Expand All @@ -84,6 +139,15 @@

```js
// your code goes here
var favoriteRecipes = {
title: "chicken",
serves: 2,
ingredients: ["cinnamon, cumin, code"],
}

console.log(favoriteRecipes.title);
console.log(favoriteRecipes.serves);
console.log(favoriteRecipes.ingredients);
```

15. Keep track of which books you read and which books you want to read!
Expand All @@ -94,4 +158,55 @@

```js
// your code goes here
var allBooks = [];

allBooks.push(book1)
allBooks.push(book2)
allBooks.push(book3)
allBooks.push(book4)
allBooks.push(book5)

var booksToRead = [];

var book1 = {
title: "Ullyses",
author: "James Joyce",
alreadyRead: true,
}
var book2 = {
title: "one good deed",
author: "David Baldacci",
alreadyRead: true,
}

var book3 = {
title: "Under Currents",
author: "Nora Roberts",
alreadyRead: true,
}

var book4 = {
title: "Thin Air",
author: "Lisa Gray",
alreadyRead: false,
}

var book5 = {
title: "The Last Widow",
author: "Karin Slaughter",
alreadyRead: false,
}

var booksAlreadyRead = [];

for (i = 0; i < allBooks.length; i++) {

if (allBooks[i].alreadyRead) {
booksAlreadyRead.push(allBooks[i]);
} else {
booksToRead.push(allBooks[i]);
}
}


```
34 changes: 32 additions & 2 deletions 1. objects/2.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
// Using the different way of accessing and assigning a value to the object using `.` or `[]`
var user = {
name: "John",
age: 40
}

user.age // 40
user.name // John
user["Native Place"] = "Dhramshala";
user["Native Place"] //Dhramshala

// 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";

var myBatch = {
batch: 10
}

// 5. Using the alert function alert the value of the key added above.

alert(myBatch.batch);

// 6. Add a key of `42` to the object with a value of `The answer to the meaning of life 🧸`.

myBatch[42] = "The answer to the meaning of life 🧸"

// 7. Using the function console.log log the value of the key `42`.

console.log([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.");


myBatch.city = "true";


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

console.log(myBatch.city);

// 10. Can you define a key of `let or var` in any object? Reason.
Yes, it can be defined becuase in object there's no restrictions like in the variables.
42 changes: 42 additions & 0 deletions 2. array/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,112 @@

```js
// your code goes here

var colors = ["Purple", "Red", "Pink", "Blue", "Green"]
```

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";

```

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("orange")

```

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

```js
// your code goes here

colors.unshift("dark blue")

```

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("brown")

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 (i = 0; i >= colors.length; i++) {
console.log(colors.length);
}

```

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(i=0; 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

var lastColor=colors[colors.length -1]
console.log(lastColor);

```
Loading