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
81 changes: 67 additions & 14 deletions 1. objects/1.md
Original file line number Diff line number Diff line change
@@ -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<myPenguin.length;i++)
{
console.log(myPenguin[i]);
}
```

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
var i;
for(i in myPenguin) {
console.log(myPenguin[i]);
}
```

## Exercise 2
Expand All @@ -83,7 +104,18 @@
```

```js
// your code goes here
var recipe = {
title: "Mole",
servings: 2,
ingredients: ["cinnamon", "cumin", "cocoa"]
}
console.log(recipe.title);
console.log("serves: " + recipe.servings);
console.log("Ingrdients: ");
for(var i=0;i<recipe.ingredients.length;i++)
{
console.log(recipe.ingredients[i]);
}
```

15. Keep track of which books you read and which books you want to read!
Expand All @@ -93,5 +125,26 @@
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 books = [ {
title: "The Hobbit",
author: "J.R.R. Tolkien",
alreadyRead: false,
}, {
title: "Harry Potter",
author: "J.K. Rowling",
alreadyRead: true,
}, {
title: "Inferno",
author: "Dan Brown",
alreadyRead: true,
} ]

for(var i=0;i<books.length;i++) {
console.log(books[i].title + " by " + books[i].author);
if(books[i].alreadyRead==true) {
console.log("You already read " + books[i].title + " by " + books[i].author);
} else {
console.log("You still need to read the " + books[i].title + " by " + books[i].author);
}
}
```
11 changes: 11 additions & 0 deletions 1. objects/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ var city = prompt("Enter the city name you visited las time.");
// 9. Uisng console.log log the value of the key defined above.

// 10. Can you define a key of `let or var` in any object? Reason.

var user = {
"user name": "Black Panther",
}
console.log(user["user name"]);
user[batch] = 10;
alert(user[batch]);
user[42] = "The answer to the meaning of life 🧸";
console.log(user[42]);
user[city] = true;
console.log(user[city]);
28 changes: 17 additions & 11 deletions 2. array/1.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,77 @@
1. Create an array named colors that contains five different names of colors as strings.

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

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

```js
// your code goes here
console.log(array[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(array[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
array[array.length-1] = "ulraviolet";
```

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 = array[3];
```

6.Add another color to the end of the list.

```js
// your code goes here
array.push("purple");
```

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

```js
// your code goes here
array.unshift("cyan");
```

8. Print the length of the array to the console with console.log()

```js
// your code goes here
console.log(array.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
array.pop();

```

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(var i=0;i<array.length;i++) {
console.log(array[i]);
}
```

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(var i=;i<array.length;i++) {
console.log(i + ", " + array[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 = array[array.length-1];
```
51 changes: 29 additions & 22 deletions 3. copy-vs-reference/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@ var x = 10;
var y = "abc";
var a = x;
var b = y;

//x=10, y="abc", a=10, b="abc"
// Value of x, y, a, b
var x = 10;
var y = "abc";
var a = x;
var b = y;
a = 5;
b = "def";

// x=10, y="abc", a=5, b="def"
// Value of arr & arrCopy
var arr = [1];
var arrCopy = arr;
arr.push(2);

//arr=[1,2], arrCopy=[1]
// Value of obj
var obj = { name: "ryan" };
obj = { surname: "florance" };

// surname: "florance"
// What's the output.
var arr = ["Hi"];
var arr2 = arr;
console.log(arr === arr2);

//true
// What's the output.
var arr1 = ["Hi!"];
var arr2 = ["Hi!"];
console.log(arr1 === arr2);
console.log(arr == arr2);

//false
//false
//What's the output
console.log([10] === [10]);

// false
// What's the output?

function personDetails(person) {
Expand All @@ -52,29 +53,33 @@ var personObj1 = {
age: 30
};
var personObj2 = personDetails(personObj1);
console.log(personObj1); // -> ?
console.log(personObj2); // -> ?
console.log(personObj1); // -> name:Alex Age: 25
console.log(personObj2); // -> ?name: John Age: 30

// Guess the output
var oldArray = [];
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);

//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 = [];
Expand All @@ -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 = {
Expand All @@ -95,6 +101,7 @@ var person = {
};

var clone = {};
clone = {...person};

// Output of the following
let brothers = ["John", "Bran", "Robb"];
Expand All @@ -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
Loading