mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
chore: format code (#1515)
* chore: format code * Updated Documentation in README.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -9,6 +9,7 @@
|
|||||||
* [SumOfSubset](Backtracking/SumOfSubset.js)
|
* [SumOfSubset](Backtracking/SumOfSubset.js)
|
||||||
* **Bit-Manipulation**
|
* **Bit-Manipulation**
|
||||||
* [BinaryCountSetBits](Bit-Manipulation/BinaryCountSetBits.js)
|
* [BinaryCountSetBits](Bit-Manipulation/BinaryCountSetBits.js)
|
||||||
|
* [GrayCodes](Bit-Manipulation/GrayCodes.js)
|
||||||
* [IsPowerofFour](Bit-Manipulation/IsPowerofFour.js)
|
* [IsPowerofFour](Bit-Manipulation/IsPowerofFour.js)
|
||||||
* [IsPowerOfTwo](Bit-Manipulation/IsPowerOfTwo.js)
|
* [IsPowerOfTwo](Bit-Manipulation/IsPowerOfTwo.js)
|
||||||
* [LogTwo](Bit-Manipulation/LogTwo.js)
|
* [LogTwo](Bit-Manipulation/LogTwo.js)
|
||||||
@ -55,6 +56,7 @@
|
|||||||
* [OctToDecimal](Conversions/OctToDecimal.js)
|
* [OctToDecimal](Conversions/OctToDecimal.js)
|
||||||
* [OuncesToKilograms](Conversions/OuncesToKilograms.js)
|
* [OuncesToKilograms](Conversions/OuncesToKilograms.js)
|
||||||
* [RailwayTimeConversion](Conversions/RailwayTimeConversion.js)
|
* [RailwayTimeConversion](Conversions/RailwayTimeConversion.js)
|
||||||
|
* [RgbHslConversion](Conversions/RgbHslConversion.js)
|
||||||
* [RgbHsvConversion](Conversions/RgbHsvConversion.js)
|
* [RgbHsvConversion](Conversions/RgbHsvConversion.js)
|
||||||
* [RGBToHex](Conversions/RGBToHex.js)
|
* [RGBToHex](Conversions/RGBToHex.js)
|
||||||
* [RomanToDecimal](Conversions/RomanToDecimal.js)
|
* [RomanToDecimal](Conversions/RomanToDecimal.js)
|
||||||
@ -72,14 +74,14 @@
|
|||||||
* [Graph2](Data-Structures/Graph/Graph2.js)
|
* [Graph2](Data-Structures/Graph/Graph2.js)
|
||||||
* [Graph3](Data-Structures/Graph/Graph3.js)
|
* [Graph3](Data-Structures/Graph/Graph3.js)
|
||||||
* **Heap**
|
* **Heap**
|
||||||
|
* [BinaryHeap](Data-Structures/Heap/BinaryHeap.js)
|
||||||
* [KeyPriorityQueue](Data-Structures/Heap/KeyPriorityQueue.js)
|
* [KeyPriorityQueue](Data-Structures/Heap/KeyPriorityQueue.js)
|
||||||
* [MaxHeap](Data-Structures/Heap/MaxHeap.js)
|
|
||||||
* [MinHeap](Data-Structures/Heap/MinHeap.js)
|
|
||||||
* [MinPriorityQueue](Data-Structures/Heap/MinPriorityQueue.js)
|
* [MinPriorityQueue](Data-Structures/Heap/MinPriorityQueue.js)
|
||||||
* **Linked-List**
|
* **Linked-List**
|
||||||
* [AddTwoNumbers](Data-Structures/Linked-List/AddTwoNumbers.js)
|
* [AddTwoNumbers](Data-Structures/Linked-List/AddTwoNumbers.js)
|
||||||
* [CycleDetection](Data-Structures/Linked-List/CycleDetection.js)
|
* [CycleDetection](Data-Structures/Linked-List/CycleDetection.js)
|
||||||
* [DoublyLinkedList](Data-Structures/Linked-List/DoublyLinkedList.js)
|
* [DoublyLinkedList](Data-Structures/Linked-List/DoublyLinkedList.js)
|
||||||
|
* [MergeTwoSortedLinkedLists](Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js)
|
||||||
* [ReverseSinglyLinkedList](Data-Structures/Linked-List/ReverseSinglyLinkedList.js)
|
* [ReverseSinglyLinkedList](Data-Structures/Linked-List/ReverseSinglyLinkedList.js)
|
||||||
* [SinglyCircularLinkedList](Data-Structures/Linked-List/SinglyCircularLinkedList.js)
|
* [SinglyCircularLinkedList](Data-Structures/Linked-List/SinglyCircularLinkedList.js)
|
||||||
* [SinglyLinkedList](Data-Structures/Linked-List/SinglyLinkedList.js)
|
* [SinglyLinkedList](Data-Structures/Linked-List/SinglyLinkedList.js)
|
||||||
@ -88,6 +90,7 @@
|
|||||||
* [Queue](Data-Structures/Queue/Queue.js)
|
* [Queue](Data-Structures/Queue/Queue.js)
|
||||||
* [QueueUsing2Stacks](Data-Structures/Queue/QueueUsing2Stacks.js)
|
* [QueueUsing2Stacks](Data-Structures/Queue/QueueUsing2Stacks.js)
|
||||||
* **Stack**
|
* **Stack**
|
||||||
|
* [EvaluateExpression](Data-Structures/Stack/EvaluateExpression.js)
|
||||||
* [Stack](Data-Structures/Stack/Stack.js)
|
* [Stack](Data-Structures/Stack/Stack.js)
|
||||||
* [StackES6](Data-Structures/Stack/StackES6.js)
|
* [StackES6](Data-Structures/Stack/StackES6.js)
|
||||||
* **Tree**
|
* **Tree**
|
||||||
@ -179,6 +182,7 @@
|
|||||||
* [DecimalExpansion](Maths/DecimalExpansion.js)
|
* [DecimalExpansion](Maths/DecimalExpansion.js)
|
||||||
* [DecimalIsolate](Maths/DecimalIsolate.js)
|
* [DecimalIsolate](Maths/DecimalIsolate.js)
|
||||||
* [DegreeToRadian](Maths/DegreeToRadian.js)
|
* [DegreeToRadian](Maths/DegreeToRadian.js)
|
||||||
|
* [Determinant](Maths/Determinant.js)
|
||||||
* [EuclideanDistance](Maths/EuclideanDistance.js)
|
* [EuclideanDistance](Maths/EuclideanDistance.js)
|
||||||
* [EulerMethod](Maths/EulerMethod.js)
|
* [EulerMethod](Maths/EulerMethod.js)
|
||||||
* [EulersTotient](Maths/EulersTotient.js)
|
* [EulersTotient](Maths/EulersTotient.js)
|
||||||
@ -239,6 +243,7 @@
|
|||||||
* [RadianToDegree](Maths/RadianToDegree.js)
|
* [RadianToDegree](Maths/RadianToDegree.js)
|
||||||
* [ReverseNumber](Maths/ReverseNumber.js)
|
* [ReverseNumber](Maths/ReverseNumber.js)
|
||||||
* [ReversePolishNotation](Maths/ReversePolishNotation.js)
|
* [ReversePolishNotation](Maths/ReversePolishNotation.js)
|
||||||
|
* [RowEchelon](Maths/RowEchelon.js)
|
||||||
* [ShorsAlgorithm](Maths/ShorsAlgorithm.js)
|
* [ShorsAlgorithm](Maths/ShorsAlgorithm.js)
|
||||||
* [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js)
|
* [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js)
|
||||||
* [SieveOfEratosthenesIntArray](Maths/SieveOfEratosthenesIntArray.js)
|
* [SieveOfEratosthenesIntArray](Maths/SieveOfEratosthenesIntArray.js)
|
||||||
|
@ -36,7 +36,7 @@ describe('BinaryHeap', () => {
|
|||||||
it('should handle insertion of duplicate values', () => {
|
it('should handle insertion of duplicate values', () => {
|
||||||
// Check if the heap handles duplicate values correctly
|
// Check if the heap handles duplicate values correctly
|
||||||
minHeap.insert(2)
|
minHeap.insert(2)
|
||||||
console.log(minHeap.heap);
|
console.log(minHeap.heap)
|
||||||
expect(minHeap.heap).toEqual([1, 3, 2, 4, 8, 6, 2])
|
expect(minHeap.heap).toEqual([1, 3, 2, 4, 8, 6, 2])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -6,53 +6,53 @@
|
|||||||
* @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid.
|
* @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid.
|
||||||
*/
|
*/
|
||||||
function evaluatePostfixExpression(expression) {
|
function evaluatePostfixExpression(expression) {
|
||||||
const stack = [];
|
const stack = []
|
||||||
|
|
||||||
// Helper function to perform an operation and push the result to the stack. Returns success.
|
// Helper function to perform an operation and push the result to the stack. Returns success.
|
||||||
function performOperation(operator) {
|
function performOperation(operator) {
|
||||||
const rightOp = stack.pop(); // Right operand is the top of the stack
|
const rightOp = stack.pop() // Right operand is the top of the stack
|
||||||
const leftOp = stack.pop(); // Left operand is the next item on the stack
|
const leftOp = stack.pop() // Left operand is the next item on the stack
|
||||||
|
|
||||||
if (leftOp === undefined || rightOp === undefined) {
|
if (leftOp === undefined || rightOp === undefined) {
|
||||||
return false; // Invalid expression
|
return false // Invalid expression
|
||||||
}
|
}
|
||||||
switch (operator) {
|
switch (operator) {
|
||||||
case '+':
|
case '+':
|
||||||
stack.push(leftOp + rightOp);
|
stack.push(leftOp + rightOp)
|
||||||
break;
|
break
|
||||||
case '-':
|
case '-':
|
||||||
stack.push(leftOp - rightOp);
|
stack.push(leftOp - rightOp)
|
||||||
break;
|
break
|
||||||
case '*':
|
case '*':
|
||||||
stack.push(leftOp * rightOp);
|
stack.push(leftOp * rightOp)
|
||||||
break;
|
break
|
||||||
case '/':
|
case '/':
|
||||||
if (rightOp === 0) {
|
if (rightOp === 0) {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
stack.push(leftOp / rightOp);
|
stack.push(leftOp / rightOp)
|
||||||
break;
|
break
|
||||||
default:
|
default:
|
||||||
return false; // Unknown operator
|
return false // Unknown operator
|
||||||
}
|
}
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokens = expression.split(/\s+/);
|
const tokens = expression.split(/\s+/)
|
||||||
|
|
||||||
for (const token of tokens) {
|
for (const token of tokens) {
|
||||||
if (!isNaN(parseFloat(token))) {
|
if (!isNaN(parseFloat(token))) {
|
||||||
// If the token is a number, push it to the stack
|
// If the token is a number, push it to the stack
|
||||||
stack.push(parseFloat(token));
|
stack.push(parseFloat(token))
|
||||||
} else {
|
} else {
|
||||||
// If the token is an operator, perform the operation
|
// If the token is an operator, perform the operation
|
||||||
if (!performOperation(token)) {
|
if (!performOperation(token)) {
|
||||||
return null; // Invalid expression
|
return null // Invalid expression
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (stack.length === 1) ? stack[0] : null;
|
return stack.length === 1 ? stack[0] : null
|
||||||
}
|
}
|
||||||
|
|
||||||
export { evaluatePostfixExpression };
|
export { evaluatePostfixExpression }
|
||||||
|
@ -1,22 +1,21 @@
|
|||||||
import { evaluatePostfixExpression } from '../EvaluateExpression.js';
|
import { evaluatePostfixExpression } from '../EvaluateExpression.js'
|
||||||
|
|
||||||
describe('evaluatePostfixExpression', () => {
|
describe('evaluatePostfixExpression', () => {
|
||||||
it('should evaluate a valid expression', () => {
|
it('should evaluate a valid expression', () => {
|
||||||
const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 11
|
const expression = '3 4 * 2 / 5 +' // (3 * 4) / 2 + 5 = 11
|
||||||
const result = evaluatePostfixExpression(expression);
|
const result = evaluatePostfixExpression(expression)
|
||||||
expect(result).toBe(11);
|
expect(result).toBe(11)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should handle division by zero', () => {
|
it('should handle division by zero', () => {
|
||||||
const expression = '3 0 /'; // Division by zero
|
const expression = '3 0 /' // Division by zero
|
||||||
const result = evaluatePostfixExpression(expression);
|
const result = evaluatePostfixExpression(expression)
|
||||||
expect(result).toBe(null);
|
expect(result).toBe(null)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should handle an invalid expression', () => {
|
it('should handle an invalid expression', () => {
|
||||||
const expression = '3 * 4 2 / +'; // Invalid expression
|
const expression = '3 * 4 2 / +' // Invalid expression
|
||||||
const result = evaluatePostfixExpression(expression);
|
const result = evaluatePostfixExpression(expression)
|
||||||
expect(result).toBe(null);
|
expect(result).toBe(null)
|
||||||
});
|
})
|
||||||
|
})
|
||||||
});
|
|
||||||
|
@ -54,10 +54,7 @@ describe('Determinant', () => {
|
|||||||
'Square matrix is required.'
|
'Square matrix is required.'
|
||||||
],
|
],
|
||||||
[[1, 3, 2, [5, 8, 6], 3], 'Input is not a valid 2D matrix.']
|
[[1, 3, 2, [5, 8, 6], 3], 'Input is not a valid 2D matrix.']
|
||||||
])(
|
])('Should return the error message.', (matrix, expected) => {
|
||||||
'Should return the error message.',
|
|
||||||
(matrix, expected) => {
|
|
||||||
expect(() => determinant(matrix)).toThrowError(expected)
|
expect(() => determinant(matrix)).toThrowError(expected)
|
||||||
}
|
})
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user