Skip to content

Commit 4249848

Browse files
committed
Complete palindrom checker algorithm
1 parent e3ce72c commit 4249848

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Algorithms from the Freecodecamp Javascript algorithms and data structures certi
1212

1313
- [Basic Algorithms](https://github.com/ahcode0919/javascript-algorithm-ds/blob/master/src/fcc-basic-algorithms/fcc-basic-algorithms.md#freecodecamp-basic-algorithms)
1414
- [Intermediate Algorithms](https://github.com/ahcode0919/javascript-algorithm-ds/blob/master/src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md#freecodecamp-intermediate-algorithms)
15+
- [Algorithm Projects](https://github.com/ahcode0919/javascript-algorithm-ds/blob/master/src/fcc-course-projects/fcc-course-projects.md#freecodecamp-algorithm-projects)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- markdownlint-disable MD001 -->
2+
3+
# Freecodecamp Algorithm Projects
4+
5+
From the Freecodecamp Javascript Certification Algorithms Projects module
6+
7+
- [Palindrome Checker](#palindrome-checker)
8+
9+
#### Palindrome Checker
10+
11+
Return `true` if the given string is a palindrome. Otherwise, return `false`.
12+
13+
A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
14+
15+
Note
16+
You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.
17+
18+
We'll pass strings with varying formats, such as `"racecar"`, `"RaceCar"`, and `"race CAR"` among others.
19+
20+
We'll also pass strings with special symbols, such as `"2A3*3a2"`, `"2A3 3a2"`, and `"2_A3*3#A2"`.
21+
22+
```javascript
23+
export function palindrome(str) {
24+
const normalizedStr = str.toLowerCase().split(/[\W_]+/).join('');
25+
return normalizedStr === normalizedStr.split('').reverse().join('');
26+
}
27+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function palindrome(str) {
2+
const normalizedStr = str.toLowerCase().split(/[\W_]+/).join('');
3+
return normalizedStr === normalizedStr.split('').reverse().join('');
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { palindrome } from "../../src/fcc-course-projects/palindrome_checker";
2+
3+
test('should ', () => {
4+
expect(palindrome('aba')).toBe(true);
5+
expect(palindrome('Race Car')).toBe(true);
6+
expect(palindrome('2!4 4_2')).toBe(true);
7+
expect(palindrome('test')).toBe(false);
8+
expect(palindrome('almostomla')).toBe(false);
9+
});

0 commit comments

Comments
 (0)