mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +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)
|
||||
* **Bit-Manipulation**
|
||||
* [BinaryCountSetBits](Bit-Manipulation/BinaryCountSetBits.js)
|
||||
* [GrayCodes](Bit-Manipulation/GrayCodes.js)
|
||||
* [IsPowerofFour](Bit-Manipulation/IsPowerofFour.js)
|
||||
* [IsPowerOfTwo](Bit-Manipulation/IsPowerOfTwo.js)
|
||||
* [LogTwo](Bit-Manipulation/LogTwo.js)
|
||||
@ -55,6 +56,7 @@
|
||||
* [OctToDecimal](Conversions/OctToDecimal.js)
|
||||
* [OuncesToKilograms](Conversions/OuncesToKilograms.js)
|
||||
* [RailwayTimeConversion](Conversions/RailwayTimeConversion.js)
|
||||
* [RgbHslConversion](Conversions/RgbHslConversion.js)
|
||||
* [RgbHsvConversion](Conversions/RgbHsvConversion.js)
|
||||
* [RGBToHex](Conversions/RGBToHex.js)
|
||||
* [RomanToDecimal](Conversions/RomanToDecimal.js)
|
||||
@ -72,14 +74,14 @@
|
||||
* [Graph2](Data-Structures/Graph/Graph2.js)
|
||||
* [Graph3](Data-Structures/Graph/Graph3.js)
|
||||
* **Heap**
|
||||
* [BinaryHeap](Data-Structures/Heap/BinaryHeap.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)
|
||||
* **Linked-List**
|
||||
* [AddTwoNumbers](Data-Structures/Linked-List/AddTwoNumbers.js)
|
||||
* [CycleDetection](Data-Structures/Linked-List/CycleDetection.js)
|
||||
* [DoublyLinkedList](Data-Structures/Linked-List/DoublyLinkedList.js)
|
||||
* [MergeTwoSortedLinkedLists](Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js)
|
||||
* [ReverseSinglyLinkedList](Data-Structures/Linked-List/ReverseSinglyLinkedList.js)
|
||||
* [SinglyCircularLinkedList](Data-Structures/Linked-List/SinglyCircularLinkedList.js)
|
||||
* [SinglyLinkedList](Data-Structures/Linked-List/SinglyLinkedList.js)
|
||||
@ -88,6 +90,7 @@
|
||||
* [Queue](Data-Structures/Queue/Queue.js)
|
||||
* [QueueUsing2Stacks](Data-Structures/Queue/QueueUsing2Stacks.js)
|
||||
* **Stack**
|
||||
* [EvaluateExpression](Data-Structures/Stack/EvaluateExpression.js)
|
||||
* [Stack](Data-Structures/Stack/Stack.js)
|
||||
* [StackES6](Data-Structures/Stack/StackES6.js)
|
||||
* **Tree**
|
||||
@ -179,6 +182,7 @@
|
||||
* [DecimalExpansion](Maths/DecimalExpansion.js)
|
||||
* [DecimalIsolate](Maths/DecimalIsolate.js)
|
||||
* [DegreeToRadian](Maths/DegreeToRadian.js)
|
||||
* [Determinant](Maths/Determinant.js)
|
||||
* [EuclideanDistance](Maths/EuclideanDistance.js)
|
||||
* [EulerMethod](Maths/EulerMethod.js)
|
||||
* [EulersTotient](Maths/EulersTotient.js)
|
||||
@ -239,6 +243,7 @@
|
||||
* [RadianToDegree](Maths/RadianToDegree.js)
|
||||
* [ReverseNumber](Maths/ReverseNumber.js)
|
||||
* [ReversePolishNotation](Maths/ReversePolishNotation.js)
|
||||
* [RowEchelon](Maths/RowEchelon.js)
|
||||
* [ShorsAlgorithm](Maths/ShorsAlgorithm.js)
|
||||
* [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js)
|
||||
* [SieveOfEratosthenesIntArray](Maths/SieveOfEratosthenesIntArray.js)
|
||||
|
@ -36,7 +36,7 @@ describe('BinaryHeap', () => {
|
||||
it('should handle insertion of duplicate values', () => {
|
||||
// Check if the heap handles duplicate values correctly
|
||||
minHeap.insert(2)
|
||||
console.log(minHeap.heap);
|
||||
console.log(minHeap.heap)
|
||||
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.
|
||||
*/
|
||||
function evaluatePostfixExpression(expression) {
|
||||
const stack = [];
|
||||
const stack = []
|
||||
|
||||
// Helper function to perform an operation and push the result to the stack. Returns success.
|
||||
function performOperation(operator) {
|
||||
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 rightOp = stack.pop() // Right operand is the top of the stack
|
||||
const leftOp = stack.pop() // Left operand is the next item on the stack
|
||||
|
||||
if (leftOp === undefined || rightOp === undefined) {
|
||||
return false; // Invalid expression
|
||||
return false // Invalid expression
|
||||
}
|
||||
switch (operator) {
|
||||
case '+':
|
||||
stack.push(leftOp + rightOp);
|
||||
break;
|
||||
stack.push(leftOp + rightOp)
|
||||
break
|
||||
case '-':
|
||||
stack.push(leftOp - rightOp);
|
||||
break;
|
||||
stack.push(leftOp - rightOp)
|
||||
break
|
||||
case '*':
|
||||
stack.push(leftOp * rightOp);
|
||||
break;
|
||||
stack.push(leftOp * rightOp)
|
||||
break
|
||||
case '/':
|
||||
if (rightOp === 0) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
stack.push(leftOp / rightOp);
|
||||
break;
|
||||
stack.push(leftOp / rightOp)
|
||||
break
|
||||
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) {
|
||||
if (!isNaN(parseFloat(token))) {
|
||||
// If the token is a number, push it to the stack
|
||||
stack.push(parseFloat(token));
|
||||
stack.push(parseFloat(token))
|
||||
} else {
|
||||
// If the token is an operator, perform the operation
|
||||
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', () => {
|
||||
it('should evaluate a valid expression', () => {
|
||||
const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 11
|
||||
const result = evaluatePostfixExpression(expression);
|
||||
expect(result).toBe(11);
|
||||
});
|
||||
const expression = '3 4 * 2 / 5 +' // (3 * 4) / 2 + 5 = 11
|
||||
const result = evaluatePostfixExpression(expression)
|
||||
expect(result).toBe(11)
|
||||
})
|
||||
|
||||
it('should handle division by zero', () => {
|
||||
const expression = '3 0 /'; // Division by zero
|
||||
const result = evaluatePostfixExpression(expression);
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
const expression = '3 0 /' // Division by zero
|
||||
const result = evaluatePostfixExpression(expression)
|
||||
expect(result).toBe(null)
|
||||
})
|
||||
|
||||
it('should handle an invalid expression', () => {
|
||||
const expression = '3 * 4 2 / +'; // Invalid expression
|
||||
const result = evaluatePostfixExpression(expression);
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
|
||||
});
|
||||
const expression = '3 * 4 2 / +' // Invalid expression
|
||||
const result = evaluatePostfixExpression(expression)
|
||||
expect(result).toBe(null)
|
||||
})
|
||||
})
|
||||
|
@ -54,10 +54,7 @@ describe('Determinant', () => {
|
||||
'Square matrix is required.'
|
||||
],
|
||||
[[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)
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user