function scoreSorter(array, topScore) {
for (let i=0; i<array.length; i++) {
for (let j=0; j<array.length; j++){
if (array[i] > array[j]) {
[array[i], array[j]] = [array[j], array[i]];
}
}
}
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
let sortedArray = []
// let decimal = []
for(let j = 0; j < array.length; j++){
if(array[j] > topScore || array[j] < 0){
return "ERROR: not valid score"
}
// this accounts for multiples of number(not sure why/how it has to go first)
if (sortedArray.includes(array[j])){
sortedArray.splice(array[j],0,array[j])
}
sortedArray[array[j]] = array[j]
// this accounts for nonintegers
if(!Number.isInteger(array[j]) ){
sortedArray.splice(Math.floor(array[j]),0,array[j])
}
}
// this filters out underfined elements and reverses array
return sortedArray.filter(num => typeof num != undefined).reverse()
}
console.log(scoreSorter([1, 2,11111, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;
function scoreSorter(array, topScore) {
// Write your code here
let swapping = true;
let newArray=topScore ? array.filter(item=>item<=topScore):array;
while(swapping){
swapping=false;
for(let i = 0; i < newArray.length-1; i++){
if(newArray[i] < newArray[i+1]){
const temp=newArray[i];
newArray[i]=newArray[i + 1];
newArray[i+1]=temp;
swapping=true;
}
}
}
return newArray;
}
console.log(scoreSorter([13, 4, 9, 9999, 1],10000))
// Leave this so we can test your code:
module.exports = scoreSorter;
function quickSort(arr){
if(arr.length <= 1){
return arr;
}
const pivot = arr[arr.length - 1];
const leftArr = [];
const rightArr = [];
for(let i=0; i < arr.length-1;i++){
if(arr[i] < pivot){
leftArr.push(arr[i]);
}
else{
rightArr.push(arr[i])
}
}
return [...quickSort(leftArr) ,pivot,...quickSort(rightArr)];
}
function scoreSorter(array, topScore) {
return quickSort(array).reverse();
}
console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;
Top Score Sorter Challenge
My solution.
function scoreSorter(array, topScore) {
// Write your code here
for (let i = array.length; i > 0; i–) {
for (let j = 0; j < i; j++) {
if (array[j] < array[j + 1]) {
topScore = array[j];
array[j] = array[j + 1];
array[j + 1] = topScore;
}
}
}
return array;
}
console.log(scoreSorter([1, 2, 3, 9999, 13], 10000));
How’d I do?
Hope this code helps. Time complexity O(n * log n). Please let me know if you have any questions.
function scoreSorter(array, topScore) {
// Write your code here
for(let i = 0; i < array.length; i++){
for(let j = 0; j < array.length - i -1; j++){
if(array[j+1] > array[j]){
[array[j+1], array[j]] = [array[j], array[j+1]]
}
}
}
return array;
}
console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;
Figured the topScore variable was meant as a way to validate the inputs. All scores should be <= to the provided TopScores.
First, filter out the invalid inputs from the list. Then start the sorting proces using bubbleSort. score > 0 also filteres out ‘string’ values or other invalid inputs.
Figured this would save some time, compared to sorting all values first, and then popping off the values > Topscore from the sorted list.
O(n^2) for the nested for loop in the while loop?
My solution passed all the tests without any filtering and using topScore
my simple solution which passes the tests:
function scoreSorter(array, topScore) {
return array.sort((a, b) => b - a);
}
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 i = array.length;
if ( !i ) return array
if ( topScore === 0 ) {
if ( array.includes(0) )
return [0]
else
return []
}
if ( !topScore )
return 'Error: invalid topScore argument'
let copy = [...array]
while ( i-- ) {
let j = i
while ( j-- ) {
if ( copy[j] < copy[i] ) {
const p = copy[j]
copy[j] = copy[i]
copy[i] = p
}
}
}
return copy.filter( e => e <= topScore )
}
Precisely my thought. The second part is useless. The top score doesn’t make sense when it is going to be the max value.
My code is:
function scoreSorter(scores, highestScore) {
const scoreCounts = new Array(highestScore + 1).fill(0); // Array to count scores
// Count the occurrences of each score
for (let i = 0; i < scores.length; i++) {
const score = scores[i];
scoreCounts[score]++;
}
const sortedScores = ; // Array to store sorted scores
// Iterate through the scoreCounts array in reverse order to get scores in descending order
for (let i = highestScore; i >= 0; i–) {
const count = scoreCounts[i];
// Add the score to the sortedScores array count number of times
for (let j = 0; j < count; j++) {
sortedScores.push(i);
}
}
return sortedScores;
}
console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;
Just finished sorting algorithms, so here’s my quicksort. No array methods!
Runtime O(N log(N)). Space complexity is O(N), sorted in place.
I seen this challenge yesterday and was revising on The Bubble Sort Algorithm Today, so I decide to create a solution using a Descending Order Bubble Sort. I know this is not the most efficient sorting algorithm but seen this as a good chance to practice using it. As other who have also commented here I couldn’t understand how or why there is a Topscore variable and what they expected us to do it with.
Would appreciate any feedback on improving this using Bubble Sort. I will also do this challenge again using quicksort and merge sort Algorithms when I revise them later in the week.
function scoreSorter(array, topScore) {
let keepSwapping = true;
while (keepSwapping) {
keepSwapping = false;
for (let i = 0; i < array.length - 1; i++) {
if (array[i] < array[i + 1]) {
let temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
keepSwapping = true;
}
}
}
return array;
}
console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;
hello guys this is my solution of the Top Score Sorted
the sorted array is a O(n^2) so it could see the change each time to sort the array
function scoreSorter(array, topScore) {
let swapping = true;
while (swapping) {
swapping = false;
for (let i = 0; i < array.length - 1; i++) {
if (array[i] < array[i + 1]) {
const temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapping = true;
}
}
}
return array
}
function scoreSorter(array, topScore) {
// Write your code here
let swapping=true;
while (swapping){
swapping=false;
for(let i=0; i < array.length-1; i++){
if(array[i] < array[i+1]){
swap(array, i, i+1);
swapping=true;
}
}
}
return array;
}
const swap = (arr, index1, index2) =>{
let temp= arr[index2];
arr[index2]=arr[index1];
arr[index1]=temp;
}
console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))
// Leave this so we can test your code:
module.exports = scoreSorter;