mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
npm run style
result
This commit is contained in:
@ -5,15 +5,15 @@
|
|||||||
* @param {number[]} arrayItems
|
* @param {number[]} arrayItems
|
||||||
* @returns number
|
* @returns number
|
||||||
*/
|
*/
|
||||||
export function maxProductOfThree(arrayItems) {
|
export function maxProductOfThree (arrayItems) {
|
||||||
// if size is less than 3, no triplet exists
|
// if size is less than 3, no triplet exists
|
||||||
let n = arrayItems.length
|
const n = arrayItems.length
|
||||||
if (n < 3) throw new Error('Triplet cannot exist with the given array')
|
if (n < 3) throw new Error('Triplet cannot exist with the given array')
|
||||||
let max1 = arrayItems[0],
|
let max1 = arrayItems[0]
|
||||||
max2 = -1,
|
let max2 = -1
|
||||||
max3 = -1,
|
let max3 = -1
|
||||||
min1 = arrayItems[0],
|
let min1 = arrayItems[0]
|
||||||
min2 = -1
|
let min2 = -1
|
||||||
for (let i = 1; i < n; i++) {
|
for (let i = 1; i < n; i++) {
|
||||||
if (arrayItems[i] > max1) {
|
if (arrayItems[i] > max1) {
|
||||||
max3 = max2
|
max3 = max2
|
||||||
@ -32,7 +32,7 @@ export function maxProductOfThree(arrayItems) {
|
|||||||
min2 = arrayItems[i]
|
min2 = arrayItems[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let prod1 = max1 * max2 * max3,
|
const prod1 = max1 * max2 * max3
|
||||||
prod2 = max1 * min1 * min2
|
const prod2 = max1 * min1 * min2
|
||||||
return Math.max(prod1, prod2)
|
return Math.max(prod1, prod2)
|
||||||
}
|
}
|
||||||
|
@ -13,48 +13,48 @@
|
|||||||
* @returns Array with GCD and first and second Bézout coefficients
|
* @returns Array with GCD and first and second Bézout coefficients
|
||||||
*/
|
*/
|
||||||
const extendedEuclideanGCD = (arg1, arg2) => {
|
const extendedEuclideanGCD = (arg1, arg2) => {
|
||||||
if(typeof arg1 != 'number' || typeof arg2 != 'number') throw new TypeError('Not a Number');
|
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') throw new TypeError('Not a Number')
|
||||||
if(arg1 < 1 || arg2 < 1) throw new TypeError('Must be positive numbers');
|
if (arg1 < 1 || arg2 < 1) throw new TypeError('Must be positive numbers')
|
||||||
|
|
||||||
// Make the order of coefficients correct, as the algorithm assumes r0 > r1
|
// Make the order of coefficients correct, as the algorithm assumes r0 > r1
|
||||||
if (arg1 < arg2) {
|
if (arg1 < arg2) {
|
||||||
const res = extendedEuclideanGCD(arg2,arg1)
|
const res = extendedEuclideanGCD(arg2, arg1)
|
||||||
const temp = res[1]
|
const temp = res[1]
|
||||||
res[1] = res[2]
|
res[1] = res[2]
|
||||||
res[2] = temp
|
res[2] = temp
|
||||||
return res;
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point arg1 > arg2
|
// At this point arg1 > arg2
|
||||||
|
|
||||||
// Remainder values
|
// Remainder values
|
||||||
let r0 = arg1
|
let r0 = arg1
|
||||||
let r1 = arg2
|
let r1 = arg2
|
||||||
|
|
||||||
// Coefficient1 values
|
// Coefficient1 values
|
||||||
let s0 = 1
|
let s0 = 1
|
||||||
let s1 = 0
|
let s1 = 0
|
||||||
|
|
||||||
// Coefficient 2 values
|
// Coefficient 2 values
|
||||||
let t0 = 0
|
let t0 = 0
|
||||||
let t1 = 1
|
let t1 = 1
|
||||||
|
|
||||||
while(r1 != 0) {
|
while (r1 != 0) {
|
||||||
const q = Math.floor(r0 / r1);
|
const q = Math.floor(r0 / r1)
|
||||||
|
|
||||||
const r2 = r0 - r1*q;
|
const r2 = r0 - r1 * q
|
||||||
const s2 = s0 - s1*q;
|
const s2 = s0 - s1 * q
|
||||||
const t2 = t0 - t1*q;
|
const t2 = t0 - t1 * q
|
||||||
|
|
||||||
r0 = r1
|
r0 = r1
|
||||||
r1 = r2
|
r1 = r2
|
||||||
s0 = s1
|
s0 = s1
|
||||||
s1 = s2
|
s1 = s2
|
||||||
t0 = t1
|
t0 = t1
|
||||||
t1 = t2
|
t1 = t2
|
||||||
}
|
}
|
||||||
return [r0,s0,t0];
|
return [r0, s0, t0]
|
||||||
}
|
}
|
||||||
|
|
||||||
export { extendedEuclideanGCD };
|
export { extendedEuclideanGCD }
|
||||||
// ex
|
// ex
|
||||||
|
@ -6,11 +6,11 @@ describe('extendedEuclideanGCD', () => {
|
|||||||
expect(extendedEuclideanGCD(46, 240)).toMatchObject([2, 47, -9])
|
expect(extendedEuclideanGCD(46, 240)).toMatchObject([2, 47, -9])
|
||||||
})
|
})
|
||||||
it('should give error on non-positive arguments', () => {
|
it('should give error on non-positive arguments', () => {
|
||||||
expect(() => extendedEuclideanGCD(0,240)).toThrowError(new TypeError('Must be positive numbers'))
|
expect(() => extendedEuclideanGCD(0, 240)).toThrowError(new TypeError('Must be positive numbers'))
|
||||||
expect(() => extendedEuclideanGCD(46,-240)).toThrowError(new TypeError('Must be positive numbers'))
|
expect(() => extendedEuclideanGCD(46, -240)).toThrowError(new TypeError('Must be positive numbers'))
|
||||||
})
|
})
|
||||||
it('should give error on non-numeric arguments', () => {
|
it('should give error on non-numeric arguments', () => {
|
||||||
expect(() => extendedEuclideanGCD('240',46)).toThrowError(new TypeError('Not a Number'));
|
expect(() => extendedEuclideanGCD('240', 46)).toThrowError(new TypeError('Not a Number'))
|
||||||
expect(() => extendedEuclideanGCD([240,46])).toThrowError(new TypeError('Not a Number'));
|
expect(() => extendedEuclideanGCD([240, 46])).toThrowError(new TypeError('Not a Number'))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user