Skip to content

Commit 0144e77

Browse files
committed
chore: convert functions to an ES2015 classes
1 parent 314144f commit 0144e77

File tree

8 files changed

+297
-285
lines changed

8 files changed

+297
-285
lines changed

Conversions/RailwayTimeConversion.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const RailwayTimeConversion = (timeString) => {
2424
const [hour, minute, secondWithShift] = timeString.split(':')
2525
// split second and shift value.
2626
const [second, shift] = [
27-
secondWithShift.substr(0, 2),
28-
secondWithShift.substr(2)
27+
secondWithShift.substring(0, 2),
28+
secondWithShift.substring(2)
2929
]
3030
// convert shifted time to not-shift time(Railway time) by using the above explanation.
3131
if (shift === 'PM') {

Data-Structures/Array/Reverse.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
const Reverse = (arr) => {
99
// limit specifies the amount of Reverse actions
10-
for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--) {
11-
const temp = arr[i]
12-
arr[i] = arr[j]
13-
arr[j] = temp
14-
}
10+
for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--)
11+
[arr[i], arr[j]] = [arr[j], arr[i]] // Swapping elements ES6 way
12+
1513
return arr
1614
}
1715
export { Reverse }

Data-Structures/Stack/Stack.js

+35-33
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,47 @@
99

1010
// Creates a stack constructor
1111
const Stack = (function () {
12-
function Stack() {
13-
// The top of the Stack
14-
this.top = 0
15-
// The array representation of the stack
16-
this.stack = []
17-
}
18-
19-
// Adds a value onto the end of the stack
20-
Stack.prototype.push = function (value) {
21-
this.stack[this.top] = value
22-
this.top++
23-
}
12+
class Stack {
13+
constructor() {
14+
// The top of the Stack
15+
this.top = 0
16+
// The array representation of the stack
17+
this.stack = []
18+
}
2419

25-
// Removes and returns the value at the end of the stack
26-
Stack.prototype.pop = function () {
27-
if (this.top === 0) {
28-
return 'Stack is Empty'
20+
// Adds a value onto the end of the stack
21+
push(value) {
22+
this.stack[this.top] = value
23+
this.top++
2924
}
3025

31-
this.top--
32-
const result = this.stack[this.top]
33-
this.stack = this.stack.splice(0, this.top)
34-
return result
35-
}
26+
// Removes and returns the value at the end of the stack
27+
pop() {
28+
if (this.top === 0) {
29+
return 'Stack is Empty'
30+
}
3631

37-
// Returns the size of the stack
38-
Stack.prototype.size = function () {
39-
return this.top
40-
}
32+
this.top--
33+
const result = this.stack[this.top]
34+
this.stack = this.stack.splice(0, this.top)
35+
return result
36+
}
4137

42-
// Returns the value at the end of the stack
43-
Stack.prototype.peek = function () {
44-
return this.stack[this.top - 1]
45-
}
38+
// Returns the size of the stack
39+
size() {
40+
return this.top
41+
}
42+
43+
// Returns the value at the end of the stack
44+
peek() {
45+
return this.stack[this.top - 1]
46+
}
4647

47-
// To see all the elements in the stack
48-
Stack.prototype.view = function (output = (value) => console.log(value)) {
49-
for (let i = 0; i < this.top; i++) {
50-
output(this.stack[i])
48+
// To see all the elements in the stack
49+
view(output = (value) => console.log(value)) {
50+
for (let i = 0; i < this.top; i++) {
51+
output(this.stack[i])
52+
}
5153
}
5254
}
5355

Data-Structures/Tree/AVLTree.js

+52-47
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,62 @@ let utils
3232
* If no argument is sent it uses utils.comparator
3333
*/
3434
const AVLTree = (function () {
35-
function _avl(comp) {
36-
/** @public comparator function */
37-
this._comp = undefined
38-
this._comp = comp !== undefined ? comp : utils.comparator()
35+
class _avl {
36+
constructor(comp) {
37+
/** @public comparator function */
38+
this._comp = undefined
39+
this._comp = comp !== undefined ? comp : utils.comparator()
3940

40-
/** @public root of the AVL Tree */
41-
this.root = null
42-
/** @public number of elements in AVL Tree */
43-
this.size = 0
41+
/** @public root of the AVL Tree */
42+
this.root = null
43+
/** @public number of elements in AVL Tree */
44+
this.size = 0
45+
}
46+
47+
/* Public Functions */
48+
/**
49+
* For Adding Elements to AVL Tree
50+
* @param {any} _val
51+
* Since in AVL Tree an element can only occur once so
52+
* if a element exists it return false
53+
* @returns {Boolean} element added or not
54+
*/
55+
add(_val) {
56+
const prevSize = this.size
57+
this.root = insert(this.root, _val, this)
58+
return this.size !== prevSize
59+
}
60+
/**
61+
* TO check is a particular element exists or not
62+
* @param {any} _val
63+
* @returns {Boolean} exists or not
64+
*/
65+
find(_val) {
66+
const temp = searchAVLTree(this.root, _val, this)
67+
return temp != null
68+
}
69+
/**
70+
*
71+
* @param {any} _val
72+
* It is possible that element doesn't exists in tree
73+
* in that case it return false
74+
* @returns {Boolean} if element was found and deleted
75+
*/
76+
remove(_val) {
77+
const prevSize = this.size
78+
this.root = deleteElement(this.root, _val, this)
79+
return prevSize !== this.size
80+
}
4481
}
4582

4683
// creates new Node Object
47-
const Node = function (val) {
48-
this._val = val
49-
this._left = null
50-
this._right = null
51-
this._height = 1
84+
class Node {
85+
constructor(val) {
86+
this._val = val
87+
this._left = null
88+
this._right = null
89+
this._height = 1
90+
}
5291
}
5392

5493
// get height of a node
@@ -199,40 +238,6 @@ const AVLTree = (function () {
199238
return searchAVLTree(root._left, val, tree)
200239
}
201240

202-
/* Public Functions */
203-
/**
204-
* For Adding Elements to AVL Tree
205-
* @param {any} _val
206-
* Since in AVL Tree an element can only occur once so
207-
* if a element exists it return false
208-
* @returns {Boolean} element added or not
209-
*/
210-
_avl.prototype.add = function (_val) {
211-
const prevSize = this.size
212-
this.root = insert(this.root, _val, this)
213-
return this.size !== prevSize
214-
}
215-
/**
216-
* TO check is a particular element exists or not
217-
* @param {any} _val
218-
* @returns {Boolean} exists or not
219-
*/
220-
_avl.prototype.find = function (_val) {
221-
const temp = searchAVLTree(this.root, _val, this)
222-
return temp != null
223-
}
224-
/**
225-
*
226-
* @param {any} _val
227-
* It is possible that element doesn't exists in tree
228-
* in that case it return false
229-
* @returns {Boolean} if element was found and deleted
230-
*/
231-
_avl.prototype.remove = function (_val) {
232-
const prevSize = this.size
233-
this.root = deleteElement(this.root, _val, this)
234-
return prevSize !== this.size
235-
}
236241
return _avl
237242
})()
238243

0 commit comments

Comments
 (0)