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

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.

/*helper functions*/ function compareNumbers(a, b) { return b - a; }; const inRange = (array, maxScore) => array.every((element) => element >= 0 && element <= maxScore); /* MAIN FUNCTION */ function scoreSorter(array, maxScore) { let sorted = []; if (inRange(array, maxScore)) { sorted = array.sort(compareNumbers); } else { console.log('Scores must be in range of 1 - 1000. Please check scores for typos.'); } return sorted; }; console.log(scoreSorter([1, 2, 3, 9999, 13], 10000)) // Leave this so we can test your code: module.exports = scoreSorter;

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.

const scoreSorter = (array, topScore) => { const quicksort = (array, leftBound = 0, rightBound = array.length -1) => { if (rightBound > leftBound) { const pivotIndex = partition(array, leftBound, rightBound); quicksort(array, leftBound, pivotIndex - 1); quicksort(array, pivotIndex, rightBound); } return array; } const partition = (array, leftIndex, rightIndex) => { const pivot = array[Math.floor((rightIndex + leftIndex) / 2)]; while (leftIndex <= rightIndex) { while (array[leftIndex] > pivot) { leftIndex++; } while (array[rightIndex] < pivot) { rightIndex--; } if (leftIndex <= rightIndex) { swap(array, leftIndex, rightIndex); leftIndex++; rightIndex--; } } return leftIndex; } const swap = (array, leftIndex, rightIndex) => { const temp = array[leftIndex]; array[leftIndex] = array[rightIndex]; array[rightIndex] = temp; } return quicksort(array); } console.log(scoreSorter([1, 2, 3, 9999, 13], 10000))

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) { 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 } 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 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;

I have done Solution for all 3 sorting techniques taught Bubble, Merge, and Quicksort

function scoreSorter(array, topScore) {
  // Write your code here
  // Bubble Sort
  // 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;

  // Merge Sort
  /*
  const length = array.length;
  if (length === 1) {
    return array;
  }
  const mid = Math.floor(length / 2);
  const leftArray = array.slice(0, mid);
  const rightArray = array.slice(mid, length);

  return merge(scoreSorter(leftArray), scoreSorter(rightArray));
  */

  // Quicksort
  return quicksort(array);
}
// Merge Sort Function
const merge = (leftArray, rightArray) => {
  const sortedArray = [];
  while (leftArray.length > 0 && rightArray.length > 0) {
    if (leftArray[0] > rightArray[0]) {
      sortedArray.push(leftArray[0]);
      leftArray.shift();
    } else {
      sortedArray.push(rightArray[0]);
      rightArray.shift();
    }
  }
  return sortedArray.concat(leftArray).concat(rightArray);
}

// Quicksort Functions
const quicksort = (array, leftBound = 0, rightBound = array.length - 1) => {
  if (leftBound < rightBound) {
    const pivotIndex = partition(array, leftBound, rightBound);
    quicksort(array, leftBound, pivotIndex - 1);
    quicksort(array, pivotIndex, rightBound);
  }
  return array;
}

const partition = (array, leftIndex, rightIndex) => {
  const pivot = array[Math.floor((rightIndex + leftIndex) / 2)];
  
  while (leftIndex <= rightIndex) {
    while (array[leftIndex] > pivot) {
      leftIndex++;
    }
    while (array[rightIndex] < pivot) {
      rightIndex--;
    }
    if (leftIndex <= rightIndex) {
        const temp = array[leftIndex];
        array[leftIndex] = array[rightIndex];
        array[rightIndex] = temp;
        leftIndex++;
        rightIndex--;
    }
  }
  return leftIndex;
}


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