You’ve got some typos when accessing the array length.
A return statement automatically ends the function. Your if/else statement always returns a value. Thus, the first time you reach the if/else statement the function will end. Do you really want to do this?
Well, what i want to do is comparing the first letter with the last one … etc , if they’re equal so this word is palindrome if not then it’s false, i guess i didn’t translate my idea correctly !!
It’s the …etc part that isn’t being done because you are always ending the function after the first call to if/else. You’ll need to move the return true; statement somewhere else.
@ilenia Well, i did it but it can be use just on words not sentences !
thank you for your help
function palindrome(str){
str = str.toLowerCase();
for(let i = 0; i < str.length; i++){
if(str[i] !== str[str.length -1 - i]){
return `${str} is not a palindrome word`
}
}
return `${str} is a palindrome word`
}
The logic to make it work for sentences is exactly like your solution for words. Substitute the string for an array, and rewrite the return statements.