← Back to challenges

Swapping Two by Two

JavaScriptHardregexstrings

Instructions

Write a function that swaps the first pair (1st and 2nd characters) with the second pair (3rd and 4th characters) for every quadruplet substring.

Examples

swapTwo("ABCDEFGH") ➞ "CDABGHEF"

swapTwo("AABBCCDDEEFF") ➞ "BBAADDCCFFEE"

swapTwo("munchkins") ➞ "ncmuinhks"

swapTwo("FFGGHHI") ➞ "GGFFHHI"

Notes

Keep leftover strings in the same order.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.