Created
November 1, 2015 17:46
-
-
Save boompig/0e7ce683a4679e13632f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var makePalindrome = function(text) { | |
// take a letter from the beginning and add it to the end | |
var front = true; | |
var start = 0; | |
var end = text.length - 1; | |
while (start < end) { | |
console.log("compared " + text[start] + " and " + text[end]); | |
if (text[start] != text[end]) { | |
if (front) { | |
console.log("inserting " + text[start] + " before index " + (end + 1) + "(after char " + text[end] + ")"); | |
// add character @'start' after character @'end' | |
text = insertChar(text, text[start], end + 1); | |
end++; | |
front = false; | |
} else { | |
console.log("inserting " + text[end] + " before index " + (start) + "(before char " + text[start] + ")"); | |
// add character @'end' before character @'start' | |
text = insertChar(text, text[end], start); | |
front = true; | |
// end gets shifted back | |
end++; | |
} | |
console.log("word is now " + text); | |
} else { | |
start++; | |
end--; | |
} | |
} | |
return text; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment