Guest User

Untitled

a guest
Dec 13th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  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]));
Add Comment
Please, Sign In to add comment