Skip to content

Commit dcb837d

Browse files
author
iambrijeshtoo
committed
refactor: array
1 parent 6769508 commit dcb837d

32 files changed

+160
-98
lines changed

09_math/numbers_and_math.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Numbers
2+
3+
const number = 3.14619;
4+
console.log(number.toString());
5+
console.log(number.toFixed(2));
6+
console.log(number.toPrecision(4));
7+
8+
const loanAmount = 100000;
9+
console.log(loanAmount.toLocaleString("en-IN"));
10+
11+
// Math
12+
13+
// What
14+
// - Math is object in JavaScript.
15+
// - It has built in method which helps for math related operations.
16+
17+
console.log(Math);
18+
console.log(Math.abs(-4));
19+
console.log(Math.round(3.14));
20+
console.log(Math.ceil(3.14));
21+
console.log(Math.floor(3.14));
22+
console.log(Math.min(1, 2, 3));
23+
console.log(Math.max(5, 6, 7));
24+
25+
console.log(Math.random());
26+
console.log(Math.random() * 10 + 1);
27+
28+
const minimumNumber = 10;
29+
const maximumNumber = 20;
30+
31+
console.log(
32+
Math.floor(Math.random() * (maximumNumber - minimumNumber + 1)) +
33+
minimumNumber
34+
);

10_date/date.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Date
2+
3+
let myDate = new Date();
4+
console.log(myDate); // 2024-05-24T07:07:32.522Z
5+
console.log(myDate.toString()); // Fri May 24 2024 12:37:32 GMT+0530 (India Standard Time)
6+
console.log(myDate.toISOString()); // 2024-05-24T10:06:10.171Z
7+
console.log(myDate.toJSON()); // 2024-05-24T10:06:10.171Z
8+
9+
// Note
10+
// - In JavaScript month start from 0 (Jan) to 11 (Dec).
11+
// let myCustomDate = new Date(2024, 0, 24);
12+
// let myCustomDate = new Date(2024, 0, 24, 6, 30);
13+
// let myCustomDate = new Date("2024-05-24"); // YYYY-MM-DD (When you passing date as a string formate month will be written in normal way.)
14+
let myCustomDate = new Date("05-24-2024"); // MM-DD-YYYY (When you passing date as a string formate month will be written in normal way.)
15+
console.log(myCustomDate.toLocaleString());
16+
17+
let myTimeStamp = Date.now();
18+
console.log(myTimeStamp);
19+
console.log(myCustomDate.getTime()); // It will return the milliseconds value
20+
console.log(Math.floor(Date.now() / 1000));
21+
22+
let myMonth = new Date();
23+
console.log(myMonth);
24+
console.log(myMonth.getMonth() + 1);
25+
console.log(myMonth.getDay());
26+
27+
console.log(
28+
myMonth.toLocaleString("default", {
29+
weekday: "long",
30+
dateStyle: "full",
31+
})
32+
);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Array
22

3-
// Q. What is array?
3+
// What
44
// - It is the collection of elements (values).
55

66
// NOTE
77
// - It the common practice to create array with `const`.
8-
// - Array can store different type of value in it.
8+
// - Array can store different type of value.
99

1010
// Syntax
1111
// const arrayName = [element1, element2, element3];
1212

1313
// Default way creating array.
14-
const arr1 = [1, 2, 3];
14+
const myArray1 = [1, 2, 3];
1515

1616
// Create array from the object.
17-
const arr2 = new Array(1, 2, 3);
18-
console.log(arr2);
17+
const myArray2 = new Array(1, 2, 3);
18+
console.log(myArray2);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Array Are Object
22

3-
// Q. What is array are object?
4-
// - If you use the `typeof` operator to check the datatype of array. You will see that it will show the object type. But in JavaScript normally it known as array. Because of array is object type you can have objects in an Array. You can have functions in an Array. You can have arrays in an Array.
3+
// What
4+
// - If you use the `typeof` operator to check the datatype of array. You will see that it will show the object type. But in JavaScript normally it known as array. Because of array is object-type you can have objects in an Array. You can have functions in an Array. You can have arrays in an Array.
55

