Skip to content

Commit eca99ec

Browse files
committed
leetcode solutions added
1 parent cc04b24 commit eca99ec

15 files changed

+347
-99
lines changed

A/array-methods/array_methods.md

+108-67
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
# [Array Methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
22

3-
### map
4-
Map takes an array and performes certain operations on it and then returns a new array.
3+
4+
### copyWithin
5+
copy elements within the array
6+
```
7+
copyWithin(target, start)
8+
copyWithin(target, start, end)
9+
copy to targetIndex from startIndex to endIndex
10+
```
11+
12+
### every
13+
Checks if every element folows the given function and returns 1 boolean value
14+
```
15+
```
16+
17+
### forEach
18+
forEach runs a function on each value of the array and returns nothing
19+
```
20+
arr.forEach(item => console.log(item));
21+
```
22+
523

624
### filter
725
Filter returnes some part of the array based on certain condition
826

9-
### reduce
10-
reduce will work on the entire array and return the resultant
27+
### fill
28+
fill value in array from startIndex to End index
1129
```
12-
Array.reduce(callback, initialValue)
30+
fill(value)
31+
fill(value, start)
32+
fill(value, start, end)
1333
14-
function callback(previousValue , currentValue , currentIndex, arr){
15-
.
16-
.
17-
.
18-
return currentValue;
19-
}
2034
```
21-
### reduceRight
22-
reduceRight works from right to left
2335

2436
### Find
2537
Find will iterate through all items of array and return the first item that matches the condition given inside callback function.
@@ -40,6 +52,13 @@ arr.findIndex(callback(value, index, obj));
4052
returns first matching index or -1
4153
```
4254

55+
56+
### flat
57+
returns the flat version of the multi level array.
58+
```
59+
```
60+
61+
4362
### lastIndexOf
4463
returns the last occurance of the given item
4564

@@ -52,6 +71,28 @@ length will return the length of the given array.
5271
```
5372
```
5473

74+
75+
### map
76+
Map takes an array and performes certain operations on it and then returns a new array.
77+
78+
79+
### reduce
80+
reduce will work on the entire array and return the resultant
81+
```
82+
Array.reduce(callback, initialValue)
83+
84+
function callback(previousValue , currentValue , currentIndex, arr){
85+
.
86+
.
87+
.
88+
return currentValue;
89+
}
90+
```
91+
### reduceRight
92+
reduceRight works from right to left
93+
94+
95+
5596
### isArray()
5697
```
5798
Array.isArray(arr);
@@ -60,12 +101,7 @@ Array.isArray(arr);
60101
### reverse
61102
reverse method will return the array with reversed index on th esame array.
62103

63-
### sort
64-
sort will sort the array lexically and return the updated array.
65-
- sort method can take another param that is a comparator
66-
```
67-
Array.sort(comparator);
68-
```
104+
69105

70106

71107
### join
@@ -81,22 +117,12 @@ Note: takes no parameter
81117
82118
```
83119

84-
### flat
85-
returns the flat version of the multi level array.
86-
```
87-
```
88-
89120
### includes
90121
includes takes a key and returnd TRUE or FALSE based upon presence of that key in array.
91122
also takes starting index to search from
92123
```
93124
arr.includes(searchElement, fromIndex): boolean
94125
95-
```
96-
### forEach
97-
forEach runs a function on each value of the array and returns nothing
98-
```
99-
arr.forEach(item => console.log(item));
100126
```
101127

102128
### shift
@@ -141,40 +167,14 @@ returns iterator which contains array in which key value is present
141167
```
142168
```
143169

144-
### splice
145-
used to add/ remove few elements from the array.
146-
**start** : An integer from were the pprocess neeeds to be started
147-
**deleteCount** : An integer indicating the number of elements in the array to remove from start.
148-
```
149-
splice(start)
150-
splice(start, deleteCount)
151-
splice(start, deleteCount, item1, item2, /* …, */ itemN)
152-
```
153-
154-
155-
### slice
156-
returns subpart of array
157-
- Start to End (end not included)
158-
- Negative index counts back from the end of the array
159-
```
160-
slice()
161-
slice(start)
162-
slice(start, end)
163-
```
164-
---
165-
## *Adverbs*
166170

167171

168172
### of
169173
creates a new array with whatever comma seperated values are provided.
170174
```
171175
let arr = Array.of("2", 5);
172176
```
173-
---
174-
### every
175-
Checks if every element folows the given function and returns 1 boolean value
176-
```
177-
```
177+
178178
### some
179179
Checks if EVEN ONE element folows the given function and returns 1 boolean value
180180
```
@@ -194,7 +194,6 @@ Array.from(arrayLike, mapFn, thisArg)
194194
returns item at that particular index
195195
```
196196
```
197-
---
198197

