Just shuffle the characters and see if it reversed the string! We'll get the reverse eventually.... (prepare to wait a long time for strings longer than 8 characters)
function reverseString(string, maxIterations = 10000, verbose = false) { const originalString = string.split(""); let shuffledString = string.split(""); let isReverse; let iteration = 1; do { for (let i = 0; i < shuffledString.length; i++) { let j = Math.floor(Math.random() * (string.length - i) + i); if (i != j) { shuffledString[i] = shuffledString[i] + shuffledString[j]; shuffledString[j] = shuffledString[i].replace(shuffledString[j], ""); shuffledString[i] = shuffledString[i].replace(shuffledString[j], ""); } } if (verbose) console.log("Candidate string: ", shuffledString.join("")); isReverse = true; for (let i = 0; i < shuffledString.length; i++) { isReverse = isReverse && originalString[i] == shuffledString[shuffledString.length - 1 - i]; } iteration++; } while (!(isReverse || iteration > maxIterations)); if (isReverse) { return shuffledString.join(""); } else { return false; } }
Perfect!
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.
Just shuffle the characters and see if it reversed the string! We'll get the reverse eventually.... (prepare to wait a long time for strings longer than 8 characters)
Perfect!