Can someone help me with my palindrome code

Tell us what’s happening:
my code is supposed to take a string called str1 and return true if its a palindrome (word thats spelt the same forwards and backwards) and false if it isnt. however i keep getting a error saying str1.chartAt is not a function. i was wondering if anyone could help.

i know a for loop would be simpler i was just seeing if it would work in a while loop cus i didnt know what to do.

Your code so far


function isThisAPalindrome(str1) {
  
var length
length = (str1.length);
var count = 0
let i = 0;
while(i < length){
if (str1.charAt(length-i) === str1.charAt(i)){

count++;
 if (count == length){
   return true;
 }
} else {
  return false;
}



i++;
}


}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.72

Challenge: Comment Your JavaScript Code

Link to the challenge:

chartAt () is not a function; it’s a misspelled version of CharAt

i just changed that thank you, it runs now however it sill doesn’t return true when it is a palindrome. do you have any idea what it could be that’s wrong?

 if (count == length){
   return true;
 }
} else {
  return false;
}

A return statement will end the current function, always. Since both your if and else both have a return, it will always end. The containing if will fire if those two chars match. So, it will run the if/else the first time it finds a match.

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