Let me explain:
function PalindromeTwo(str) {
str = str.split('');
var arr = [];
str.forEach(function(it){
if( (/[a-z]/i).test(it) ){
arr.push(it);
}
})
var reverseArr = arr.reverse();
return reverseArr.join('').toUpperCase() === arr.join('').toUpperCase();
}
PalindromeTwo("hello, there");
In this example, it returns true
when it should return false
because the line var reverseArr = arr.reverse()
reversed not just reverseArr
but arr
, i.e. the original variable, as well. Why is that?