Here is my solution in Javascript. I used codepen so I can test the test values easily. I'll link both that, and the code in js (without the console logs i used for debugging ;) ).
Codepen
const reverseText = () => { let text = document.querySelector("#text").value; let toBeReversed = text; let index = toBeReversed.split("(").length - 1; let start = 0; let end = toBeReversed.length; for(let i = 0; i < index; i++) { let openPar = toBeReversed.indexOf("(", start); let closingPar = toBeReversed.lastIndexOf(")", end); let reversed = toBeReversed.substr(openPar + 1, closingPar - openPar - 1); let result = reversed.split("").reverse(); for(let j = 0; j < result.length; j++) { if(result[j] === "(") result[j] = ")"; else if(result[j] === ")") result[j] = "("; } let final = result.join(""); toBeReversed = toBeReversed.replace(reversed, final); start = openPar + 1; end = closingPar - 1; } index = toBeReversed; document.querySelector(".reversed").innerHTML = toBeReversed; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here is my solution in Javascript. I used codepen so I can test the test values easily. I'll link both that, and the code in js (without the console logs i used for debugging ;) ).
Codepen