From f215901131d9bc81a91c5a248f955d3385e872fa Mon Sep 17 00:00:00 2001 From: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> Date: Sat, 2 Mar 2024 13:57:16 +0000 Subject: [PATCH] Replace palindromes exercise solution with non-regex version (#438) Adapted the non-regex solution provided in the solutions branch (which was not brought over to the main branch's solution file) to pass the current test suite. Cleaned formatting of comments and reworded for clarity. --- 09_palindromes/solution/palindromes-solution.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/09_palindromes/solution/palindromes-solution.js b/09_palindromes/solution/palindromes-solution.js index 7950015..b731371 100644 --- a/09_palindromes/solution/palindromes-solution.js +++ b/09_palindromes/solution/palindromes-solution.js @@ -1,6 +1,19 @@ const palindromes = function (string) { - const processedString = string.toLowerCase().replace(/[^a-z0-9]/g, ""); - return processedString.split("").reverse().join("") == processedString; + // Since we only consider letters and numbers, create a variable containing all valid characters + let alphanumerical = 'abcdefghijklmnopqrstuvwxyz0123456789'; + + // 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; }; module.exports = palindromes;