Skip to content

Commit e8fc8ce

Browse files
committed
Update palindromes to handle numbers better
The previous solution removed numbers entirely, whereas this one treats them like letters and checks if they are evenly spaced. More importantly, the old solution test seemed to check if the numbers were palindromic, but because the solution replaced them with "", it wasn't testing what it seemed to. I added a new test to differentiate between the palindromic "rac3e3car" and the non-palindromic "r3ace3car". These changes also obviate the problems raised in Issue TheOdinProject#355.
1 parent 3256f98 commit e8fc8ce

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

09_palindromes/solution/palindromes-solution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const palindromes = function (string) {
2-
const processedString = string.toLowerCase().replace(/[^a-z]/g, "");
2+
const processedString = string.toLowerCase().replace(/[^a-z0-9]/g, "");
33
return processedString.split("").reverse().join("") == processedString;
44
};
55

09_palindromes/solution/palindromes-solution.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ describe('palindromes', () => {
2424
test('works with numbers in a string', () => {
2525
expect(palindromes('rac3e3car')).toBe(true);
2626
});
27+
test('works with unevenly spaced numbers in a string', () => {
28+
expect(palindromes('r3ace3car')).toBe(false);
29+
});
2730
});

0 commit comments

Comments
 (0)