Why doesn't this code works?

I already tried running my code on VSCode using Node.js… when I used it here, it just doesn’t work… Sorry for the lame question cause I’m quite new to this site… I usually did challenges on another website (Codewars)


function palindrome(str) {
str = str.toLowerCase();
var fullStr = "";
for (element of str)
    if (element >= 'a' && element <= 'z' || element >= '0' && element <= '9')
        fullStr += element;
console.log(fullStr);
return fullStr === fullStr.split("").reverse().join("");
}

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.75.

Challenge: Palindrome Checker

Link to the challenge:

You’re missing const/let and curly brackets in for.

It works! Thanks… I don’t know why VSCode shows no error though? Btw I follow the rules of C++ in which you don’t require curly brackets if you’re only making one statement under a loop… and it works even without the curly brackets…

Anyway thanks again for the help!!

freeCodeCamp runs JavaScript in strict mode, which does not allow you to use undeclared variables (to avoid scoping nightmares). To test your code in strict mode you can add the line

"use strict"

at the top of your js file.

The braces wasn’t the problem (although they are almost always used, even where not syntactically necessary). The problem is that you didn’t declare element before using it.

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