JavaScript Challenge - Top Score Sorter

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?

function scoreSorter(array, topScore) { let sorted = false; while(!sorted){ sorted = true; for(let i = 0; i < array.length-1; i++){ if(array[i] > topScore) array.splice(i, 1); if(array[i] < array[i+1]){ [array[i], array[i+1]] = [array[i+1], array[i]]; sorted = false; } } } 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 const mergeSort = startArr => { const length = startArr.length; if (length === 1) { return startArr; } const mid = Math.floor(length / 2); const leftArr = startArr.slice(0, mid); const rightArr = startArr.slice(mid, length); return merge(mergeSort(leftArr), mergeSort(rightArr)); }; const merge = (leftArr, rightArr) => { const sortedArr = []; while (leftArr.length > 0 && rightArr.length > 0) { leftArr[0] > rightArr[0] ? sortedArr.push(leftArr.shift()) : sortedArr.push(rightArr.shift()); } return sortedArr.concat(leftArr).concat(rightArr); }; // sort the array and save the array as the result variable let result = mergeSort(array); // filter the results by looking at the topScore result = result.filter(item => item <= topScore); // return the result return result; } console.log(scoreSorter([1, 2, 3, 9999, 13], 10000)) module.exports = scoreSorter;

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?

function scoreSorter(array, topScore) { // Filter the input array to contain valid Scores only. A valid score <= topScore. let validScores = array.filter((score)=> score <= topScore && score >= 0); // console.log(validScores); let swapping = true; while(swapping){ swapping = false; for (let i = 0; i < validScores.length -1; i++){ if (validScores[i] < validScores[i+1]){ swap(validScores, i, i+1); swapping = true; } } } return validScores; } const swap = (array, indexOne, indexTwo) => { let temp = array[indexOne]; array[indexOne] = array[indexTwo]; array[indexTwo] = temp; } console.log(scoreSorter([1, 2, 3, 9999, 13], 10000)) // Leave this so we can test your code: module.exports = scoreSorter;

My solution passed all the tests without any filtering and using topScore