Skip to content

Commit b1f9155

Browse files
committed
Added and Modified files
1 parent eed2f20 commit b1f9155

File tree

3 files changed

+254
-1
lines changed

3 files changed

+254
-1
lines changed

Arrays/Array.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// There are 2 ways to declare Array
2+
3+
let arr1 = new Array();
4+
let arr2 = [];
5+
6+
// Array can store elements of any data type
7+
8+
arr1 = [2, 3, 5, 3, 1, 8];
9+
arr2 = [
10+
{
11+
'a': 1,
12+
'b': 2,
13+
'c': 3,
14+
},
15+
true,
16+
'John',
17+
800000
18+
]
19+
20+
console.log(arr1);
21+
console.log(arr2);
22+
23+
/* Array methods */
24+
25+
// push()
26+
arr1.push('Voila');
27+
console.log(`arr1.push('Voila') = ${arr1}`);
28+
29+
// pop()
30+
arr1.pop()
31+
console.log(`arr1.pop() = ${arr1}`);
32+
33+
// shift() - Removes item from beginning
34+
arr1.shift();
35+
console.log(`arr1.shift() = ${arr1}`);
36+
37+
// unshift() - Add item from the beginning
38+
arr1.unshift('Start');
39+
console.log(`arr1.unshift('Start') = ${arr1}`);
40+
41+
42+
// indexOf()
43+
console.log(`arr1.indexOf(3) = ${arr1.indexOf(3)}`);
44+
45+
// splice() - Array.splice( index, remove_count, item_list )
46+
// In below example, element at index 3 will be deleted 1 time and
47+
// replaced with 'Three' and true
48+
arr1.splice(3, 1, 'Three', true);
49+
console.log(`arr1.splice(3, 1, 'Three', true) = ${arr1}`);
50+
51+
// splice() can also be used to delete operation
52+
arr1.splice(4, 2);
53+
console.log(`arr1.splice(4, 2) = ${arr1}`);
54+
55+
// slice() is used to create shallow copy of array
56+
arr3 = arr1.slice();
57+
console.log(`arr1.slice() = ${arr3}`);
58+
59+
60+
// Destructing Array
61+
62+
const func1 = function(){
63+
return [1,2,3,4];
64+
}
65+
66+
// Case 1 - Assigning each element of array in a variable
67+
let [x, y, z, a] = func1();
68+
console.log(x,y,z,a);
69+
70+
// Case 2 - Value of unassigned element of array will be discarded
71+
let [i, j, k] = func1();
72+
console.log(i,j,k);
73+
74+
// Case 3 - Assigning default values
75+
let [b = 34, c = 56] = [];
76+
console.log(b,c);
77+
78+
[b = 34, c = 56] = [87];
79+
console.log(b,c);
80+
81+
// Case 4 - Ignoring some values
82+
[a, , ,c] = [23,45,56,34,54]
83+
console.log(a,c);
84+
85+
// Case 5 - Rest Parameter with destructing
86+
let planets = ['M', 'V', 'E', 'MA', 'J', 'S', 'N', 'P'];
87+
88+
[a, ,b,c, ...others] = planets;
89+
console.log(a,b,c);
90+
console.log(others);

Arrays/index.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<script src="Array.js"></script>
8+
<link rel="shortcut icon" href="#" />
9+
<style>
10+
@import url('https://fonts.googleapis.com/css2?family=Potta+One&display=swap');
11+
body{
12+
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
13+
font-family: 'Potta One', cursive;
14+
}
15+
div{
16+
margin-top: 20%;
17+
}
18+
p{
19+
text-align:center;
20+
font-size:3rem;
21+
color: white;
22+
}
23+
</style>
24+
<title>Learning</title>
25+
</head>
26+
<body>
27+
<div><p>Array</p></div>
28+
</body>
29+
</html>

