xxxxxxxxxx
function palindrome(str) {
var len = str.length;
var mid = Math.floor(len/2);
for ( var i = 0; i < mid; i++ ) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
xxxxxxxxxx
function solution(inputString) {
return inputString === inputString.split('').reverse().join('');
}
xxxxxxxxxx
function isPalindrome(str) {
str = str.replace(/\W/g, '').toLowerCase();
return (str == str.split('').reverse().join(''));
}
console.log(isPalindrome("level")); // logs 'true'
console.log(isPalindrome("levels")); // logs 'false'
console.log(isPalindrome("A car, a man, a maraca")); // logs 'true'
xxxxxxxxxx
function solution(st) {
let palindrome = st;
let i = 0;
while (palindrome !== palindrome.split('').reverse().join('')) {
palindrome = st + st.slice(0, i).split('').reverse().join('');
i++;
}
return palindrome;
}
xxxxxxxxxx
// Palindrom or not
// l e v el
const palindrome = (data) => {
var len = data.length;
var mid = Math.floor(len / 2);
for (var i = 0; i < mid; i++) {
if (data[i] !== data[len - 1 - i]) {
return false;
}
}
return true;
}
console.log(palindrome("level"))
xxxxxxxxxx
function palindrome(str) {
// Remove all non-alphanumeric characters and convert to lowercase
str = str.replace(/[^a-z0-9]/gi, '').toLowerCase();
// Use the built-in reverse method to reverse the string
var reversed = str.split('').reverse().join('');
// Check if the reversed string is the same as the original
return str === reversed;
}
how to check string is palindrome or not in javascript
xxxxxxxxxx
let str = "AbcBa";
// Check Palindrome using built-in Functions
function isPalindrome(str){
return str === str.split("").reverse().join("");
}
console.log(isPalindrome(str.toLowerCase())); // true
// Check Palindrome Using for Loop
function isPalindrome2(str){
for(let i=0, j=str.length -1; i<j; i++, j--){
if(str[i] !== str[j]){
return false;
}
}
return true;
}
console.log(isPalindrome2(str.toLowerCase())); // true
xxxxxxxxxx
function palindrome(str) {
var splitted = str.split("");
var reversed = splitted.reverse("");
var joined = reversed.join("");
return joined.toLowerCase().replace(/[^0-9a-z]/gi, '') == str.toLowerCase().replace(/[^0-9a-z]/gi, '')
}
xxxxxxxxxx
function isPalindrome(s) {
var len = s.length;
if (len < 2) {
return true;
}
var i = 0;
var j = len - 1;
while (i < j) {
if (s[i] != s[j]) {
return false;
}
i += 1;
j -= 1;
}
return true;
}
xxxxxxxxxx
function isPalindrome(num) {
const temp = num.toString().split('').reverse().join('') * 1;
return (result = num === parseInt(temp) ? true : false);
}
console.log(isPalindrome(121));
console.log(isPalindrome(320));