0

I need to write a code with a function that has a parameter str that evaluates if the word is a palindrome or not, it should return true, else ir should return false.

Note: I need to remove all non-alphanumeric characters.

I wrote the code below but it is giving the opposite of the result expected:

  • It is giving false for all the cases.
  • Not able to remove the underscore.
  • and when use console.log below the image I get.

console.log result

Code:

function palindrome(str) {
  var exc = /[^\w_]/gi;
  var repStr = str.toLowerCase().replace(exc, "");
  console.log("Replaced string id: " + repStr);
  len = repStr.length -1

  for (var i = 0; i <= len; i++) {
    if (str[i] !== repStr[len] - i) {
      return false;
    }
  }
  return true;
}

console.log(palindrome("eye"));
console.log(palindrome("_eye"));
console.log(palindrome("race car"));
console.log(palindrome("not a palindrome"));
console.log(palindrome("A man, a plan, a canal. Panama"));
console.log(palindrome("never odd or even"));
console.log(palindrome("nope"));
console.log(palindrome("almostomla"));
console.log(palindrome("My age is 0, 0 si ega ym"));
console.log(palindrome("1 eye for of 1 eye."));
console.log(palindrome("0_0 (: /-\ :) 0-0"));
console.log(palindrome("five|\_/|four"));
1
  • Underscore is a part of alphanumeric, your regex won't remove them. Commented Jan 21, 2018 at 15:40

3 Answers 3

1

There are 2 mistakes in your code. 1) the RegExp is not replacing the underscore as [^\w_] reads as "everything that is not a word character OR an underscore". Try

/[\W_]/g

which reads as "one of the following: any character that is not a word character, underscores"

OR

/[^0-9a-z]/g

which reads as "anything that is not: digits (from 0 to 9), english alphabet letters".

2) For it to work you need to write

if(repStr[i] !== repStr[len - i])

instead of

if(str[i] !== repStr[len] - i)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for the help!!
/[0-9a-z_]/ also trims the first char, isn't it? 'eye'.replace(/[0-9a-z_]/, '') give ye
I meant /[^0-9a-z]/g. I'm sorry. I'm dizzy
Also, that was trimming the first character because it was matching the characters I wanted to keep instead of the other ones and it wasn't global (it didn't have the g flag) so it removed only the first character found
0

you can simply replace the two of your lines

for (var i = 0; i <= len; i++, len--) {
    if (str[i] !== repStr[len]) {

check this

function palindrome(str) {
  var exc = /[^\w_]/gi;
  var repStr = str.toLowerCase().replace(exc, "");
  console.log("Replaced string id: " + repStr);
  len = repStr.length -1

  for (var i = 0; i <= len; i++, len--) {
    if (str[i] !== repStr[len]) {
      return false;
    }
  }
  return true;
}

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

Comments

0

Try this out:

function palindrome(str) {
    const letters = str.replace(/[^a-z]/gi, ''); // replace any char that is not in the set [a-z] range
    const lowercase = letters.toLowerCase();
    const reversed = lowercase.split('').reverse().join('');
    return lowercase === reversed;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.