Javascript projects: Palindrome Checker

I have been trying for the past day or so to put something together that could work for this project but what I currently have is not working in any way. I know there are many problems with the code but I am not sure what to change as my skills with javascript are rough. Would anyone have any suggestions?

Here is a snip of what I have so far.

Edit: Here is the code

function strReverse() {
    return copyStr.split("").reverse().join("");
  }
  var copyStr = ("");
  
  function palindrome(str) {
    //Step 2: create a copy of the inputed string
    copyStr = str.slice();
    //Step 3: Change all characters of copy to uppercase and remove
    //non-alphanumeric characters
    copyStr.toUpperCase();
    copyStr.replace(/\W_/gi, '');
    //Step 4: Reverse the copy of the string
    strReverse();
    //Test if the original string matches the copy
    if (str == copyStr) {
      return "This is a palindrome"
    } else if (str !== copyStr) {
      return "This it not a palindrome"
    }
    return palindrome;
  }
  
  palindrome("eye");

Please post actual code instead of a picture. Thanks

It is always better if you don’t give us pics of your code but rather just paste it in here so we can copy/paste it ourselves for testing. I would probably not use a global variable for this (I’m referring to copyStr) but I don’t think that is your main problem. Remember, you only want to compare alpha-numeric characters. That applies both to the original string and the reversed string.

Sorry about that, I have fixed the post and included the code instead of a picture. I’ll keep that in mind for next time.

Try adding some console.log statements to your code so you can see the code work and debug it.


function strReverse() {
    return copyStr.split("").reverse().join("");
  }
  var copyStr = ("");
  
  function palindrome(str) {
    //Step 2: create a copy of the inputed string
    copyStr = str.slice();
    //Step 3: Change all characters of copy to uppercase and remove
    //non-alphanumeric characters
    copyStr.toUpperCase();
   console.log(“uppercased: “+copyStr);
    copyStr.replace(/\W_/gi, '');
    console.log(“refined: “+copyStr);
    //Step 4: Reverse the copy of the string
    strReverse();
    console.log(“reversed: “+ copyStr):
    console.log(“original: “+ str);
    //Test if the original string matches the copy
    if (str == copyStr) {
      return "This is a palindrome"
    } else if (str !== copyStr) {
      return "This it not a palindrome"
    }
    return palindrome;
  }
  
  palindrome("eye");

Something like the above should be interesting…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.