6-
const arr = [1, 2, 3];
7-
console.log(typeof arr);
6+
const myArray = [1, 2, 3];
7+
console.log(typeof myArray);
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Array Operation
22

3-
const arr = [1, 2, 3];
3+
const myArray = [1, 2, 3];
44

55
// Accessing element
6-
console.log(arr[0]);
6+
console.log(myArray[0]);
77

88
// Adding element
9-
arr[3] = 4;
9+
myArray[3] = 4;
1010

1111
// Changing/ Updating element
12-
arr[0] = -1;
13-
12+
myArray[0] = -1;

Array/04_array_destructuring.js renamed to 11_array/4_array_destructuring.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
// Array Destructure
22

3-
//? Q. What is array destructuring?
3+
// What
44
// - Array destructuring is a feature in JavaScript that allows you to extract values from an array and assign them to individual variables.
55

6-
const array = [1, 2, 3, 4, 5];
6+
const myArray = [1, 2, 3, 4, 5];
77

8-
//* Dry syntax
9-
let variable1 = array[0];
10-
let variable2 = array[1];
11-
let variable3 = array[2];
8+
// Dry syntax
9+
let variable1 = myArray[0];
10+
let variable2 = myArray[1];
11+
let variable3 = myArray[2];
1212
console.log(variable1);
1313
console.log(variable2);
1414
console.log(variable3);
1515

16-
//* Better syntax
17-
let [variable4, variable5, variable6] = array;
16+
// Better syntax
17+
let [variable4, variable5, variable6] = myArray;
1818
console.log(variable4);
1919
console.log(variable5);
2020
console.log(variable6);
2121

22-
//* Store element in single variable and creating new array using remaining elements.
23-
let [variable7, ...newArray] = array;
22+
// Store element in single variable and creating new array using remaining elements.
23+
let [variable7, ...newArray] = myArray;
2424
console.log(variable7);
2525
console.log(newArray);
2626

27-
//* Skipping single element when storing array elements inside variables.
28-
let [variable8, , variable9] = array;
27+
// Skipping single element when storing array elements inside variables.
28+
let [variable8, , variable9] = myArray;
2929
console.log(variable8);
3030
console.log(variable9);
3131

32-
//* Swapping values using array destructuring
32+
// Swapping values using array destructuring
3333
let variable10 = "value1";
3434
let variable11 = "value2";
3535
[variable11, variable10] = [variable10, variable11];

11_array/5_array_iterate.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Array Iterate
2+
3+
const myArray = [1, 2, 3, 4, 5];
4+
5+
// For loop
6+
for (let i = 0; i < myArray.length; i++) {
7+
const ele = myArray[i];
8+
console.log(ele);
9+
}
10+
11+
// While loop
12+
let i = 0;
13+
while (i < myArray.length) {
14+
const ele = myArray[i];
15+
console.log(ele);
16+
i++;
17+
}
18+
19+
// For of loop (Recommended)
20+
for (const ele of myArray) {
21+
console.log(ele);
22+
}
23+
24+
// For in loop (Not Recommended)
25+
for (const i in myArray) {
26+
const ele = myArray[i];
27+
console.log(ele);
28+
}

11_array/ArraysMethod/1_push.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// push() Method
2+
3+
// What
4+
// - It add new element at the end of array.
5+
6+
const myArray = [1, 2, 3, 4, 5];
7+
myArray.push(6);
8+
console.log(myArray);

11_array/ArraysMethod/2_pop.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// pop() Method
2+
3+
// What
4+
// - It remove the last element in array.
5+
6+
const myArray = [1, 2, 3, 4, 5];
7+
myArray.pop();
8+
console.log(myArray);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Shift Method
22

3-
// Q. What is shift method?
3+
// What
44
// - The shift() method removes the first element from an array and returns that element. It also shifts all remaining elements one position towards the front of the array, effectively reducing the length of the array by 1.
55

6-
const arr = [1, 2, 3, 4, 5];
7-
arr.shift();
8-
console.log(arr);
6+
const myArray = [1, 2, 3, 4, 5];
7+
myArray.shift();
8+
console.log(myArray);

0 commit comments

Comments
 (0)