1function palindrome(str) {
2
3 var len = str.length;
4 var mid = Math.floor(len/2);
5
6 for ( var i = 0; i < mid; i++ ) {
7 if (str[i] !== str[len - 1 - i]) {
8 return false;
9 }
10 }
11
12 return true;
13}
14
1function isPalindrome(str) {
2 str = str.replace(/\W/g, '').toLowerCase();
3 return (str == str.split('').reverse().join(''));
4}
5
6console.log(isPalindrome("level")); // logs 'true'
7console.log(isPalindrome("levels")); // logs 'false'
8console.log(isPalindrome("A car, a man, a maraca")); // logs 'true'
9
10
1const isPalindrome = str => str === str.split('').reverse().join('');
2
3// Examples
4isPalindrome('abc'); // false
5isPalindrom('abcba'); // true
1isPalindrome = function(x) {
2 if (x < 0) {
3 return false;
4 }
5
6 return x === reversedInteger(x, 0);
7};
8
9reversedInteger = function(x, reversed) {
10 if(x<=0) return reversed;
11 reversed = (reversed * 10) + (x % 10);
12 x = Math.floor(x / 10);
13
14 return reversedInteger(x, reversed);
15};
1function remove_spaces_strict(string)
2{
3 return string.replace(/\s/gm, "");
4}
5
6function to_lower_case(string="")
7{
8 return string.toLocaleLowerCase();
9}
10
11
12function isPalindrome(string) {
13 let string1 = string;
14 if (typeof string === 'string' || typeof string === 'number') {
15 if (typeof string === 'number') {
16 string1 = new Number(string1).toString();
17 }
18 string1 = remove_spaces_strict(string1);
19 string1 = to_lower_case(string1);
20 let len1 = string1.length;
21 let len = Math.floor(string1.length / 2);
22 let i = 0;
23
24 while(i < len) {
25
26 if (string1[i] !== string1[len1 - (1 + i)]) {
27 return false;
28 }
29 console.log(string1[i] + " = " + string1[len1 - (1 + i)]);
30 i++;
31 }
32 }else{
33 throw TypeError(`Was expecting an argument of type string1 or number, recieved an argument of type ${ typeof string1}`);
34 }
35 return string;
36}
37