I wrote a simple sort algorithm in JavaScript, but code climate is complaining its complexity being too high (currently sitting on 6 instead of 5, which is code climate wants).
I personally cannot see how it can be further simplified.
Can anyone help? The following algorithm sorts an array of numbers from largest to smallest.
function selectionSort(array) {
let index = 0;
let terminatingIndex = array.length - 1;
let nextElementIndex = null;
while (index < terminatingIndex) {
nextElementIndex = index + 1;
while (nextElementIndex <= terminatingIndex) {
if (array[index] < array[nextElementIndex]) {
[array[index], array[nextElementIndex]] = [array[nextElementIndex], array[index]];
}
nextElementIndex++;
}
index++;
}
console.log(`The sorted array is: ${array}`);
}