I am doing some JS algorithms test, for example I want to check whether a word is palindrome.
And let’s say that we ignored the fact that there might be non-letters/upper or lowercase in the passed in variable.
1st method: compare array:
function palindrome(str) {
let arr = str.split('');
let reversed = [];
for(let i = arr.length-1;i>=0;i--){
reversed.push(arr[i]);
}
console.log("reversed is:" + reversed);
console.log("original is:" + arr);
if(reversed===arr){return true;}
else{return false;}
}
console.log(palindrome("eye"));
2nd method comparing strings
function check (string){
let new_string = string.split('').reverse().join('');
console.log(string);
console.log(new_string);
if (string === new_string){
return true;
}
return false;
}
console.log(check("hello"));
console.log(check("eye"));
how come the second one works and the 1st one will give you false no matter what?