Palindrome Checker freecodecamp Problem

Hi guys Ive done the proyect in vscode i tried the code and it worked then I pasted the code inside freecodecamps and its saying some of the test doesnt provide the correct solution. I typed every test in my vscode editor and every test answed the question correctly. The following are the test`s i checked.

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 (: /-\ :slight_smile: 0-0”));

console.log(palindrome(“five|_/|four”));

and every answer matched but not in freecodecamp

Can someone help me please? thanks!

Please post the full code and the link to the challenge as well.

function palindrome(str) {
    let upSideStr=[ ];
    let normalStr=[];
    for(let i=0; i<str.length; i++){
        if(isNumOrLetter(str[i])==false){
            str[i]=" ";
        }else{
            normalStr.push(str[i].toLowerCase());
            
            upSideStr.unshift(str[i].toLowerCase());
        }
    }
    console.log(upSideStr);
    console.log(normalStr);
    return isEqual(upSideStr,normalStr);
  }
  
  function isNumOrLetter(a){
    let question;
    let aNumber=parseInt(a);
    a.charCodeAt(a)>=97&&a.charCodeAt(a)<=122 ? question=true:
    a.charCodeAt(a)>=65&&a.charCodeAt(a)<=90 ? question=true:
    aNumber>=0&&aNumber<=9 ? question=true:
    question=false;
    return question;
  }

function isEqual(str1,str2){
    let isTrue=true;
    for(let i=0; i<str1.length&&isTrue==true; i++){
        str1[i]==str2[i] ? isTrue=true:
        isTrue=false;
    }
    return isTrue;
}
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"));

Do you see this error

true
TypeError: 0 is read-only

This, by the way, is an anti-practice. The ternary is not a general purpose replacement for if.

Is this line legal code?

I would not trust your VSCode, as this should never be allowed in JS. Something must be broken on your computer.

I changed it with str[i]=String.fromCharCode(32) and it keeps giving me the same results on vs code the correct answers in freecodecamp in most of the cases fails the test

Thanks for the advice, in which cases i should use ternary?. I know i haven`t use the best practices, i havent finished the course in freecamp but i wanted to take the challenge for the fun of it. I will do it again once i finish the course.


No, i dont

You can not mutate a string. All primitives are immutable.

const firstName = "John"
firstName[0] = "L"
console.log(firstName) // John

const age = 42
age[0] = 5
console.log(age) // 42

If you want to change a string you have to create a new one, which is why String methods return a new string.

Edit: Do note that it is not a syntax error and no error is thrown unless you are in strict mode (the challenges run in strict mode).

"use strict";
const firstName = "John"
firstName[0] = "L" // Cannot assign to read only property '0' of string 'John'
console.log(firstName)
1 Like

But i dont use strings i use arrays and they are indeed new

Right here this is you attempting to mutate a string

1 Like

My bad i took that line that is wasnt necessary at all and changed the NumOrLetter function with the regex mode. Thanks everyone giving me a hand!

function palindrome(str) {

    let upSideStr=[];
    let normalStr=[];

    for(let i=0; i<str.length; i++){
        if(isNumOrLetter(str[i])==true){
            normalStr.push(str[i].toLowerCase());
            upSideStr.unshift(str[i].toLowerCase());
        }
    }
    console.log(upSideStr);
    console.log(normalStr);
    return isEqual(upSideStr,normalStr);
  }
  
  
  function isNumOrLetter(a){
    let question;
    let regex=/[a-zA-Z0-9]/;
    return regex.test(a);
  }



function isEqual(str1,str2){
    let isTrue=true;
    for(let i=0; i<str1.length&&isTrue==true; i++){
        str1[i]==str2[i] ? isTrue=true:
        isTrue=false;
    }
    return isTrue;

}

str1[i] == str2[i] ? isTrue = true: isTrue = false;

When you do an assignment using a ternary you should do it on the left side of it.

const isAuthed = true;
const route = isAuthed ? '/admin' : '/signin'
console.log(route) // '/admin'

Personally, I would always avoid variable assignments inside the expressions. Although, you do see it from time to time I don’t really think it is the correct use of a ternary.

If you need to do assignments to more than one variable I would suggest not using a ternary.

1 Like

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