Skip to content

Commit afd0b45

Browse files
committed
♻️ refactor: improving and fixing some code
1 parent ecac786 commit afd0b45

File tree

13 files changed

+36
-107
lines changed

13 files changed

+36
-107
lines changed

Bit-Manipulation/IsPowerOfTwo.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,5 @@
2323
*/
2424

2525
export const IsPowerOfTwo = (n) => {
26-
if (n > 0 && (n & (n - 1)) === 0) {
27-
return true
28-
}
29-
return false
26+
return n > 0 && (n & (n - 1)) === 0
3027
}

Cellular-Automata/ConwaysGameOfLife.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ export function newGeneration (cells) {
2929

3030
// Decide whether the cell is alive or dead
3131
const alive = cells[i][j] === 1
32-
if ((alive && neighbourCount >= 2 && neighbourCount <= 3) || (!alive && neighbourCount === 3)) {
33-
nextGenerationRow.push(1)
34-
} else {
35-
nextGenerationRow.push(0)
36-
}
32+
33+
const cellIsAlive =
34+
(alive && neighbourCount >= 2 && neighbourCount <= 3) ||
35+
(!alive && neighbourCount === 3)
36+
37+
nextGenerationRow.push(cellIsAlive ? 1 : 0)
3738
}
3839
nextGeneration.push(nextGenerationRow)
3940
}

Conversions/DateDayDifference.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const DateToDay = (dd, mm, yyyy) => {
1919

2020
const DateDayDifference = (date1, date2) => {
2121
// firstly, check that both input are string or not.
22-
if (typeof date1 !== 'string' && typeof date2 !== 'string') {
22+
if (typeof date1 !== 'string' || typeof date2 !== 'string') {
2323
return new TypeError('Argument is not a string.')
2424
}
2525
// extract the first date

Data-Structures/Graph/test/Graph2.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ describe('Test Graph2', () => {
55
const graph = new Graph(vertices.length)
66

77
// adding vertices
8-
for (let i = 0; i < vertices.length; i++) {
9-
graph.addVertex(vertices[i])
10-
}
8+
vertices.forEach((vertice) => graph.addVertex(vertice))
119

1210
// adding edges
1311
graph.addEdge('A', 'B')
@@ -27,7 +25,7 @@ describe('Test Graph2', () => {
2725
expect(mockFn.mock.calls.length).toBe(vertices.length)
2826

2927
// Collect adjacency lists from output (call args)
30-
const adjListArr = mockFn.mock.calls.map(v => v[0])
28+
const adjListArr = mockFn.mock.calls.map((v) => v[0])
3129

3230
expect(adjListArr).toEqual([
3331
'A -> B D E ',

Data-Structures/Heap/MinPriorityQueue.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class MinPriorityQueue {
4747

4848
// returns boolean value whether the heap is full or not
4949
isFull () {
50-
if (this.size === this.capacity) return true
51-
return false
50+
return this.size === this.capacity
5251
}
5352

5453
// prints the heap

Data-Structures/Tree/Trie.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ Trie.prototype.contains = function (word) {
106106
// find the node with given prefix
107107
const node = this.findPrefix(word)
108108
// No such word exists
109-
if (node === null || node.count === 0) return false
110-
return true
109+
110+
return node !== null && node.count !== 0
111111
}
112112

113113
Trie.prototype.findOccurrences = function (word) {

Dynamic-Programming/FindMonthCalendar.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class Month {
5252
}
5353

5454
isLeapYear (year) {
55-
if (((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))) return true
56-
return false
55+
return ((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))
5756
}
5857

5958
isGreater (startDate, endDate) {

Graphs/DijkstraSmallestPath.js

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -41,70 +41,3 @@ function solve (graph, s) {
4141
}
4242

4343
export { solve }
44-
45-
// // create graph
46-
// const graph = {}
47-
48-
// const layout = {
49-
// R: ['2'],
50-
// 2: ['3', '4'],
51-
// 3: ['4', '6', '13'],
52-
// 4: ['5', '8'],
53-
// 5: ['7', '11'],
54-
// 6: ['13', '15'],
55-
// 7: ['10'],
56-
// 8: ['11', '13'],
57-
// 9: ['14'],
58-
// 10: [],
59-
// 11: ['12'],
60-
// 12: [],
61-
// 13: ['14'],
62-
// 14: [],
63-
// 15: []
64-
// }
65-
66-
// // convert uni-directional to bi-directional graph
67-
// let graph = {
68-
// a: {e:1, b:1, g:3},
69-
// b: {a:1, c:1},
70-
// c: {b:1, d:1},
71-
// d: {c:1, e:1},
72-
// e: {d:1, a:1},
73-
// f: {g:1, h:1},
74-
// g: {a:3, f:1},
75-
// h: {f:1}
76-
// };
77-
78-
// for (const id in layout) {
79-
// if (!graph[id]) { graph[id] = {} }
80-
// layout[id].forEach(function (aid) {
81-
// graph[id][aid] = 1
82-
// if (!graph[aid]) { graph[aid] = {} }
83-
// graph[aid][id] = 1
84-
// })
85-
// }
86-
87-
// // choose start node
88-
// const start = '10'
89-
// // get all solutions
90-
// const solutions = solve(graph, start)
91-
92-
// // for s in solutions..
93-
// ' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')'
94-
95-
// From '10' to
96-
// -> 2: [7, 5, 4, 2] (dist:4)
97-
// -> 3: [7, 5, 4, 3] (dist:4)
98-
// -> 4: [7, 5, 4] (dist:3)
99-
// -> 5: [7, 5] (dist:2)
100-
// -> 6: [7, 5, 4, 3, 6] (dist:5)
101-
// -> 7: [7] (dist:1)
102-
// -> 8: [7, 5, 4, 8] (dist:4)
103-
// -> 9: [7, 5, 4, 3, 13, 14, 9] (dist:7)
104-
// -> 10: [] (dist:0)
105-
// -> 11: [7, 5, 11] (dist:3)
106-
// -> 12: [7, 5, 11, 12] (dist:4)
107-
// -> 13: [7, 5, 4, 3, 13] (dist:5)
108-
// -> 14: [7, 5, 4, 3, 13, 14] (dist:6)
109-
// -> 15: [7, 5, 4, 3, 6, 15] (dist:6)
110-
// -> R: [7, 5, 4, 2, R] (dist:5)

Maths/MatrixMultiplication.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ const matrixCheck = (matrix) => {
2020
// tests to see if the matrices have a like side, i.e. the row length on the first matrix matches the column length on the second matrix, or vice versa.
2121
const twoMatricesCheck = (first, second) => {
2222
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)]
23-
if (firstRowLength !== secondColLength || secondRowLength !== firstColLength) {
24-
// These matrices do not have a common side
25-
return false
26-
} else {
27-
return true
28-
}
23+
24+
// These matrices do not have a common side
25+
return firstRowLength === secondColLength && secondRowLength === firstColLength
2926
}
3027

3128
// returns an empty array that has the same number of rows as the left matrix being multiplied.

Maths/MidpointIntegration.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ function integralEvaluation (N, a, b, func) {
4242
// Calculate the integral
4343
let result = h
4444
temp = 0
45-
for (let i = 0; i < pointsArray.length; i++) temp += pointsArray[i]
45+
pointsArray.forEach(point => {
46+
temp += point
47+
})
4648

4749
result *= temp
4850

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy