Post your Palindrome Checker solutions here!

This is how I did it:

function palindrome(str) {
  // remove all special characters from str 
  let noAlphaNumericChars = str.replace(/([^0-9a-z]|\s)/gi, '');
  // change str to lowecase
  
  let lowerCaseStr = convertToLowerCase(noAlphaNumericChars);
  
  // reverse str
    let reversedStr = reverseStr(lowerCaseStr);

  //compare the two strings
  
    if(lowerCaseStr === reversedStr) {
      return true;
    } else {
      return false;
    }
}

function convertToLowerCase(str) {
      return str.toLowerCase();
}

function reverseStr(str){
  let reversedStr = '';
  for(let i = str.length - 1; i>= 0; i--) {
      reversedStr += str[i];
  }
  return reversedStr;
}
console.log(palindrome("0-0 (: /-\ :) 0-0"));
3 Likes

Awesome job, friend. Compact!!!
If you care, you could just write return (justAlpha === reversedStr). Equality operators (in this case, the strict ===) evaluate to a Boolean result (true or false). so, justAlpha === reversedStr would return true or false.
Here’s a helpful link - Strict equality (===) - JavaScript | MDN

Cheers, T.

2 Likes

I wrote it out as individual steps, and then converted it to an Arrow Function.

Cheers, T.

const palindrome = str =>
    (str.replace(/[\W_]/g, "").toUpperCase()) ===
        ([...(str.replace(/[\W_]/g, "").toUpperCase())]).reverse().join('')

console.log(palindrome("eye")); //should return true.
console.log(palindrome("_eye")); //should return true.
console.log(palindrome("race car")); //should return true.
console.log(palindrome("not a palindrome")); //should return false.
console.log(palindrome("A man, a plan, a canal. Panama")); //should return true.
console.log(palindrome("never odd or even")); //should return true.
console.log(palindrome("nope")); //should return false.
console.log(palindrome("almostomla")); //should return false.
console.log(palindrome("My age is 0, 0 si ega ym.")); //should return true.
console.log(palindrome("1 eye for of 1 eye.")); //hould return false.
console.log(palindrome("0_0 (: /-\ :) 0-0")); //should return true.
console.log(palindrome("five|\_/|four")); //should return false.
3 Likes

function palindrome(str) {
var sentence = str.replace(/[\W_]/g, ‘’);
for(var i = 0, count = sentence.length - 1; i < count/2; i++) {
if(sentence[i].toLowerCase() !== sentence[count-i].toLowerCase()) {
return false;
}
}
return true;
};

palindrome(“eYeye”);

3 Likes

function palindrome(str){
//declare variable to replace within string and make all characters lowercase(in this case)

const replaceVal = str.replace(/[\W_]/gi, ‘’).toLowerCase();

//declare reverse string

let checkPal = replaceVal.split(‘’).reverse().join(‘’);

//strictly compare forward and reverse strings to identify palindromes

return (checkPal === replaceVal);
}
console.log(palindrome(“eye”));//console log as many potential palindrome string types as possible to see what output is printed to the console

5 Likes

Hello, here’s my solution below.

My solution may be a bit cumbersome. a’s first element is compared to its last element, and if it’s the same, b will increase by one. And its second element is compared to its last but one element, and if it’s the same, b will increase by one again, and so one. And then at the end, the first element will be compared to its element again. If b equal a.length (whether a.length is odd or even number), the given string is a palindrome and return true.

And forgive my bad English expression please.

function palindrome(str) {
  let lstr=str.toLowerCase();
  let strregex = /[a-z0-9]/gi;
  let a = lstr.match(strregex);
  let b=0;
  for (let i=0;i<a.length;i++){
    if (a[i]===a[a.length-i-1]){
      b+=1;
    }
  }
  if (b===a.length){
    return true;
  }else{
    return false;
  }
}
2 Likes

here is my solution

function palindrome(str) {
  str=str.replace(/\W|_/g,"")
      .toLowerCase()
  for(let i=0;i< str.length/2; i++){
    if(str[i]!==str[str.length-1-i])
    return false
  }
  return true;
}
4 Likes
function palindrome(str) {
  const letters = str.toLowerCase().match(/[a-z0-9]/g);
  const half = parseInt(letters.length/2);

  for(let i=0; i<half; i++) {
    if(letters[i] !== letters[letters.length - 1 - i]) {
      return false;
    }
  }
  return true;
}
palindrome("eye");
2 Likes

The reverse checking was a tricky one, after some tries there’s my solution:

function palindrome(str) {
  const alphanumericOnly = str
  // 1) Lowercase the input
  .toLowerCase()
  // 2) Strip out non-alphanumeric characters
  .match(/[a-z0-9]/g);
  // 3) Return string === reversedString and check palindrome!
  return alphanumericOnly.join('') === alphanumericOnly.reverse().join('');
}

console.log(palindrome("eye"));
3 Likes

Hi Ben @benhasel2924116

If you select all your code and click on </> icon your code look better.

Good job and happy coding!!

2 Likes

thanks for the tip! Edited for better legibility.
Cheers

3 Likes

Thank you for all the examples. Below my Code:

