Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function largestNumberIndex(numArray) {
- const largestNumberInfos = numArray.reduce(
- function(acc, val, index) {
- if (val > acc[1]) {
- return [index, val];
- }
- return acc;
- },
- [0, numArray[0],]
- );
- return largestNumberInfos[0];
- }
- function orderArray(numArray) {
- const finalResult = numArray.reduce(
- function(acc) {
- const finalArray = acc[0];
- const currentArrayToOrder = acc[1];
- const largestIndex = largestNumberIndex(currentArrayToOrder);
- const numArrayCopy = currentArrayToOrder.slice();
- const removedNumber = numArrayCopy.splice(largestIndex, 1)[0];
- finalArray.push(removedNumber);
- return [finalArray, numArrayCopy];
- },
- [[], numArray]
- );
- return finalResult[0];
- }
- console.log(orderArray([5, 10, 7, 8]));
Add Comment
Please, Sign In to add comment