8000 Replace palindromes exercise solution with non-regex version (#438) · mstottrop/javascript-exercises@f215901 · GitHub
[go: up one dir, main page]

Skip to content

Commit f215901

Browse files
authored
Replace palindromes exercise solution with non-regex version (TheOdinProject#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.
1 parent a7bcd23 commit f215901

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
const palindromes = function (string) {
2-
const processedString = string.toLowerCase().replace(/[^a-z0-9]/g, "");
3-
return processedString.split("").reverse().join("") == processedString;
2+
// Since we only consider letters and numbers, create a variable containing all valid characters
3+
let alphanumerical = 'abcdefghijklmnopqrstuvwxyz0123456789';
4+
5+
// Convert to lowercase, split to array of individual characters, filter only valid characters, then rejoin as new string
6+
const cleanedString = string
7+
.toLowerCase()
8+
.split('')
9+
.filter((character) => alphanumerical.includes(character))
10+
.join('');
11+
12+
// Create a new reversed string for comparison
13+
const reversedString = cleanedString.split('').reverse().join('');
14+
15+
// Return the outcome of the comparison which will either be true or false
16+
return cleanedString === reversedString;
417
};
518

619
module.exports = palindromes;

0 commit comments

Comments
 (0)
0