A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Longest Palindromic Substring in Javascript page! Here, you'll find the source code for this program as well as a description of how the program works.
const [, , input] = process.argv;
const getLongestPalindromic = (string) => {
if (!string) return;
let longestPal = '';
for (let i = 1; i < string.length; i++) {
for (let j = 0; j < string.length - i; j++) {
let possiblePal = string.substring(j, j + i + 1).toLowerCase();
if (
possiblePal === [...possiblePal].reverse().join('') &&
possiblePal.length > longestPal.length
)
longestPal = possiblePal;
}
}
return longestPal;
};
console.log(
getLongestPalindromic(input) ||
'Usage: please provide a string that contains at least one palindrome'
);
Longest Palindromic Substring in Javascript was written by:
If you see anything you'd like to change or update, please consider contributing.
Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 26 2020 13:59:30. The solution was first committed on Oct 17 2020 22:08:50. As a result, documentation below may be outdated.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.