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 Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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”?!
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;
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;
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) {
// Write your code here
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;