Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr){
  2.     let sorted = arr.sort(compareNumbers);
  3.     let final = []
  4.  
  5.     function compareNumbers(a, b)
  6.     {
  7.         return a - b;
  8.     }
  9.  
  10.     const sortLent = sorted.length;
  11.  
  12.     while(final.length < sortLent){
  13.         if(sorted.length > 1){
  14.             final.push(sorted[sorted.length - 1]);
  15.             final.push(sorted[0]);
  16.  
  17.             sorted.pop(); //Remoe first element
  18.             sorted.shift(); //Remoe last one
  19.         } else {
  20.             final.push(sorted[0]);
  21.         }
  22.     }
  23.    
  24.     console.log(final.join(' '));
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement