# JavaScript Challenge - Top Score Sorter

This community-built FAQ covers the “Top Score Sorter” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

## Top Discussions on the JavaScript challenge Top Score Sorter

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply ( ) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply ( ) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like ( ) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Space complexity, O(N); time complexity, O(N^2)? or O(N log N)?

``````function scoreSorter(array, topScore) {
const reverseRange = n => {
const a = [];
let i = 0;
while (a.length < n) {
a.push(n - ++i);
}
return a;
}
const r = reverseRange(array.length)
for (let i = 0; i < array.length - 1; i++) {
r.pop();
for (let j of r) {
if (array[i] < array[j]) {
[array[i], array[j]] = [array[j], array[i]];
}
}
}
return array;
}
``````

Looks like O(N^2) due to nested for-loops. Is ++i in reverseRange a typo?

By no means. It’s what gives us a step.

One knows this is not the sort of logic to use in production code, as boilerplate as it must be. This is just fun.

The inner loop is shrinking with time. How does that factor in?

A simpler reverseRange…

`````` const reverseRange = n => {
const a = new Array(n);
let i = 0;
while (i < n) {
a[i] = n - ++i;
}
return a;
}
``````

Again, we get the step from `++i`. Because it is prefixed, the operation takes place before the rest of the evaluation. In the first iteration we are subtracting 1 from n.

1 Like

I think it is O((N^2)/2) - the average. But we drop the constant of 1/2 so therefore it becomes O(N^2).

1 Like

Any observer would look at this and ask, “Where does the top score factor in?” I would be among them.

1 Like
``````function scoreSorter(array, topScore) {
return array.filter(score => score <= topScore)
.sort((a, b) => b - a);
}

console.log(scoreSorter([1, 2, 3, 9999, 13], 10000));
console.log(scoreSorter([10001, 13, 1, 2, 3], 10000));
console.log(scoreSorter([4, 1, 12, 6, 3, 24], 20));

// Leave this so we can test your code:
module.exports = scoreSorter;
``````

I’m not sure if thats a valid answer, because I use the prebuilt `.sort()` and `.filter()` function - but I like the way `.sort()` can sort numbers without much effort. I will write a self implemented version, too, but for now I’m open for discussion of this “solution”?! my favorite sort algorithm with the complexity O(n) in best case and O(n*n) in worst case

``````function scoreSorter(array, topScore) {
array = array.filter(item => item <= topScore);
for(let i = 0; i < array.length - 1; i++)
for (let j = 0; j < array.length - i - 1; j++)
if (array[j] < array[j+1]) {
const tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
return array
}
``````

Correct, not a valid answer since it does not implement its own sort algorithm as specified in the directions.

Recursively:

``````const scoreSorter = (array, _, currentIndex = 0) => {
//Edge cases
if (array.length === currentIndex) return array;

for (let i = 0; i < array.length; i++) {
if (array[i] < array[i + 1])
[array[i], array[i + 1]] = [array[i + 1], array[i]];
}
return scoreSorter(array, _, currentIndex + 1);
};

console.log(scoreSorter([1, 2, 3, 999], 1000));

// Leave this so we can test your code:
module.exports = scoreSorter;
``````

Space complexity: O(N^2)

1 Like

I struggled a lot with this, and succumbed to using helping methods.

function scoreSorter(array, topScore) { // Write your code here let copy = array.slice() let sorted = [] for(let i = 0; i < array.length; i++) { let max = Math.max(...copy) sorted.push(max) copy.splice(copy.indexOf(max), 1) } return sorted } console.log(scoreSorter([1, 2, 3, 9999, 13], 10000)) // Leave this so we can test your code: module.exports = scoreSorter;
1 Like

I dont know if we can use sort function, i can do it again with a quicksort

``````function scoreSorter(array, topScore) {
array.sort((a,b) => b - a);
return array;
}

console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;
``````
function scoreSorter(array, topScore) { // Write your code here var scambi; var index, quanti, comodo; quanti=topScore; do { scambi=false; for (index=0; index<quanti-1; index++) { if (array[index]<array[index+1]) { comodo=array[index]; array[index]=array[index+1]; array[index+1]=comodo; scambi=true; } } }while (scambi==true); return array; } console.log(scoreSorter([1, 2, 3, 9999, 13], 10000)) // Leave this so we can test your code: module.exports = scoreSorter;

Explicitly stated in the instructions was to implement our own sort method. Let’s see your quicksort.

I finally use a merge sort (i was close to the quicksort, i think i will do later)

``````function merge(left, right) {
let arr = [];
if (left.length < 1) {
return right;
} else if (right.length < 1) {
return left;
} else if (left < right) {
arr.push(left);
return left.slice(0, 1).concat(merge(left.slice(1), right));
} else {
arr.push(right);
return arr.concat(merge(left, right.slice(1)));
}
}

function mergeSort(array) {
let n = array.length;
let middle = Math.floor(n / 2);
return (n <= 1) ? array :
merge(mergeSort(array.slice(0, middle)),
mergeSort(array.slice(middle)));
}

function scoreSorter(array, topScore) {
return mergeSort(array).reverse();
}

console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;
``````
``````function scoreSorter(array, topScore) {
let sorted = [],
eligible = (array || []).filter(v => v <= (topScore || 0));

do {
let topIndex = eligible.findIndex(el => el == Math.max(...eligible));
sorted.push(eligible.splice(topIndex,1))
} while (eligible.length);

return sorted;
}

``````

I also struggled a bit with this one until I finally came up with this pricey algorithm. I think the while loop though conserves resources since it will stop counting down once the right number of scores have been found. I decided to go with this because I don’t know why else they would include the highest possible score as information.

const scoreSorter = (scoresArr, topScore) => { let sortedScores = []; while (sortedScores.length < scoresArr.length) { let arr = scoresArr; for (let i=topScore; i>=0; i--) { for (let j=0; j<arr.length; j++) { if (i === arr[j]) { sortedScores.push(i); } } } } return sortedScores; } // Glory to you and your code console.log(scoreSorter([3, 2, 1, 13, 9999, 345, 193, 237, 394, 2605, 0, 3859, 3749, 5947, 463, 8374, 184], 10000)) // Leave this so we can test your code: module.exports = scoreSorter;
``````function scoreSorter(array, topScore) {
if (array.length < 1)
return [];

const max = array.reduce((max, x) => Math.max(max, x), 0);

return [
...array.filter(score => score == max),
...scoreSorter(array.filter(score => score < max), max)
];
}

console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))

// Leave this so we can test your code:
module.exports = scoreSorter;

``````

function scoreSorter(array, topScore) {

let notSorted = true;

while(notSorted) {
notSorted = false;

``````for(let i = 0; i < array.length - 1; i++) {
if(array[i] < array[i + 1]) {
let temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
notSorted = true;
}
}
``````

}

return array;
}

console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;

``````              function scoreSorter(array, topScore) {

let arr2 = []

array.forEach((value, index) => {
if(value <= topScore){
arr2.push(value);
}

})

const sorty = (a, b) => {
return b - a;

}

return arr2.sort(sorty);

}

console.log(scoreSorter([1, 4, 20000, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;``````