Skip to content

Commit e535816

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] sonarqube: Prefer Math.max() to simplify ternary expressions.
Ternary expressions should be replaced with "Math.min()" or "Math.max()" for simple comparisons typescript:S7766
1 parent c092b27 commit e535816

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger';
33

4-
import { maxSubsetSum } from './max_array_sum';
4+
import { maxSubsetSum, bigIntMax } from './max_array_sum';
55

66
import TEST_CASES from './max_array_sum.testcases.json';
77
import TEST_CASE3 from './max_array_sum.testcase3.json';
@@ -10,6 +10,29 @@ const ALL_TEST_CASES = [...TEST_CASES, TEST_CASE3];
1010

1111
const DECIMAL_RADIX = 10;
1212

13+
describe('bigIntMax', () => {
14+
it('bigIntMax test cases', () => {
15+
expect.assertions(1);
16+
17+
const inputs = [1n, 3n, 2n, 5n, 4n];
18+
const expected = 5n;
19+
20+
const answer = bigIntMax(...inputs);
21+
22+
console.debug(`bigIntMax(${inputs.toString()}) solution found: ${answer}`);
23+
24+
expect(answer).toStrictEqual(expected);
25+
});
26+
27+
it('bigIntMax edge case', () => {
28+
expect.assertions(1);
29+
30+
expect(() => {
31+
bigIntMax();
32+
}).toThrow('bigIntMax requires at least one argument.');
33+
});
34+
});
35+
1336
describe('max_array_sum', () => {
1437
it('maxSubsetSum test cases', () => {
1538
expect.assertions(5);

src/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum-solution-notes.md]]
44
*/
55

6-
const bigIntMax = (...args: bigint[]): bigint =>
7-
args.reduce((m, e) => {
8-
const _e = BigInt(e);
9-
const _m = BigInt(m);
10-
return _e > _m ? _e : _m;
11-
}, BigInt(0));
6+
const bigIntMax = (...args: bigint[]): bigint => {
7+
if (args.length === 0) {
8+
throw new Error('bigIntMax requires at least one argument.');
9+
}
10+
return args.reduce((max, current) => {
11+
if (current > max) {
12+
return current;
13+
}
14+
return max;
15+
}, 0n);
16+
};
1217

1318
function maxSubsetSum(arr: number[]): number {
1419
const arrCopy: bigint[] = arr.map(BigInt);
@@ -36,4 +41,4 @@ function maxSubsetSum(arr: number[]): number {
3641
}
3742

3843
export default { maxSubsetSum };
39-
export { maxSubsetSum };
44+
export { maxSubsetSum, bigIntMax };

0 commit comments

Comments
 (0)