Words Palindrome checker!

Hello FCC Campers, well i want to make palindrome checker only for words,my code obviously doesn’t work.

let arrList = 'kayak';

function palindrome(arr){
  for(let i = 0; i < arr.length; i++){
      for(let j = 1; j < arr.lentgh; j--){
        if(arr[i] === arr[arr.lentgh - j]){
          return true;
        } else {
          return false
        }
      }
  }
  return arr
}
console.log(palindrome(arrList))

A few things to get you started

  • 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?
1 Like

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.

1 Like

you are just doing that, making so that a word like test returns true because first and last letter are equal

1 Like

@bbsmooth, @ilenia i get that part, but for example when i write the word welcome it’s logging the word !

the second loop is never entered:

so this is executed:

careful also that if you fix your typo you have an infinite loop:

1 Like

I will try to fix that, maybe i should use a recursive function, will see that, thank you so much for you time and for you answers @ilenia

@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`
}

congratulations!
and now maybe try to solve this:

1 Like

Thank you, well i will try !! :smiley:

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.

1 Like

I will try, and let you know thank you!!

1 Like