2

Here is the problem. https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker

My solution was

function palindrome(str) {{
let regex = /\W/g;
/\W/g.remove;
str.toLowerCase;
}
function reverseString(str) {
const reverseString = "";
for (let i = str.length - 1; i >= 0; i++) {
  reverseString += str[i];
}
return reverseString;
}
if (str === reverseString) {
  return true;
}  return false;
}

palindrome("eye");

What should I do?

Photons
  • 129
  • 3
  • 1
    What should you do about what? – Nick Jul 05 '22 at 04:32
  • what does this do? `/\W/g.remove` also, `str.toLowerCase` does nothing too, even if you just did `str.toLowerCase()` (i.e. called the method) it still wouldn't do what you expect ... and `function palindrome(str) {{` ... you only need one `{` – Jaromanda X Jul 05 '22 at 04:38
  • Does this answer your question? [Palindrome check in Javascript](https://stackoverflow.com/questions/14813369/palindrome-check-in-javascript) – poPaTheGuru Jul 05 '22 at 05:16

2 Answers2

3

here's the problems with your code

  1. function palindrome(str) {{ remove the second {
  2. you declare regex but never use it
  3. /\W/g.remove; ... what does this do? a regex doesn't have a remove property
  4. toLowerCase is a function, that returns a new value - what you do doesn't call the function, nor does it store the result of calling the function
  5. remove this line
  6. OK
  7. don't make this a const because you can't change const's - also, naming a variable the same as the function it is in can cause confusion - as you'll see
  8. this will cause an infinite loop, you need i-- to decrement i on each loop
  9. OK
  10. OK
  11. OK
  12. OK
  13. You need to call the function - it's confusing because your variable name
  14. OK
  15. OK
  16. OK
  17. OK
  18. This calls the function, but does nothing with the result

So, the resulting fixed code is:

function palindrome(str) {
    let regex = /\W/g;
    str = str.toLowerCase().replace(regex, '');

    function reverseString(str) {
        let reverseString = "";
        for (let i = str.length - 1; i >= 0; i--) {
            reverseString += str[i];
        }
        return reverseString;
    }
    if (str === reverseString(str)) {
        return true;
    }
    return false;
}

const result = palindrome("eye");
console.log(result)

But, no need for that inner function

function palindrome(str) {
    const regex = /\W/g;
    let reverseString = "";

    str = str.toLowerCase().replace(regex, '');

    for (let i = str.length - 1; i >= 0; i--) {
        reverseString += str[i];
    }

    if (str === reverseString) {
        return true;
    }
    return false;
}

const result = palindrome("eye");
console.log(result)

Though I'd probably write it like

function palindrome(str) {
    str = str.toLowerCase().replace(/\W/g, '');

    return str === str.split('').reverse().join('');
}

const result = palindrome("eye");
console.log(result);
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
1

You can do the following:

function palindrome(str) {
    str = str.toLowerCase();  //Turn everything to lower case
    str = str.replace(/[^a-z0-9]/g, ''); // Remove all non alphanumeric characters.

    var revStr = str.split('');
    revStr = revStr.reverse().join('').toString();
  
    return str == revStr;

}
samnoon
  • 1,340
  • 2
  • 13
  • 23