daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function largestNumberIndex(numArray) {
  2.   const largestNumberInfos = numArray.reduce(
  3.     function(acc, val, index) {
  4.       if (val > acc[1]) {
  5.         return [index, val];
  6.       }
  7.       return acc;
  8.     },
  9.     [0, numArray[0],]
  10.   );
  11.   return largestNumberInfos[0];
  12. }
  13.  
  14. function orderArray(numArray) {
  15.   const finalResult = numArray.reduce(
  16.     function(acc) {
  17.       const finalArray = acc[0];
  18.       const currentArrayToOrder = acc[1];
  19.       const largestIndex = largestNumberIndex(currentArrayToOrder);
  20.       const numArrayCopy = currentArrayToOrder.slice();
  21.       const removedNumber = numArrayCopy.splice(largestIndex, 1)[0];
  22.       finalArray.push(removedNumber);
  23.  
  24.       return [finalArray, numArrayCopy];
  25.     },
  26.     [[], numArray]
  27.   );
  28.   return finalResult[0];
  29. }
  30.  
  31. console.log(orderArray([5, 10, 7, 8]));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top