Check for Palindromes explain, please!

Check for Palindromes explain, please!
0.0 0

#1

Hello everyone!


Can someone,please, explain, how the computer reads the commands step by step in this task:

function palindrome(str) {

var string = “”;
string = str.toLowerCase().replace(/[^A-Za-z0-9]/g,’’).split("").reverse().join("");

if (string === str.toLowerCase().replace(/[^A-Za-z0-9]/g, ‘’)) {
return true;
}
else if (string != str) {
return false;

}
}

palindrome(“zooz”);


#2

line 1 blank
line 2: declare a variable called string and assign it the value “” (empty string)
line 3: convert str to lowercase and after it is converted replace all characters which are not letters or numbers with ‘’ (empty string) and after the replacement, split whatever is left in to an array, then reverse this array, the join the array back into a string and assign the total result back to string.
line 4 blank
line 5 test if string is equal to a lowercase version of str where all the characters which are not numbers or letters have been replaced by ‘’ (empty string)
line 6 return true if line 4 tests out to be true
line 7 end of first part of if
line 8 test whether string isnot equal to str
line 9 return false if line 8 tests out to be false
line 10 blank
line 11 end of else if part


#3

FYI - lines 9 through 12:

else if (string != str) {
return false;

}

could be replaced with one line and accomplish the same thing:

return false;

#4

Thank you very much, my friend!