-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSpalindrome.js
More file actions
23 lines (19 loc) · 1.02 KB
/
Copy pathJSpalindrome.js
File metadata and controls
23 lines (19 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function palindrome(str) {
//Replace non-alphanumeric characters as well as make them lowercase.
var Regex = str.replace(/[\W_]/gi, '').toLowerCase();
// \W replaces the non-alphanumeric characters. g(is for global replacement) i(ignores case sensitivity)
/* Take the string(str) passed to the Regex variable, split it, reverse it, then join it together.
This algorithm splits the string to put it into an array so it cna be reversed */
var revString = Regex.split('').reverse().join('');
/* If statement to check if the reversed string matches the criteria of the Regex.
if string passed into the palindrome function IS a palindrome, return true. */
if (revString === Regex) {
return true;
// If the passed in string is NOT a palindrome, return false
} else {
return false;
}
}
/* Call to the palindrome function. The string "race car" is passed in as an argument
in this case, the passed in string is in fact a palindrome */
palindrome("race car");