Skip to main content
Commonmark migration
Source Link

#JavaScript (ES6), 50 bytes

JavaScript (ES6), 50 bytes

f=a=>(b=[...a]).some((_,i)=>a[i]-->=a[i+1])?f(a):b 

Explanation:

This is a recursive solution, which first clones the array, then decreases all values up until an element is greater or equal to the next element in the array.

The function calls itself as long as any elements are out of order. When the elements are finally sorted, the clone is returned. (We can't return the array itself, because the some() method would have decremented all its elements, making them all off by -1.)

Test cases:

f=a=>(b=[...a]).some((_,i)=>a[i]-->=a[i+1])?f(a):b console.log(f([10,5,7,6,1])+''); console.log(f([1,1,1,1,1,1,1,1,1])+''); console.log(f([5,7,11,6,16,2,9,16,6,16])+''); console.log(f([19])+''); console.log(f([-8,17,9,7])+''); console.log(f([1,2,3,4,5,6,7])+'');

#JavaScript (ES6), 50 bytes

f=a=>(b=[...a]).some((_,i)=>a[i]-->=a[i+1])?f(a):b 

Explanation:

This is a recursive solution, which first clones the array, then decreases all values up until an element is greater or equal to the next element in the array.

The function calls itself as long as any elements are out of order. When the elements are finally sorted, the clone is returned. (We can't return the array itself, because the some() method would have decremented all its elements, making them all off by -1.)

Test cases:

f=a=>(b=[...a]).some((_,i)=>a[i]-->=a[i+1])?f(a):b console.log(f([10,5,7,6,1])+''); console.log(f([1,1,1,1,1,1,1,1,1])+''); console.log(f([5,7,11,6,16,2,9,16,6,16])+''); console.log(f([19])+''); console.log(f([-8,17,9,7])+''); console.log(f([1,2,3,4,5,6,7])+'');

JavaScript (ES6), 50 bytes

f=a=>(b=[...a]).some((_,i)=>a[i]-->=a[i+1])?f(a):b 

Explanation:

This is a recursive solution, which first clones the array, then decreases all values up until an element is greater or equal to the next element in the array.

The function calls itself as long as any elements are out of order. When the elements are finally sorted, the clone is returned. (We can't return the array itself, because the some() method would have decremented all its elements, making them all off by -1.)

Test cases:

f=a=>(b=[...a]).some((_,i)=>a[i]-->=a[i+1])?f(a):b console.log(f([10,5,7,6,1])+''); console.log(f([1,1,1,1,1,1,1,1,1])+''); console.log(f([5,7,11,6,16,2,9,16,6,16])+''); console.log(f([19])+''); console.log(f([-8,17,9,7])+''); console.log(f([1,2,3,4,5,6,7])+'');

deleted 4 characters in body
Source Link
Rick Hitchcock
  • 2.6k
  • 12
  • 18

#JavaScript (ES6), 5250 bytes

f=a=>(b=[...a],a).some((_,i)=>a[i]-->=a[i+1])?f(a):b) 

Explanation:

This is a recursive solution, which first clones the array, then decreases all values up until an element is greater or equal to the next element in the array.

The function calls itself as long as any elements are out of order. When the elements are finally sorted, the clone is returned. (We can't return the array itself, because the some() method would have decremented all its elements, making them all off by -1.)

Test cases:

f=a=>(b=[...a],a).some((_,i)=>a[i]-->=a[i+1])?f(a):b) console.log(f([10,5,7,6,1])+''); console.log(f([1,1,1,1,1,1,1,1,1])+''); console.log(f([5,7,11,6,16,2,9,16,6,16])+''); console.log(f([19])+''); console.log(f([-8,17,9,7])+''); console.log(f([1,2,3,4,5,6,7])+'');

#JavaScript (ES6), 52 bytes

f=a=>(b=[...a],a.some((_,i)=>a[i]-->=a[i+1])?f(a):b) 

Explanation:

This is a recursive solution, which first clones the array, then decreases all values up until an element is greater or equal to the next element in the array.

The function calls itself as long as any elements are out of order. When the elements are finally sorted, the clone is returned. (We can't return the array itself, because the some() method would have decremented all its elements, making them all off by -1.)

Test cases:

f=a=>(b=[...a],a.some((_,i)=>a[i]-->=a[i+1])?f(a):b) console.log(f([10,5,7,6,1])+''); console.log(f([1,1,1,1,1,1,1,1,1])+''); console.log(f([5,7,11,6,16,2,9,16,6,16])+''); console.log(f([-8,17,9,7])+''); console.log(f([1,2,3,4,5,6,7])+'');

#JavaScript (ES6), 50 bytes

f=a=>(b=[...a]).some((_,i)=>a[i]-->=a[i+1])?f(a):b 

Explanation:

This is a recursive solution, which first clones the array, then decreases all values up until an element is greater or equal to the next element in the array.

The function calls itself as long as any elements are out of order. When the elements are finally sorted, the clone is returned. (We can't return the array itself, because the some() method would have decremented all its elements, making them all off by -1.)

Test cases:

f=a=>(b=[...a]).some((_,i)=>a[i]-->=a[i+1])?f(a):b console.log(f([10,5,7,6,1])+''); console.log(f([1,1,1,1,1,1,1,1,1])+''); console.log(f([5,7,11,6,16,2,9,16,6,16])+''); console.log(f([19])+''); console.log(f([-8,17,9,7])+''); console.log(f([1,2,3,4,5,6,7])+'');

Source Link
Rick Hitchcock
  • 2.6k
  • 12
  • 18

#JavaScript (ES6), 52 bytes

f=a=>(b=[...a],a.some((_,i)=>a[i]-->=a[i+1])?f(a):b) 

Explanation:

This is a recursive solution, which first clones the array, then decreases all values up until an element is greater or equal to the next element in the array.

The function calls itself as long as any elements are out of order. When the elements are finally sorted, the clone is returned. (We can't return the array itself, because the some() method would have decremented all its elements, making them all off by -1.)

Test cases:

f=a=>(b=[...a],a.some((_,i)=>a[i]-->=a[i+1])?f(a):b) console.log(f([10,5,7,6,1])+''); console.log(f([1,1,1,1,1,1,1,1,1])+''); console.log(f([5,7,11,6,16,2,9,16,6,16])+''); console.log(f([-8,17,9,7])+''); console.log(f([1,2,3,4,5,6,7])+'');