Advertisement
Btwonu

Both

Jun 29th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. function filterArray(array) {
  4.   let finalArray = [];
  5.  
  6.   while (array.length > 0) {
  7.       let biggestNumber = Math.max(...array);
  8.       let smallestNumber;
  9.  
  10.       if (array.length > 1) {
  11.           smallestNumber = Math.min(...array);
  12.           finalArray.push(biggestNumber, smallestNumber);
  13.       } else {
  14.           finalArray.push(biggestNumber);
  15.       }
  16.  
  17.       array = array.filter(a => a != biggestNumber);
  18.       array = array.filter(a => a != smallestNumber);
  19.  
  20.   }
  21.  
  22.   console.log(finalArray.join(' '));
  23. }
  24.  
  25. let input = [94, 1, 21, 3, 52, 69, 63, 31, 2, 18, 94];
  26.  
  27. let result = filterArray(input);
  28. console.log(result);
  29.  
  30. function sorting(arr) {
  31.   let highLowArr = [];
  32.   let descendingArr = arr.sort((a, b) => b - a);
  33.  
  34.   let smallest = descendingArr.pop();
  35.   let biggest = descendingArr.shift();
  36.  
  37.   while (smallest !== undefined || biggest !== undefined) {
  38.     //Add to sorted array
  39.     if (biggest !== undefined) {
  40.       highLowArr.push(biggest);
  41.     }
  42.    
  43.     if (smallest !== undefined) {
  44.       highLowArr.push(smallest);
  45.     }
  46.  
  47.     //Get new values from old arr
  48.     smallest = descendingArr.pop();
  49.     biggest = descendingArr.shift();
  50.   }
  51.   //Output
  52.   console.log(highLowArr.join(' '));
  53. }
  54.  
  55. let input2 = [94, 1, 21, 3, 52, 69, 63, 31, 2, 18, 94];
  56.  
  57. let result2 = sorting(input2);
  58. console.log(result2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement