From 9eb942909e9ddbda53e1ea2bfeda162812e2b38b Mon Sep 17 00:00:00 2001 From: Hifilo <92943726+Hifilo@users.noreply.github.com> Date: Wed, 22 Mar 2023 21:42:17 -0400 Subject: [PATCH] 09 Palindromes: Replace Regex Solution (#319) * Replaces the original regex method, with more familiar methods * Add descriptive comments for clarity --- palindromes/palindromes.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/palindromes/palindromes.js b/palindromes/palindromes.js index 5c41da4..dd9e277 100644 --- a/palindromes/palindromes.js +++ b/palindromes/palindromes.js @@ -1,11 +1,14 @@ -const palindromes = function(string) { - const processedString = string.toLowerCase().replace(/[^a-z]/g, ""); - return ( - processedString - .split("") - .reverse() - .join("") == processedString - ); +// Non regex +const palindromes = function (string) { + let alphabet = 'abcdefghijklmnopqrstuvwxyz'; //Create a variable that holds all the letters of the alphabet + const cleanedString = string // Convert to lowercase, split, & filter only letters, rejoin as new const + .toLowerCase() + .split('') + .filter((letter) => alphabet.includes(letter)) + .join(''); + const reversedString = cleanedString.split('').reverse().join(''); //Create a new const that holds reversed string + return cleanedString === reversedString; //Compare cleanedString & reversedString which returns true/false }; +// Do not edit below this line module.exports = palindromes;