-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidPalindrome.js
41 lines (33 loc) · 1.26 KB
/
validPalindrome.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
'use strict';
const isPalindrome = function(s) {
s = s.replace(/[^a-z0-9]/gi,'').toLowerCase();
let sArray = s.split('');
let sArrayReversed = s.split('').reverse();
if (JSON.stringify(sArray) === JSON.stringify(sArrayReversed)) return true
else return false;
};
// 'use strict';
// const isPalindrome = function(s) {
// // replace non-alphanumeric chars with '' and convert it to lower case
// s = s.replace(/[^a-z0-9]/gi,'').toLowerCase();
// // divide the string into 2 (front & end)
// let n = s.length / 2;
// let front, end;
// // if the string is odd number, ignore the char in the center when dividing the char into 2 (front & end)
// if (!n % 1) {
// front = s.slice(0, Math.floor(n));
// end = s.slice(Math.ceil(n));
// } else {
// front = s.slice(0, n);
// end = s.slice(n);
// }
// // loop through front & end strings to check if it's panlindrome
// for (let i = 0; i < front.length; i++) {
// console.log(`front[${i}]: ${front[i]}`);
// console.log(`end[end.length - ${i} + 1]: ${end[end.length - (i + 1)]}`);
// if (front[i] !== end[end.length - (i + 1)]) return false;
// }
// return true;
// };
// // let s = "A man, a plan, a canal: Panama";
// // console.log(isPalindrome(s));