function palindrome(str) {
  //remove all non-alphanumeric characters
  let cleanStr = str.replace(/\W|_/g, '');
  // covert to lowercase;
  cleanStr = cleanStr.toLowerCase();
  // reverse string  
  let reversedStr = cleanStr.split('').reverse().join('');
  // Compare cleanStr and reversedStr
  for (let i = 0; i < cleanStr.length; i++) {
    // if a letter does not match return false
    if (cleanStr[i] != reversedStr[i]) {
      return false; 
    }
  }
  // cleanStr and reversedStr match, return true
  return true;
}
3 Likes

Love the way how you use the .match() method.
Just remember that it returns an array. Your naming could be confusing as strUpper is now an array.

2 Likes

Thanks a lot for your feedback, you’re absolutely right, didn’t think about that :person_facepalming:

3 Likes

Have some errors specially on the Regex and needs a bit of cleaning up but this is my solution guys:

function palindrome(str) {

let noSpacesStr = str.replace(/\s/g, '').toUpperCase();
let regex = /[A-Za-z0-9]/g;
const noSymbolsArr = noSpacesStr.match(regex);

let j = noSymbolsArr.length - 1;

for (let i = 0; i < noSymbolsArr.length; i++ ){
      if (noSymbolsArr[i] !== noSymbolsArr[j-i]){
        console.log('>>',noSymbolsArr[i],'>>',noSymbolsArr[j-i] );
 return false; 
      }
  }  
return true      

}

console.log(palindrome("0_0 (: /-\ :) 0-0"));
2 Likes
//replaces all non alphanumeric and underscores 
function removeNonAlpha(str) {
  return str.replace(/[\W_]/g,""); 
};
//split the string into an array, reverse it and then rejoin it back into a string
function flipString(str) {
    return str
    .split("")
    .reverse()
    .join("");
};

function palindrome(str) {
//remove non-alphanumeric characters using function above
  const cleanString = removeNonAlpha(str);
  
//make clean string uppercase
  const uppercaseString = cleanString.toUpperCase();

//reverse uppercase string using flipString function
  const reverseString = flipString(uppercaseString);

//compare reversed and uppercase string - if they are the same then return true
if (uppercaseString === reverseString) {
  return true;
  } else {
  return false;
 }
}

palindrome("eye");
2 Likes

Jumped ahead where I could, and used Ramón’s guidance when needed, especially the for loop. Felt good to get most of the way myself :slight_smile:

function cleanNonAlpha(str) {
  return str.replace(/[\W_]/g, "");
}

// 1. For Loop method
//function reverseTheString(str) {
//  let result = "";
//  for (let i = str.length - 1; i >= 0; i--) {
//    result = result + str[i];
//  };
//  return result;
//}

// 2. Easier method: split to array, reverse array, rejoin
function reverseTheString(str) {
  return str.split('').reverse('').join('');
}

function palindrome(str) {
  //Remove non alphanumeric characters
  const nonAlphaStr = cleanNonAlpha(str);

  //Make all lowercase
  const lowerCaseStr = nonAlphaStr.toLowerCase();

  //Reverse string
  const reverseStr = reverseTheString(lowerCaseStr);
  
  //Test output
  //console.log(lowerCaseStr);
  //console.log(reverseStr);

  //Check if str = reverseStr
  if (lowerCaseStr === reverseStr) {
    return true;
  } else {
    return false;
  }
}

console.log(palindrome("A man, a plan, a canal. Panama"));
2 Likes
// FUNCTION TO remove all non-alphanumeric characters 
function cleanUp(str) {
 return str.replace(/[\W_]/g, "");
}

// FUNCTION TO Turn all to a smaller case
function makeSmaller(str) {
  return str.toLowerCase();
}

// FUNCTION TO Reverse to smaller case
function revSmaller(str) {
 //Create an empty string
 let result = "";
 //Create a loop that reverses it
 for (let i = str.length-1; i>=0; i--) {
   result += str[i];
 }
// return result str
 return result;
}

function palindrome(str) {
  // Firstly remove all non-alphanumeric characters 
 const cleanedUpStr = cleanUp(str);

// Turn all to a smaller case
const makedSmallerStr = makeSmaller(cleanedUpStr);

 // Reverse smaller case
const revdSmallerStr = revSmaller(makedSmallerStr);
console.log(revdSmallerStr);


// compare if same then return true
 return revdSmallerStr === makedSmallerStr;
}

console.log(palindrome("DOD"));
2 Likes

function palindrome(str) {

str = str.toLowerCase().replace(/[^a-z0-9]/gi, ‘’);
let reverseStr = str.split(“”).reverse().join(“”);
return str === reverseStr;
}

palindrome(“eye”);

3 Likes

Here’s mine!

function palindrome(str) {
  //initalizes variables
  let isPalindrome = true;
  const cleanReg = /[a-z0-9]/g;
  let countTo;

  
  const cleanArr = str.toLowerCase().match(cleanReg); //sends string to lowercase turns it into an array, cleaning it of non-alphanumerics 
  const arrLen = cleanArr.length-1; //captures length of array
  
  //determines how far the below for loop should run, depending on whether arrLen is even or odd
  if (arrLen % 2 != 0){
    countTo = arrLen/2;
   } else {
    countTo = (arrLen-1)/2;
   }
  
  //compares each entry in the array with its mirror and sets isPalindrome to false if ever it does not match
  for (let i = 0; i < countTo; i++) {
      if (cleanArr[i] != cleanArr[arrLen-i]){
        isPalindrome = false;
      } 
  }
  
  return isPalindrome;
}
3 Likes