chunzg
#1
Would be grateful if someone could explain the logic behind arr
and reversed
printing out the same value? Why is arr
already reversed?
function isPalindrome(str) {
var arr = str.replace(/\W/g, '').split('')
var reversed = arr.reverse()
console.log(arr)
console.log(reversed)
}
jsdisco
#3
In other words, the reverse() method changes the original array.
If you want to keep the original array, you’d have to spread it into a new array before applying reverse to it like so:
var reversed = [...arr].reverse()
2 Likes
ilenia
#4
also arr.slice().reverse()
3 Likes
jsdisco
#5
Yeah, that’s how the ancients did it 
2 Likes