JSFiddle - React, Tailwind, and code Playground

by Alireza Safian

JavaScript

function meanderingArray(unsorted) {
    // Write your code here
    const result = [];
    const temp = [...unsorted]
    
    for(let i =0; i < unsorted.length ; i++){
      const max = Math.max(...temp);      
      const maxIndex = temp.indexOf(max);
      console.log(maxIndex)
      temp.splice(maxIndex, 1);
      result.push(max);
        if(temp.length == 0){
          break;
      }
      
      const min = Math.min(...temp);
      result.push(min);
      const minIndex = temp.indexOf(min);      
      temp.splice(minIndex, 1);
    }
    
    return result;

}


console.log(meanderingArray([5, 2, 7, 8, -2, 25, 25]));