Palindrome Checker problem

i have problem with this code , can you help me to fix it

function palindrome(str) {

  str = str.replace(/[^a-zA-Z0-9]/g, '');

  str = str.toUpperCase();

  var arr1 = str.split('');

  var arr2 = arr1.reverse();

  function arraysAreIdentical(arr1, arr2){

    for (var i = 0; i < arr1.length;  i++){

        if (arr1[i] !== arr2[i]){

            return false;

        }

    }

    return true; 

}

arraysAreIdentical(arr1, arr2)

    }

palindrome("ok");

have you tried to do some debugging using console.log?

thank you @ilenia
yes i use console.log with it but it give me all time true only whene i write the elements of arr1 and arr2 ex( arrysAreIdentical([β€œo”,β€œk”],[β€œk”,β€œo”]) it means dont work automaticly :thinking:

Array.prototype.reverse() works in place. This means when you do arr1.reverse() you are reverting arr1, and also referencing arr1 in a new variable arr2 (so, both arrays will always be the same)

thank you @rferromoreno i fix that parte by doing this code ;
var arr1 = str.split(’’);
var arr2 = str.split(’’).reverse();
but i still have problem to show the result on screen , whene i use console.log on palindrome(β€œeye”); it return undefined

It’s because your function is not returning any result.

On the last line of your function you should do return arraysAreIdentical(arr1, arr2)

@rferromoreno thanks for your help it work now :heart_eyes:

1 Like

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