diff --git a/Maths/MobiusFunction.js b/Maths/MobiusFunction.js index bd268b8bbd..4239d6ab31 100644 --- a/Maths/MobiusFunction.js +++ b/Maths/MobiusFunction.js @@ -28,6 +28,6 @@ export const mobiusFunction = (number) => { return primeFactorsArray.length !== new Set(primeFactorsArray).size ? 0 : primeFactorsArray.length % 2 === 0 - ? 1 - : -1 + ? 1 + : -1 } diff --git a/Maths/RowEchelon.js b/Maths/RowEchelon.js index c773bb80a9..cd0db4fc73 100644 --- a/Maths/RowEchelon.js +++ b/Maths/RowEchelon.js @@ -117,7 +117,7 @@ const rowEchelon = (matrix) => { let result = matrix // Iterate through the rows (i) and columns (j) of the matrix. - for (let i = 0, j = 0; i < numRows && j < numCols; ) { + for (let i = 0, j = 0; i < numRows && j < numCols;) { // If the current column has all zero elements below the current row, // move to the next column. if (!checkNonZero(i, j, result)) { diff --git a/Project-Euler/Problem013.js b/Project-Euler/Problem013.js index 8748a8ee17..a8b0418058 100644 --- a/Project-Euler/Problem013.js +++ b/Project-Euler/Problem013.js @@ -13,7 +13,7 @@ export function largeSum(bignum) { let num = 0 while (pos--) { - for (let i = nums.length; i--; ) { + for (let i = nums.length; i--;) { num += +nums[i].charAt(pos) } ret = (num % 10) + ret diff --git a/String/GenerateGUID.js b/String/GenerateGUID.js index 1583920261..eb8d224cd0 100644 --- a/String/GenerateGUID.js +++ b/String/GenerateGUID.js @@ -8,7 +8,7 @@ export const Guid = () => { const pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' let currentDateMilliseconds = new Date().getTime() return pattern.replace(/[xy]/g, (currentChar) => { - const randomChar = (currentDateMilliseconds + Math.random() * 16) % 16 | 0 + const randomChar = ((currentDateMilliseconds + Math.random() * 16) % 16) | 0 currentDateMilliseconds = Math.floor(currentDateMilliseconds / 16) return ( currentChar === 'x' ? randomChar : (randomChar & 0x7) | 0x8 diff --git a/factorial.js b/factorial.js new file mode 100644 index 0000000000..da06e19766 --- /dev/null +++ b/factorial.js @@ -0,0 +1,16 @@ +// Function to calculate factorial + +function factorial(n) { + if (!Number.isInteger(n) || n < 0) { + throw new Error('Enter a non-negative integer') + } + + if (n === 0 || n === 1) { + return 1 + } + + return n * factorial(n - 1) +} + +// Example usage +console.log(factorial(5)) // Output: 120