Skip to content
5 changes: 1 addition & 4 deletions Bit-Manipulation/IsPowerOfTwo.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@
*/

export const IsPowerOfTwo = (n) => {
if (n > 0 && (n & (n - 1)) === 0) {
return true
}
return false
return n > 0 && (n & (n - 1)) === 0
}
11 changes: 6 additions & 5 deletions Cellular-Automata/ConwaysGameOfLife.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ export function newGeneration (cells) {

// Decide whether the cell is alive or dead
const alive = cells[i][j] === 1
if ((alive && neighbourCount >= 2 && neighbourCount <= 3) || (!alive && neighbourCount === 3)) {
nextGenerationRow.push(1)
} else {
nextGenerationRow.push(0)
}

const cellIsAlive =
(alive && neighbourCount >= 2 && neighbourCount <= 3) ||
(!alive && neighbourCount === 3)

nextGenerationRow.push(cellIsAlive ? 1 : 0)
}
nextGeneration.push(nextGenerationRow)
}
Expand Down
2 changes: 1 addition & 1 deletion Conversions/DateDayDifference.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const DateToDay = (dd, mm, yyyy) => {

const DateDayDifference = (date1, date2) => {
// firstly, check that both input are string or not.
if (typeof date1 !== 'string' && typeof date2 !== 'string') {
if (typeof date1 !== 'string' || typeof date2 !== 'string') {
return new TypeError('Argument is not a string.')
}
// extract the first date
Expand Down
9 changes: 8 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* **Backtracking**
* [AllCombinationsOfSizeK](Backtracking/AllCombinationsOfSizeK.js)
* [generateParentheses](Backtracking/generateParentheses.js)
* [GeneratePermutations](Backtracking/GeneratePermutations.js)
* [KnightTour](Backtracking/KnightTour.js)
* [NQueens](Backtracking/NQueens.js)
Expand All @@ -18,12 +19,14 @@
* [Memoize](Cache/Memoize.js)
* **Cellular-Automata**
* [ConwaysGameOfLife](Cellular-Automata/ConwaysGameOfLife.js)
* [Elementary](Cellular-Automata/Elementary.js)
* **Ciphers**
* [AffineCipher](Ciphers/AffineCipher.js)
* [Atbash](Ciphers/Atbash.js)
* [CaesarCipher](Ciphers/CaesarCipher.js)
* [KeyFinder](Ciphers/KeyFinder.js)
* [KeywordShiftedAlphabet](Ciphers/KeywordShiftedAlphabet.js)
* [MorseCode](Ciphers/MorseCode.js)
* [ROT13](Ciphers/ROT13.js)
* [VigenereCipher](Ciphers/VigenereCipher.js)
* [XORCipher](Ciphers/XORCipher.js)
Expand Down Expand Up @@ -66,6 +69,7 @@
* [Graph2](Data-Structures/Graph/Graph2.js)
* [Graph3](Data-Structures/Graph/Graph3.js)
* **Heap**
* [KeyPriorityQueue](Data-Structures/Heap/KeyPriorityQueue.js)
* [MaxHeap](Data-Structures/Heap/MaxHeap.js)
* [MinHeap](Data-Structures/Heap/MinHeap.js)
* [MinPriorityQueue](Data-Structures/Heap/MinPriorityQueue.js)
Expand Down Expand Up @@ -112,7 +116,10 @@
* [Shuf](Dynamic-Programming/Shuf.js)
* [SieveOfEratosthenes](Dynamic-Programming/SieveOfEratosthenes.js)
* **Sliding-Window**
* [HouseRobber](Dynamic-Programming/Sliding-Window/HouseRobber.js)
* [LongestSubstringWithoutRepeatingCharacters](Dynamic-Programming/Sliding-Window/LongestSubstringWithoutRepeatingCharacters.js)
* [MaxConsecutiveOnes](Dynamic-Programming/Sliding-Window/MaxConsecutiveOnes.js)
* [MaxConsecutiveOnesIII](Dynamic-Programming/Sliding-Window/MaxConsecutiveOnesIII.js)
* [PermutationinString](Dynamic-Programming/Sliding-Window/PermutationinString.js)
* [SudokuSolver](Dynamic-Programming/SudokuSolver.js)
* [TrappingRainWater](Dynamic-Programming/TrappingRainWater.js)
Expand Down Expand Up @@ -212,6 +219,7 @@
* [ModularBinaryExponentiationRecursive](Maths/ModularBinaryExponentiationRecursive.js)
* [NumberOfDigits](Maths/NumberOfDigits.js)
* [Palindrome](Maths/Palindrome.js)
* [ParityOutlier](Maths/ParityOutlier.js)
* [PascalTriangle](Maths/PascalTriangle.js)
* [PerfectCube](Maths/PerfectCube.js)
* [PerfectNumber](Maths/PerfectNumber.js)
Expand Down Expand Up @@ -365,7 +373,6 @@
* [Upper](String/Upper.js)
* [ValidateCreditCard](String/ValidateCreditCard.js)
* [ValidateEmail](String/ValidateEmail.js)
* [ValidateUrl](String/ValidateUrl.js)
* [ZFunction](String/ZFunction.js)
* **Timing-Functions**
* [GetMonthDays](Timing-Functions/GetMonthDays.js)
Expand Down
6 changes: 2 additions & 4 deletions Data-Structures/Graph/test/Graph2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ describe('Test Graph2', () => {
const graph = new Graph(vertices.length)

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

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

// Collect adjacency lists from output (call args)
const adjListArr = mockFn.mock.calls.map(v => v[0])
const adjListArr = mockFn.mock.calls.map((v) => v[0])

expect(adjListArr).toEqual([
'A -> B D E ',
Expand Down
3 changes: 1 addition & 2 deletions Data-Structures/Heap/MinPriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class MinPriorityQueue {

// returns boolean value whether the heap is full or not
isFull () {
if (this.size === this.capacity) return true
return false
return this.size === this.capacity
}

// prints the heap
Expand Down
4 changes: 2 additions & 2 deletions Data-Structures/Tree/Trie.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ Trie.prototype.contains = function (word) {
// find the node with given prefix
const node = this.findPrefix(word)
// No such word exists
if (node === null || node.count === 0) return false
return true

return node !== null && node.count !== 0
}

Trie.prototype.findOccurrences = function (word) {
Expand Down
3 changes: 1 addition & 2 deletions Dynamic-Programming/FindMonthCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class Month {
}

isLeapYear (year) {
if (((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))) return true
return false
return ((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))
}

isGreater (startDate, endDate) {
Expand Down
67 changes: 0 additions & 67 deletions Graphs/DijkstraSmallestPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,70 +41,3 @@ function solve (graph, s) {
}

export { solve }

// // create graph
// const graph = {}

// const layout = {
// R: ['2'],
// 2: ['3', '4'],
// 3: ['4', '6', '13'],
// 4: ['5', '8'],
// 5: ['7', '11'],
// 6: ['13', '15'],
// 7: ['10'],
// 8: ['11', '13'],
// 9: ['14'],
// 10: [],
// 11: ['12'],
// 12: [],
// 13: ['14'],
// 14: [],
// 15: []
// }

// // convert uni-directional to bi-directional graph
// let graph = {
// a: {e:1, b:1, g:3},
// b: {a:1, c:1},
// c: {b:1, d:1},
// d: {c:1, e:1},
// e: {d:1, a:1},
// f: {g:1, h:1},
// g: {a:3, f:1},
// h: {f:1}
// };

// for (const id in layout) {
// if (!graph[id]) { graph[id] = {} }
// layout[id].forEach(function (aid) {
// graph[id][aid] = 1
// if (!graph[aid]) { graph[aid] = {} }
// graph[aid][id] = 1
// })
// }

// // choose start node
// const start = '10'
// // get all solutions
// const solutions = solve(graph, start)

// // for s in solutions..
// ' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')'

// From '10' to
// -> 2: [7, 5, 4, 2] (dist:4)
// -> 3: [7, 5, 4, 3] (dist:4)
// -> 4: [7, 5, 4] (dist:3)
// -> 5: [7, 5] (dist:2)
// -> 6: [7, 5, 4, 3, 6] (dist:5)
// -> 7: [7] (dist:1)
// -> 8: [7, 5, 4, 8] (dist:4)
// -> 9: [7, 5, 4, 3, 13, 14, 9] (dist:7)
// -> 10: [] (dist:0)
// -> 11: [7, 5, 11] (dist:3)
// -> 12: [7, 5, 11, 12] (dist:4)
// -> 13: [7, 5, 4, 3, 13] (dist:5)
// -> 14: [7, 5, 4, 3, 13, 14] (dist:6)
// -> 15: [7, 5, 4, 3, 6, 15] (dist:6)
// -> R: [7, 5, 4, 2, R] (dist:5)
6 changes: 1 addition & 5 deletions Maths/LeapYear.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,5 @@
* @returns {boolean} true if this is a leap year, false otherwise.
*/
export const isLeapYear = (year) => {
if (year % 400 === 0) return true
if (year % 100 === 0) return false
if (year % 4 === 0) return true

return false
return ((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))
}
9 changes: 3 additions & 6 deletions Maths/MatrixMultiplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ const matrixCheck = (matrix) => {
// 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.
const twoMatricesCheck = (first, second) => {
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)]
if (firstRowLength !== secondColLength || secondRowLength !== firstColLength) {
// These matrices do not have a common side
return false
} else {
return true
}

// These matrices do not have a common side
return firstRowLength === secondColLength && secondRowLength === firstColLength
}

// returns an empty array that has the same number of rows as the left matrix being multiplied.
Expand Down
4 changes: 3 additions & 1 deletion Maths/MidpointIntegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ function integralEvaluation (N, a, b, func) {
// Calculate the integral
let result = h
temp = 0
for (let i = 0; i < pointsArray.length; i++) temp += pointsArray[i]
pointsArray.forEach(point => {
temp += point
})

result *= temp

Expand Down
4 changes: 3 additions & 1 deletion Maths/SimpsonIntegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ function integralEvaluation (N, a, b, func) {
// Calculate the integral
let result = h / 3
temp = 0
for (let i = 0; i < pointsArray.length; i++) temp += pointsArray[i]
pointsArray.forEach(point => {
temp += point
})

result *= temp

Expand Down
23 changes: 12 additions & 11 deletions Sorts/SimplifiedWiggleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@
import { quickSelectSearch } from '../Search/QuickSelectSearch.js'

export const simplifiedWiggleSort = function (arr) {
const arrSize = arr.length
// find Median using QuickSelect
let median = quickSelectSearch(arr, Math.floor(arr.length / 2.0))
median = median[Math.floor(arr.length / 2.0)]
let median = quickSelectSearch(arr, Math.floor(arrSize / 2.0))
median = median[Math.floor(arrSize / 2.0)]

const sorted = new Array(arr.length)
const sorted = new Array(arrSize)

let smallerThanMedianIndx = 0
let greaterThanMedianIndx = arr.length - 1 - (arr.length % 2)
let greaterThanMedianIndx = arrSize - 1 - (arrSize % 2)

for (let i = 0; i < arr.length; i++) {
if (arr[i] > median) {
sorted[greaterThanMedianIndx] = arr[i]
arr.forEach(element => {
if (element > median) {
sorted[greaterThanMedianIndx] = element
greaterThanMedianIndx -= 2
} else {
if (smallerThanMedianIndx < arr.length) {
sorted[smallerThanMedianIndx] = arr[i]
if (smallerThanMedianIndx < arrSize) {
sorted[smallerThanMedianIndx] = element
smallerThanMedianIndx += 2
} else {
sorted[greaterThanMedianIndx] = arr[i]
sorted[greaterThanMedianIndx] = element
greaterThanMedianIndx -= 2
}
}
}
})

return sorted
}
2 changes: 1 addition & 1 deletion String/CheckRearrangePalindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const palindromeRearranging = (str) => {
return 'Not a string'
}
// Check if is a empty string
if (str.length === 0) {
if (!str) {
return 'Empty string'
}

Expand Down