Skip to content

Commit 2e9ffaf

Browse files
array slice method.
1 parent b09f87c commit 2e9ffaf

File tree

4 files changed

+156
-11
lines changed

4 files changed

+156
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
<style>
8+
.newDiv {
9+
width: 100%;
10+
height: 300px;
11+
background-color: aqua;
12+
}
13+
.borderRed {
14+
border: 3px solid red;
15+
}
16+
</style>
17+
</head>
18+
<body>
19+
<h1>Dynamic Element</h1>
20+
<div id="root"></div>
21+
<script>
22+
const rootEl = document.getElementById("root");
23+
const newDiv = document.createElement("div");
24+
newDiv.classList.add("newDiv");
25+
newDiv.classList.add("borderRed");
26+
27+
const h1El = document.createElement("h1");
28+
h1El.innerHTML = "Hello";
29+
newDiv.appendChild(h1El);
30+
31+
rootEl.appendChild(newDiv);
32+
</script>
33+
</body>
34+
</html>

03 - Arrary/Methods/18_slice.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Array.prototype.slice()
2+
// The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
3+
4+
// Try it
5+
const animals = ["ant", "bison", "camel", "duck", "elephant"];
6+
7+
console.log(animals.slice(2));
8+
// Expected output: Array ["camel", "duck", "elephant"]
9+
10+
console.log(animals.slice(2, 4));
11+
// Expected output: Array ["camel", "duck"]
12+
13+
console.log(animals.slice(1, 5));
14+
// Expected output: Array ["bison", "camel", "duck", "elephant"]
15+
16+
console.log(animals.slice(-2));
17+
// Expected output: Array ["duck", "elephant"]
18+
19+
console.log(animals.slice(2, -1));
20+
// Expected output: Array ["camel", "duck"]
21+
22+
console.log(animals.slice());
23+
// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]

04 - Object/index.js

+52-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
// const person = { lastName: "Doe" };
2-
// console.log(person);
3-
// person.firstName = "John";
4-
// console.log(person);
5-
// person.age = 29;
6-
// console.log(person);
7-
// console.log(person.firstName);
8-
91
// Nested Object
102
const person = {
113
firstName: "Pradip",
@@ -33,6 +25,55 @@ const fullName = {
3325
fullName: "Pradip Chaudhary",
3426
},
3527
};
36-
console.log(fullName);
37-
console.log(Object.keys(fullName));
38-
console.log(Object.values(fullName));
28+
// console.log(fullName);
29+
30+
const nameKey = "name";
31+
const agekey = "age";
32+
33+
const nameValue = "Pradip Chaudhary";
34+
const ageValue = 29;
35+
36+
const personOne = {
37+
nameKey: nameValue,
38+
agekey: ageValue,
39+
"full name": "This is the full name ",
40+
};
41+
// console.log(personOne);
42+
// personOne.name = "Younz";
43+
// console.log(personOne);
44+
// console.log(Object.keys(personOne));
45+
46+
// console.log(personOne["age"]);
47+
// console.log(personOne["full name"]);
48+
49+
// Constructor Notation: (new is the constructor function)
50+
const personTwo = new Object();
51+
personTwo.age = 20;
52+
// add values by the brackets notation
53+
personTwo["full name"] = "John Doe";
54+
// console.log(personTwo);
55+
56+
// Call the object
57+
58+
// console.log(Object.keys(person));
59+
// console.log(Object.values(person));
60+
// console.log(typeof Object.entries(person));
61+
62+
// null and undefined objects return blank objects
63+
// const obj1 = new Object(null);
64+
// console.log(obj1);
65+
// const obj2 = new Object(undefined);
66+
// console.log(obj2);
67+
68+
const obj = {
69+
foo: 5,
70+
// You should not define such a method on your own object,
71+
// but you may not be able to prevent it from happening if
72+
// you are receiving the object from external input
73+
propertyIsEnumerable() {
74+
return false;
75+
},
76+
};
77+
78+
console.log(obj.propertyIsEnumerable("hello")); // false; unexpected result
79+
Object.prototype.propertyIsEnumerable.call(obj, "foo"); // true; expected result

Call Apply and Bind/index.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Understanding Call, Bind and Apply Methods in JavaScript
2+
3+
// Simple JavaScript Object whith speak methods
4+
5+
const cat = {
6+
name: "Oreo",
7+
noise: "Meow",
8+
speak: function () {
9+
return `${this.name} says ${this.noise}`;
10+
},
11+
};
12+
13+
const dog = {
14+
name: "kale",
15+
noise: "woof",
16+
speak: function () {
17+
return `${this.name} says ${this.noise}`;
18+
},
19+
};
20+
21+
// console.log(cat.speak());
22+
// console.log(dog.speak());
23+
24+
const hands = {
25+
name: "kale",
26+
noise: "Ha Ha Ha",
27+
};
28+
29+
const cow = {
30+
name: "rami",
31+
noise: "Mai Mai Mai",
32+
};
33+
34+
function speak() {
35+
return `${this.name} says ${this.noise}`;
36+
}
37+
38+
// Call the same function on the above two objects
39+
console.log(speak.call(hands));
40+
console.log(speak.call(cow));
41+
42+
// object methods with two arguments
43+
function walk(adj, place) {
44+
return `${this.name} like to ${adj} walk to the ${place}`;
45+
}
46+
47+
console.log(walk.call(cow, "crzy", "gaighat"));

0 commit comments

Comments
 (0)