logo CodeStepByStep logo

swapPairs

Language/Type: JavaScript string return

Write a function named swapPairs that accepts a string as a parameter and returns a new string such that each pair of adjacent letters will be reversed. If the string has an odd number of letters, the last letter is unchanged. For example, if a string variable s stores "example", the call of swapPairs(s) should return "xemalpe". If s had been "hello there", the call would produce "ehll ohtree".

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.