Skip to content

Commit 48e42d0

Browse files
[Addition]: added several array methods
1 parent 2fcbfed commit 48e42d0

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

array-methods/main.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ console.log(cars)//['BWM','Mercedes-Benz','Toyota-Vitz']
1717

1818
//3. shift() -Removes the first element from an array.
1919

20-
let fruits = ["apple", "banana", "orange"];
21-
let first = fruits.shift();
20+
let fruits1 = ["apple", "banana", "orange"];
21+
let first = fruits1.shift();
2222

2323
console.log(first); // "apple"
24-
console.log(fruits); // ["banana", "orange"]
24+
console.log(fruits1); // ["banana", "orange"]
2525

2626
//4. unshift() -Adds one or more elements at the beginnig of an array.
2727
// -Returns the new length of an array.
@@ -32,8 +32,8 @@ console.log(alphabets)//[ 'Z', 'Y', 'A', 'B', 'C', 'D' ]
3232

3333
//5. map()-Creates a new array by applying a function to each element.
3434

35-
let nums = [1, 2, 3, 4, 5]
36-
let productOfNums = nums.map(elm => elm * 2)//multiply each element in an array by 2
35+
let nums1 = [1, 2, 3, 4, 5]
36+
let productOfNums = nums1.map(elm => elm * 2)//multiply each element in an array by 2
3737
console.log(productOfNums)//[2,4,6,8,10]
3838

3939
//6. filter()-Creates a new array with elements that passes a particular test or passes a condition
@@ -81,9 +81,9 @@ console.log(words.join("-")); // "Hello-World"
8181

8282
//12.splice()-Adds/removes items in the origianal array
8383
//format - array.splice(start,deletecount,newItems...)
84-
let fruits = ["apple", "banana", "cherry"];
85-
fruits.splice(1, 1, "mango");
86-
console.log(fruits); // ["apple", "mango", "cherry"]
84+
let fruits2 = ["apple", "banana", "cherry"];
85+
fruits2.splice(1, 1, "mango");
86+
console.log(fruits2); // ["apple", "mango", "cherry"]
8787

8888
//13.slice()-Returns a portion of the array ,does not change the original
8989
let arr = [1, 2, 3, 4, 5];
@@ -102,19 +102,30 @@ console.log(nested.flat()); // [1, 2, [3, 4]]
102102
console.log(nested.flat(2)); // [1, 2, 3, 4]
103103

104104
//16.some()-Returns true if at least one element passes a condition.
105-
let nums = [1, 3, 5, 8];
106-
console.log(nums.some(n => n % 2 === 0)); // true (because 8 is even)
105+
let nums2 = [1, 3, 5, 8];
106+
console.log(nums2.some(n => n % 2 === 0)); // true (because 8 is even)
107107

108108
//17.every()Returns true if all element passes a condition
109-
let nums2 = [2, 4, 6];
110-
console.log(nums2.every(n => n % 2 === 0)); // true
109+
let nums3 = [2, 4, 6];
110+
console.log(nums3.every(n => n % 2 === 0)); // true
111111

112112
//18.sort()-Sorts elements as strings by default.
113113
// -To sort numbers correctly, pass a compare function.
114114
let letters = ["c", "a", "b"];
115115
console.log(letters.sort()); // ["a", "b", "c"]
116116

117-
let nums3 = [40, 5, 100, 25];
118-
console.log(nums3.sort()); // [100, 25, 40, 5] (wrong for numbers)
119-
console.log(nums3.sort((a, b) => a - b)); // [5, 25, 40, 100] (correct)
117+
let nums4 = [40, 5, 100, 25];
118+
console.log(nums4.sort()); // [100, 25, 40, 5] (wrong for numbers)
119+
console.log(nums4.sort((a, b) => a - b)); // [5, 25, 40, 100] (correct)
120120

121+
//19.reverse()-Reverses the order of elements in an array.
122+
let nums5 = [9,6];
123+
console.log(nums5.reverse()); // [6, 9]
124+
125+
//20.fill()-Fills all the elements in an array with a static value.
126+
let nums6 = [];
127+
console.log(nums6.fill(0, 0, 5)); // [0, 0, 0, 0, 0]
128+
129+
//21.includes()-Checks if an array contains a specific element.
130+
let nums7 = [1, 2, 3, 4, 5];
131+
console.log(nums7.includes(3)); // true

date-methods/main.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ console.log(date.getDate())//22
1818
console.log(date.toISOString())//2025-08-22T12:07:25.921Z
1919

2020
//5.getTime()-Returns a number of milliseconds since January 1,1970
21-
console.log(date.getTime())//1755864578215
21+
console.log(date.getTime())//1755864578215
22+
23+
//6.getDay()-Returns the day of the week(0-6) for a specific date
24+
console.log(date.getDay()) //3
25+
26+
//7.setFullYear(year)-Sets the year of a date object
27+
console.log(date.setFullYear(20230))
28+
29+
//we have setMonth(), setDate(), setHours(), setMinutes(), setSeconds(), setMilliseconds() to be used as above

0 commit comments

Comments
 (0)