Basics/JS_Basics.js

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,138 @@ func2();
141141
const func3 = (a = 'Paul', b = 'John') => {
142142
console.log(`${a} & ${b} = The Beatles`);
143143
};
144-
func3();
144+
func3();
145+
146+
// Object Continues..
147+
// There are 2 ways to define objects
148+
// Constructor Syntax
149+
let obj2 = new Object();
150+
151+
// Literal Syntax
152+
let obj3 = {};
153+
154+
// Object Literal and Properties
155+
obj2 = {
156+
name: 'Fred',
157+
age: 54,
158+
job: 'Artist',
159+
married: true,
160+
}
161+
162+
// Accessing Object Properties
163+
// Dot Notation
164+
console.log(`obj2.name = ${obj2.name}`);
165+
console.log(`obj2.job = ${obj2.job}`);
166+
167+
// Square Brackets
168+
console.log(`obj2['name'] = ${obj2['name']}`);
169+
console.log(`obj2['job'] = ${obj2['job']}`);
170+
171+
// Adding a property in object
172+
obj2.canDrive = true
173+
console.log(obj2);
174+
175+
// Notice when we initialized obj2, we didn't quote string keys like we used to do in
176+
// Python dict. However, when you are passing multi word key, then in that case
177+
// you need to use quotes. For example,
178+
obj2["pin code"] = 205001;
179+
console.log(obj2);
180+
// In above case, you can't use dot notation, either for declaring property or accessing it
181+
// For eg. - obj2.'pin code' = 205001 will throw error
182+
183+
184+
// Computed Properties
185+
// let fruit = prompt('Which fruit you want to buy?', null);
186+
let fruit = 'apple'
187+
let obj4 = {
188+
[fruit]:5,
189+
};
190+
console.log(obj4);
191+
192+
// 'in' operator. Syntax - <'key' in object>
193+
console.log(`'age' in obj2 = ${'age' in obj2}`);
194+
console.log(`'address' in obj2 = ${'address' in obj2}`);
195+
196+
// for..in loop
197+
for (let key in obj2){
198+
console.log(key);
199+
}
200+
201+
// this keyword - It has different values depending on where it is used:
202+
/*
203+
- In a method, 'this' refers to the owner object
204+
- Alone, 'this' refers to the global object
205+
- In a function, 'this' refers to the global object
206+
- In a function, in strict mode, 'this' is undefined
207+
- In an event, 'this' refers to the element that received the event
208+
- Methods like call() and apply() can refer 'this' to any object
209+
*/
210+
211+
// Loop in JS
212+
// for loop
213+
214+
for(let i = 0; i < 3; i++){
215+
console.log(i);
216+
}
217+
218+
// break with label
219+
220+
loop1:
221+
for(let i=0;i<5;i++){
222+
loop2:
223+
for(let j=0; j<i;j++){
224+
if (i === 4){
225+
break loop1;
226+
}
227+
console.log(j);
228+
}
229+
}
230+
231+
// In similar manner, label can be used with continue
232+
233+
// while loop
234+
let temp5 = 121;
235+
let temp7, temp6;
236+
temp6, temp7 = 0;
237+
while (temp5 != 0){
238+
temp6 = temp5 % 10;
239+
temp5 = parseInt(temp5/10);
240+
temp7 = (temp7 * 10) + temp6;
241+
}
242+
console.log(temp7);
243+
244+
//do while loop
245+
let temp8 = 1;
246+
do {
247+
console.log(temp8);
248+
temp8++;
249+
}while(temp8 < 5);
250+
251+
// arguments in JS
252+
// Arrow Function doesn't have arguments
253+
const func4 = function(p1, p2){
254+
console.log(`para1 = ${p1}, para2 = ${p2}, args = ${arguments}`);
255+
console.log(typeof(arguments));
256+
};
257+
258+
func4(1,2,3,4);
259+
260+
// restArgs
261+
262+
const func5 = function(para1, para2, ...restArgs){
263+
console.log(`para1 = ${para1}, para2 = ${para2}, rest = ${restArgs}`);
264+
console.log(`typeof(restArgs) = ${typeof(restArgs)}`);
265+
};
266+
267+
func5(32,45,12,99,99,99,99);
268+
269+
/*
270+
1 - Rest Parameters is a real array and methods like forEach and sort can be applied.
271+
Even though the arguments object has the length method, it is not a real array and using
272+
array methods like sort would only bring us misery and sorrow.
273+
274+
2 -Rest Parameters contain only the arguments that have no corresponding parameter
275+
while arguments object contains all the arguments passed to the function.
276+
*/
277+
278+

0 commit comments

Comments
 (0)