From 22ce7603e4391eec0a02f9fc3d1c1f91e3c62764 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Thu, 21 Oct 2021 22:59:56 +0530 Subject: [PATCH] fix: standard style issues --- Conversions/BinaryToHex.js | 2 +- Conversions/DecimalToRoman.js | 2 +- Conversions/HexToDecimal.js | 28 +++++++++----------------- Data-Structures/Queue/CircularQueue.js | 4 +--- Maths/ExtendedEuclideanGCD.js | 8 ++++---- Project-Euler/Problem007.js | 26 ------------------------ Sorts/PancakeSort.js | 6 +++--- 7 files changed, 20 insertions(+), 56 deletions(-) delete mode 100644 Project-Euler/Problem007.js diff --git a/Conversions/BinaryToHex.js b/Conversions/BinaryToHex.js index 60c64bde4..71b32cc2f 100644 --- a/Conversions/BinaryToHex.js +++ b/Conversions/BinaryToHex.js @@ -1,5 +1,5 @@ const pad = (num, padlen) => { - var pad = new Array(1 + padlen).join(0) + const pad = new Array(1 + padlen).join(0) return (pad + num).slice(-pad.length) } diff --git a/Conversions/DecimalToRoman.js b/Conversions/DecimalToRoman.js index 99c23567e..62126de37 100644 --- a/Conversions/DecimalToRoman.js +++ b/Conversions/DecimalToRoman.js @@ -40,7 +40,7 @@ const orders = [ function decimalToRoman (num) { let roman = '' - for (var symbol of orders) { + for (const symbol of orders) { while (num >= values[symbol]) { roman += symbol num -= values[symbol] diff --git a/Conversions/HexToDecimal.js b/Conversions/HexToDecimal.js index 76d7a54cf..9623d09a2 100644 --- a/Conversions/HexToDecimal.js +++ b/Conversions/HexToDecimal.js @@ -1,18 +1,16 @@ function hexToInt (hexNum) { const numArr = hexNum.split('') // converts number to array - numArr.map((item, index) => { - if (!(item > 0)) { - switch (item) { - case 'A': return (numArr[index] = 10) - case 'B': return (numArr[index] = 11) - case 'C': return (numArr[index] = 12) - case 'D': return (numArr[index] = 13) - case 'E': return (numArr[index] = 14) - case 'F': return (numArr[index] = 15) - } - } else numArr[index] = parseInt(item) + return numArr.map((item, index) => { + switch (item) { + case 'A': return 10 + case 'B': return 11 + case 'C': return 12 + case 'D': return 13 + case 'E': return 14 + case 'F': return 15 + default: return parseInt(item) + } }) - return numArr // returns an array only with integer numbers } function hexToDecimal (hexNum) { @@ -23,9 +21,3 @@ function hexToDecimal (hexNum) { } export { hexToInt, hexToDecimal } - -// > hexToDecimal('5DE9A')) -// 384666 - -// > hexToDecimal('3D')) -// 61 diff --git a/Data-Structures/Queue/CircularQueue.js b/Data-Structures/Queue/CircularQueue.js index 45e0ce3f2..a9bbc3738 100644 --- a/Data-Structures/Queue/CircularQueue.js +++ b/Data-Structures/Queue/CircularQueue.js @@ -33,9 +33,7 @@ class CircularQueue { } const y = this.queue[this.front] this.queue[this.front] = '*' - if (this.checkSingleelement()) { - - } else { + if (!this.checkSingleelement()) { if (this.front === this.maxLength) this.front = 1 else { this.front += 1 diff --git a/Maths/ExtendedEuclideanGCD.js b/Maths/ExtendedEuclideanGCD.js index a5623b18c..c666a9989 100644 --- a/Maths/ExtendedEuclideanGCD.js +++ b/Maths/ExtendedEuclideanGCD.js @@ -2,19 +2,19 @@ * Problem statement and explanation: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm * * This algorithm plays an important role for modular arithmetic, and by extension for cyptography algorithms - * + * * Basic explanation: * The Extended Euclidean algorithm is a modification of the standard Euclidean GCD algorithm. * It allows to calculate coefficients x and y for the equation: * ax + by = gcd(a,b) - * + * * This is called Bézout's identity and the coefficients are called Bézout coefficients - * + * * The algorithm uses the Euclidean method of getting remainder: * r_i+1 = r_i-1 - qi*ri * and applies it to series s and t (with same quotient q at each stage) * When r_n reaches 0, the value r_n-1 gives the gcd, and s_n-1 and t_n-1 give the coefficients - * + * * This implementation uses an iterative approach to calculate the values */ diff --git a/Project-Euler/Problem007.js b/Project-Euler/Problem007.js deleted file mode 100644 index 2bb4eeef4..000000000 --- a/Project-Euler/Problem007.js +++ /dev/null @@ -1,26 +0,0 @@ -// https://projecteuler.net/problem=7 -// My approach does not use the Sieve of Eratosthenes but that is another common way to approach this problem. Sieve of Atkin is another possibility as well. - -export const calculatePrime = (num = 10001, primes = [2, 3, 5, 7, 11, 13]) => { - // Calculate each next prime by checking each number to see what it's divisible by - let count = primes.length // count number of primes calculated - let current = primes[count - 1] + 1 // current number being assessed if prime - while (count < num) { // repeat while we haven't reached goal number of primes - // go through each prime and see if divisible by the previous primes - let prime = false - primes.some((n, i) => { - if (current % n === 0) { - return true - } - if (i === count - 1) { - prime = true - } - }) - if (prime) { // if prime, add to prime list and increment count - primes.push(current) - count += 1 - } - current += 1 - } - return primes[num - 1] -} diff --git a/Sorts/PancakeSort.js b/Sorts/PancakeSort.js index 285127c99..d6e1a3d55 100644 --- a/Sorts/PancakeSort.js +++ b/Sorts/PancakeSort.js @@ -26,7 +26,7 @@ * @param {number} endIndex The end of the subarray * @returns The flipped array */ -export function flipArray(array, startIndex, endIndex) { +export function flipArray (array, startIndex, endIndex) { while (startIndex < endIndex) { // swap front and back of the subarray const temp = array[startIndex] @@ -49,7 +49,7 @@ export function flipArray(array, startIndex, endIndex) { * @param {*} endIndex The end of the subarray * @returns The index of the maximum number */ -export function findMax(array, startIndex, endIndex) { +export function findMax (array, startIndex, endIndex) { let maxIndex = 0 for (let i = startIndex; i <= endIndex; i++) { if (array[i] > array[maxIndex]) maxIndex = i @@ -67,7 +67,7 @@ export function findMax(array, startIndex, endIndex) { * @param {number[]} array The array to sort * @returns The sorted array */ -export function pancakeSort(array) { +export function pancakeSort (array) { for (let subarraySize = array.length; subarraySize > 1; subarraySize--) { const maximumIndex = findMax(array, 0, subarraySize - 1)