Skip to content

Instantly share code, notes, and snippets.

@SolidZORO
Last active March 18, 2018 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SolidZORO/d30fdac96fd00af1be9b77e2c64ab9fa to your computer and use it in GitHub Desktop.
Save SolidZORO/d30fdac96fd00af1be9b77e2c64ab9fa to your computer and use it in GitHub Desktop.
reverse char in string
const source = '(a,(b,(c,(d,(e,(f))))))';
const sourceList = source.split('');
const charList = [];
const keyList = [];
// Find Char & Key
sourceList.map((char, i) => {
if (/[a-z]/i.test(char)) {
charList.push(char);
keyList.push(i);
}
});
// Replace sourceList String
keyList.map((key, i) => {
sourceList[key] = charList.reverse()[i];
});
// (a,(b,(c,(d,(e,(f))))))
console.log(source);
// (f,(b,(d,(d,(b,(f))))))
console.log(sourceList.join(''));
// @see https://www.v2ex.com/t/438930
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment