-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindromes.js
46 lines (37 loc) · 1.17 KB
/
palindromes.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
35
36
37
38
39
40
41
42
43
44
45
46
// Bonfire: Check for Palindromes
// Return true if the given string is a palindrome. Otherwise, return false.
//
// A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
//
// You'll need to remove punctuation and turn everything lower case in order to check for palindromes.
//
// We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.
function palindrome(str) {
// get phrase, remove spaces, convert to lowercase, store into variable
var strEdited = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
// reverse sting
var StrReverse = strEdited.split('').reverse().join('');
// compare reversed string with original
// if same
// return true
// else
// return false
// end
if ( StrReverse === strEdited) {
return true;
} else {
return false;
};
}
palindrome("race car");
function palindrome(str) {
var clean = str.replace(/[^A-Z0-9]/ig, "").toLowerCase();
var compare = clean.toLocaleLowerCase().split("").reverse().join("");
if (clean === compare) {
return true;
}
else {
return false;
}
}
palindrome("race car");