-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCC-11.js
34 lines (28 loc) · 983 Bytes
/
DCC-11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Challenge: 11-isPalindrome
// Difficulty: Intermediate
// Prompt:
// - Write a function called isPalindrome that accepts a single string argument, then returns true or false depending upon whether or not the string is a palindrome.
// - A palindrome is a word or phrase that are the same forward or backward.
// - Casing and spaces are not included when considering whether or not a string is a palindrome.
// - If the length of the string is 0 or 1, return true.
// Examples:
// isPalindrome('SEI Rocks'); //=> false
// isPalindrome('rotor'); //=> true
// isPalindrome('A nut for a jar of tuna'); //=> true
// isPalindrome(''); //=> true
const isPalindrome = (str) => {
let noSpaceString = str.toLowerCase().replace(/\s+/g, "");
if (str.length === 0 || str.length === 1) {
return true;
}
let changedString = str
.toLowerCase()
.split("")
.reverse()
.join("")
.replace(/\s+/g, "");
if (changedString === noSpaceString) {
return true;
}
return false;
};