-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjan62021.js
132 lines (121 loc) · 3.46 KB
/
jan62021.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// adding reverse array, second largest number and isSortedArray methods
let myArray = {};
myArray.length = 0;
const addElement = (value) => {
myArray[myArray?.length ?? 0] = value;
myArray.length++;
};
const deleteElement = () => {
if (myArray?.length > 0) {
delete myArray[myArray?.length - 1];
myArray.length--;
} else {
throw Error(`Can't pop the empty array`);
}
};
const maximumElement = () => {
if (myArray?.length > 1) {
let maxValue = myArray[0];
for (let index = 1; index < myArray.length; index++) {
if (myArray[index] > maxValue) {
maxValue = myArray[index];
}
}
console.log(maxValue, "is the maximum value!");
} else {
throw Error(`I need atleast two elements to find the maximum value`);
}
};
const secondMaximumElement = () => {
if (myArray?.length > 1) {
let firstMax = myArray[0];
let secondMax;
for (let index = 1; index < myArray.length; index++) {
if (myArray[index] > firstMax) {
secondMax = firstMax;
firstMax = myArray[index];
}
if (myArray[index] > secondMax && myArray[index] < firstMax) {
secondMax = myArray[index];
}
}
console.log(secondMax, "is the second largest value!");
} else {
throw Error(`I need atleast two elements to find the maximum value`);
}
};
const checkIfArrayIsSorted = () => {
if (myArray?.length > 1) {
let isSorted = true;
for (let index = 0; index < myArray.length; index++) {
if (myArray[index] > myArray[index + 1]) {
isSorted = false;
break;
}
}
if (isSorted) {
console.log("Array is sorted!");
} else {
console.log("Array is not sorted!");
}
} else {
throw Error(`I need atleast two elements to check if sorted or not.`);
}
};
const printMeReverse = () => {
if (myArray.length > 1) {
let reverseArray = "[";
for (let index = myArray.length - 1; index >= 0; index--) {
reverseArray += ` ${myArray[index]} `;
}
reverseArray += "]";
console.log("Reversed ==>", reverseArray);
} else {
throw Error("Add atleast 2 elements to reverse");
}
};
const searchByIndex = (inputIndex) => {
if (inputIndex >= 0 && inputIndex < myArray.length) {
console.log("Value at", inputIndex, "index is", myArray[inputIndex]);
} else {
throw Error(`Array Out Of Bound!`);
}
};
const searchByValue = (inputValue) => {
if (myArray?.length > 1) {
let recordIndex = null;
for (let index = 0; index < myArray.length; index++) {
if (myArray[index] === inputValue) {
recordIndex = index;
return;
}
}
if (recordIndex) {
console.log("Input value ", inputValue, "is at", recordIndex, "index.");
} else {
console.log("Your value is not here!");
}
} else {
throw Error(`Empty Array. You can use pushElement function.`);
}
};
myArray.pushElement = addElement;
myArray.popElement = deleteElement;
myArray.getMax = maximumElement;
myArray.getSecondLargestNumber = secondMaximumElement;
myArray.isSorted = checkIfArrayIsSorted;
myArray.searchByIndex = searchByIndex;
myArray.searchByValue = searchByValue;
myArray.printMeReverse = printMeReverse;
for (let index = 0; index < 7; index++) {
const ranNum = Math.ceil(Math.random() * 100);
myArray.pushElement(ranNum);
}
console.clear();
myArray.getMax();
myArray.searchByIndex(0);
myArray.searchByValue(0);
myArray.isSorted();
myArray.getSecondLargestNumber();
myArray.printMeReverse();
console.log("array => ", myArray);