Skip to content

Commit 228ef0b

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] SonarQube: Prefer .at(…) over [….length - index].
Complex index access patterns should be replaced with ".at()" method javascript:S7755
1 parent 2586c6b commit 228ef0b

File tree

4 files changed

+4
-7
lines changed

4 files changed

+4
-7
lines changed

src/hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
function maxMin(k, arr) {
66
const sortedlist = arr.map((x) => x).sort((a, b) => a - b);
77

8-
let result = sortedlist[sortedlist.length - 1] - sortedlist[0];
8+
let result = sortedlist.at(-1) - sortedlist[0];
99

1010
for (let i = 0; i < sortedlist.length - k + 1; i++) {
1111
const tmin = sortedlist[i];

src/hackerrank/interview_preparation_kit/greedy_algorithms/minimum_absolute_difference_in_an_array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
function minimumAbsoluteDifference(arr) {
66
const sortedNums = arr.map((x) => x).sort((a, b) => b - a);
77

8-
let result = Math.abs(sortedNums[sortedNums.length - 1] - sortedNums[0]);
8+
let result = Math.abs(sortedNums.at(-1) - sortedNums[0]);
99

1010
for (let i = 0; i < sortedNums.length - 1; i++) {
1111
const aValue = sortedNums[i];

src/hackerrank/interview_preparation_kit/stacks_and_queues/balanced_brackets.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ function isBalancedCompute(s) {
1212
for (const letter of s.split('')) {
1313
if (letter in pairs) {
1414
brackets.push(letter);
15-
} else if (
16-
brackets.length > 0 &&
17-
pairs[brackets[brackets.length - 1]] === letter
18-
) {
15+
} else if (brackets.length > 0 && pairs[brackets.at(-1)] === letter) {
1916
brackets.pop();
2017
} else {
2118
return false;

src/projecteuler/problem0007.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function problem0007(_top) {
2323

2424
console.log(`primes count: ${primes.length}`);
2525

26-
const answer = primes[primes.length - 1];
26+
const answer = primes.at(-1);
2727

2828
const cycles = i;
2929
console.log(`${_top} prime number is: ${answer} in ${cycles} cycles`);

0 commit comments

Comments
 (0)