2022-02-20 14:07:44 -05:00
|
|
|
const palindromes = function (string) {
|
2024-03-02 13:57:16 +00:00
|
|
|
// Since we only consider letters and numbers, create a variable containing all valid characters
|
2024-03-03 01:44:34 +00:00
|
|
|
const alphanumerical = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
2024-03-02 13:57:16 +00:00
|
|
|
|
|
|
|
|
// Convert to lowercase, split to array of individual characters, filter only valid characters, then rejoin as new string
|
|
|
|
|
const cleanedString = string
|
|
|
|
|
.toLowerCase()
|
|
|
|
|
.split('')
|
|
|
|
|
.filter((character) => alphanumerical.includes(character))
|
|
|
|
|
.join('');
|
|
|
|
|
|
|
|
|
|
// Create a new reversed string for comparison
|
|
|
|
|
const reversedString = cleanedString.split('').reverse().join('');
|
|
|
|
|
|
|
|
|
|
// Return the outcome of the comparison which will either be true or false
|
|
|
|
|
return cleanedString === reversedString;
|
2022-02-20 14:07:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = palindromes;
|