Check for Palindromes - Can anyone tell me where i am going wrong?

Check for Palindromes - Can anyone tell me where i am going wrong?
0

#1

Here is what I have got so far but I am unsure why my code does not complete the test.

Your code so far

function palindrome(str) {
  // remove non-alphanumerical characters and transform to lower case
  var strClean = str.replace(/[0-9a-z]/gi, '').toLowerCase();

  // convert string to an array
  var strArray = str.split('');

// reverse the array
  var strArrayReverse = strArray.reverse();

// compare each element of array and reverse array
  for(var i = 0; i < strArray.length; i++){
    if(strArray[i] === strArrayReverse[i]){
      return true;
    } else {
      return false;
    }
  }

}



palindrome("eye");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063.

Link to the challenge:


#2

What @JScars said, plus in your for loop, your solution only iterates one time before you return true or false. Once a return statement is executed, the function is exited and a value is returned. The function is exited even if a for loop has not been completely iterated.

One way to approach this problem is to look for a false condition while iterating through the array and return false when found. After iterating through the entire array and never seeing a false condition, would mean you could safely return true.


#3
  1. You are not using the strClean variable
  2. When you use the method reverse() it saves the reverse array in the same object, so strArray is reversing too.
  3. An array can’t be copied using the = operator. They are referenced, it means that when you change the original “the copy” is also changed

A solution for this is, for example change the strArrayReverse line making it a “split” of the original str and then reversing the same.

You also need to return true only

if all the letters are the same forward and backward. It means after check them all

that’s a clue

~Greetings, and good luck.


#4

Thanks for the help @JScars and @RandellDawson I ended up with this solution after fixing my mistakes

function Palidrome(str){
    // remove all non-alphanumerical characters
    var newStr = str.replace(/[^0-9a-z]/gi, '').toLowerCase();
    // convert str to array, reverse the array then join the array back together creating a new string 
    var reverseStr = newStr.split("").reverse().join('');
    // compare the cleaned string and the reversed string
   if(newStr === reverseStr){
       console.log("true");
   } else {
       console.log("false");
   }
}

#5

Your function will still need to return a value of true or false.


#6

if(newStr === reverseStr){
return true;
} else {
return false;
}

I had written my code using VScode so I needed to print the result to the console.


#7

Since newStr === reverse evaluates to a true or false value, you could also just return this expression as:

return newStr === reverseStr;

This would eliminate the if/else statements and make your code more streamlined.


#8

What a smart solution :hushed:!
the split doesn’t changes the original array so the newStr variable keep original and you generates a copy.
then you reverse the copy array.
and finaly you joint the copy array.

Excelent! a big demostration of the importance of know the difference beetween value and reference


#9

Thanks for the advice :ok_hand:t5: