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.
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");
}
}
What a smart solution !
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