199198
### flatMap
200199
It is identical to a map() followed by a flat() of depth **1**(arr.map(...args).flat())
@@ -204,20 +203,62 @@ flatMap(callbackFn, thisArg)
204203
205204
```
206205

207-
### fill
208-
fill value in array from startIndex to End index
206+
207+
208+
### splice
209+
- same array ko update krdega.
210+
used to add / remove / replace few elements from the array.
211+
**It modifies the original array**
212+
*returnType* : **returns an array containing the removed elements (if any)**
213+
214+
**start** : An integer from were the process neeeds to be started
215+
**deleteCount** : An integer indicating the number of elements in the array to remove from start.
216+
**items** : items to be inserted
209217
```
210-
fill(value)
211-
fill(value, start)
212-
fill(value, start, end)
218+
splice(start) // Removes everything from index start onwards
219+
splice(start, deleteCount)
220+
splice(start, deleteCount, item1, item2, /* …, */ itemN)
221+
```
222+
1. orignal array ko modify karega
223+
2. return kata hua ya jo remove kr reh ho vo part karega.
224+
3. sirf start doge to startIndex se array end tak sab gayab kr dega.
225+
4. jo items add hoti hai vo startIndex se hi add hona chalu hoti hai.
226+
5. Remove all elements of array
227+
```
228+
arr.splice(0, arr.length);
229+
```
230+
213231

232+
### slice
233+
234+
.slice() method is used to extract a portion of an array (or string) without modifying the original array
214235
```
215-
### copyWithin
216-
copy elements within the array
236+
slice() // No Slicing, just creates a copy of Array.
237+
slice(start) // default value of end is last index
238+
slice(start, end) // start to end-1 index
239+
217240
```
218-
copyWithin(target, start)
219-
copyWithin(target, start, end)
220-
copy to targetIndex from startIndex to endIndex
241+
1. Array kaat ke ney array deta hai.
242+
2. Existing Array ko kuch nahi hoga.
243+
3. returns subpart of array
244+
- Start to End (*end not included*)
245+
4. Negative index counts back from the end of the array
246+
- **-1 refers to the last element**
247+
```
248+
slice (-1)
249+
// -1 is startIndex
250+
// endIndex not given so last index is equal to end Index by default and is baar end index ki value bhi include hogi.
251+
```
252+
5. Remember important thing agar end index doge to end index pr jo value hai vo include nahi hogi, aur agar end index nahi doge to startIndex se last index tak ka array return milega.
253+
6. if start === end then returns empty array
254+
7. OR in simple words if endIndex <= startIndex then return empty array.
255+
256+
257+
### sort
258+
sort will sort the array lexically and return the updated array.
259+
- sort method can take another param that is a comparator
260+
```
261+
Array.sort(comparator);
221262
```
222263

223264

A/array-methods/slice.js

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
11
const arr = ["ant", "bison", "camel", "duck", "elephant"];
22

3+
4+
35
// slice(start, end)
4-
let one = arr.slice();
6+
let ans = arr.slice(0, 2);
7+
console.log("Sliced Array", ans);
8+
console.log("Original Array", arr);
9+
console.log("Original Array remains the same");
10+
11+
// slice()
12+
let ans2 = arr.slice();
13+
console.log(ans2);
14+
15+
// slice(start)
16+
let ans3 = arr.slice(1);
17+
console.log(ans3);
18+
19+
// negative index
20+
let ans4 = arr.slice(-1);
21+
console.log(ans4);
22+
23+
// negative start index
24+
let ans5 = arr.slice(-3);
25+
let ans6 = arr.slice(-3, 4);
26+
27+
console.log(ans5);
28+
console.log(ans6);
29+
30+
// negative end index
31+
let ans7 = arr.slice(3, -2);
32+
33+
console.log(ans7);
34+
35+
// start is equal to end
36+
let ans8 = arr.slice(3, 3);
37+
38+
console.log(ans8);
39+
40+
41+
//Both Start and End as Negative Values
542

6-
console.log(one);
43+
let ans9 = arr.slice(-2, -4);
44+
let ans10 = arr.slice(-4, -2);
745

46+
console.log("ans9", ans9); // "camel", "duck"
47+
console.log("ans10", ans10); // "bison", "camel"

A/array-methods/splice.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22
splice(start, deleteCount);
33
splice(start, deleteCount, item1, item2, itemN) */
44

5-
const months = ["Jan", "March", "April", "June", "July"];
5+
const months = ["Jan", "bef", "March", "April", "May", "June", "July"];
66

7-
months.splice(3 , 2 , "change", "another");
8-
console.log(months);
7+
//! Removing elements
8+
9+
//? splice for only removing
10+
// months.splice(4, 1);
11+
12+
//! Adding elements
13+
14+
//? Remove + Add splice(start, deleteCount, item1, item2, itemN)
15+
// let ans = months.splice(3 , 2 , "change", "another");
16+
// console.log(ans);
917

18+
//! Replacing Elements
19+
20+
months.splice(1, 1, "Feb");
21+
22+
console.log(months);
1023

11-
months.splice(4, 1, 'May');
12-
// Replaces 1 element at index 4
24+
//! using negative index
1325

14-
// console.log(months);
26+
//! removing all elements
27+
// arr.splice(0, arr.length);

C/classes/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Classes
2+
3+
4+
private variables in class ( using #)
5+
```
6+
class ClassName{
7+
#varName;
8+
9+
constructor(){
10+
this.#varName = value;
11+
}
12+
13+
}
14+
```

O/object-methods/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ object.hasOwnProperty()
1717

1818

1919

20-
"propertyName" in Object // checks if property is present in object
20+
"propertyName" in Object // checks if property is present in object
21+
22+
## delete a key
23+
24+
```
25+
delete obhect.key;
26+
```

O/oops/readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
A constructor function in JavaScript is a special type of function used to create and initialize objects.
44

5-
// ES 6 introduced concept of classes which is same as the constructor functions , just a syntactical sugar
5+
// ES 6 introduced concept of classes which is same as the constructor functions , just a syntactical sugar
6+
7+

machine-coding/index.html

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@
1717
</style>
1818
</head>
1919
<body>
20-
<script src="./solutions/one.js"></script>
21-
22-
<div class="list" >
23-
<input type="checkbox" name="parent" value="value" id="" />
24-
<input type="checkbox" name="parent" value="value" id="" />
25-
<input class="child" type="checkbox" name="parent" value="value" id="" />
26-
<input type="checkbox" name="parent" value="value" id="" />
27-
<input type="checkbox" name="parent" value="value" id="" />
28-
</div>
20+
21+
<script src="./lru-cache.js"></script>
2922
</body>
3023
</html>

0 commit comments

Comments
 (0)