I is not defined

I am working on the Palindrome problem and i cant seem to get my code to run correctly. I have the code and all of the test cases written in VS Code, where i worked and solved the problem, but now that i copied and pasted it into the FreeCodeCamp environment in google chrome, I keep getting this error for every test case: “i is not defined.” Everything works fine in VS code, Atom, and on SoloLearn.com. Anyone know what the problem might be? Here is my code:

function palindrome(str) {
    let alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    let alphaOnly = "";

    for (i = 0; i < str.length; i++) {
        if (alphabet.includes(str[i])) {
            alphaOnly += str[i];
        }
    }

    let loweredAlpha = alphaOnly.toLowerCase();
    let reversed = loweredAlpha.split("").reverse().join("");

    return loweredAlpha == reversed;
}


console.log(palindrome("eyE")); //true
console.log(palindrome("_eye")); //true
console.log(palindrome("race car")); //true
console.log(palindrome("0_0 (: /-\ :) 0-0")); //true
console.log(palindrome("My age is 0, 0 si ega ym.")); //true
console.log(palindrome("animal")); //false
console.log(palindrome("not a palindrome")); //false
console.log(palindrome("1 eye for of 1 eye.")); //false
console.log(palindrome("five |\_ /| four")); //false
console.log(palindrome("nope")); //false

I think you want for (let i = 0; ...

2 Likes

variables in JS have to be declared with const (short for constant meaning it’s not going to be reassigned), let (you will be reassigning as in this case), or var (try not to use this one because the other two cover all use cases and are more descriptive).

1 Like

You were right lol. I don’t know how that got past me. I’m usually pretty good about making sure i declare that i. I wonder why VS code and Atom didn’t tell me that I was doing something funky? Any-who, thanks for the help!

1 Like