mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-06-05 15:09:09 +09:00
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.
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user