Skip to main content
added an explanation
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (ES6), 67 bytes

f=([c,...a])=>c?c+f([...a.splice(0,a.indexOf(c)).reverse(),...a]):a

Try it online!

How?

a.splice(0, n) removes and returns the first \$n\$ entries in a[] if \$n\ge1\$ or removes nothing and returns an empty array if \$n<1\$. If c is not found in a[], a.indexOf(c) returns \$-1\$ and we just pass a[] unchanged to the next iteration.

JavaScript (ES6), 67 bytes

f=([c,...a])=>c?c+f([...a.splice(0,a.indexOf(c)).reverse(),...a]):a

Try it online!

JavaScript (ES6), 67 bytes

f=([c,...a])=>c?c+f([...a.splice(0,a.indexOf(c)).reverse(),...a]):a

Try it online!

How?

a.splice(0, n) removes and returns the first \$n\$ entries in a[] if \$n\ge1\$ or removes nothing and returns an empty array if \$n<1\$. If c is not found in a[], a.indexOf(c) returns \$-1\$ and we just pass a[] unchanged to the next iteration.

saved 17 bytes
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (ES6), 8467 bytes

f=([c,...a])=>c?c+f(~(i=a.indexOf(c))?[...a.slicesplice(0,ia.indexOf(c)).reverse(),...a.slice(i)]:aa]):a

Try it online!Try it online!

JavaScript (ES6), 84 bytes

f=([c,...a])=>c?c+f(~(i=a.indexOf(c))?[...a.slice(0,i).reverse(),...a.slice(i)]:a):a

Try it online!

JavaScript (ES6), 67 bytes

f=([c,...a])=>c?c+f([...a.splice(0,a.indexOf(c)).reverse(),...a]):a

Try it online!

Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (ES6), 84 bytes

f=([c,...a])=>c?c+f(~(i=a.indexOf(c))?[...a.slice(0,i).reverse(),...a.slice(i)]:a):a

Try it online!