Skip to content

Instantly share code, notes, and snippets.

@boompig
Created November 1, 2015 17:46
Show Gist options
  • Save boompig/0e7ce683a4679e13632f to your computer and use it in GitHub Desktop.
Save boompig/0e7ce683a4679e13632f to your computer and use it in GitHub Desktop.
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