Basics
https://leetcode.com/problems/palindrome-linked-list/description/
https://www.w3schools.com/jsref/jsref_slice_array.asp
const arr=[1,2];
The expression arr.reverse() reverses the order of elements in the arr array in place, meaning it modifies the original array. The arr.join() method converts the elements of the array into a string, with each element separated by a comma by default.
In the given code, arr.reverse().join() returns a string representation of the reversed array. However, the original arr array has also been reversed because the reverse() method modifies the array directly.
Therefore, arr.reverse().join() === arr.join() will evaluate to true because both sides of the comparison refer to the same array that has been reversed and then converted to a string.
In the code you provided, newarr is assigned the reference to the arr array. Both newarr and arr will point to the same array object in memory. Any modifications made to newarr or arr will affect the same underlying array.
In this case, newarr.reverse() reverses the order of elements in the array in place, modifying the original array. Consequently, both newarr and arr will have the reversed order of elements.
Therefore, newarr.reverse().join() === arr.join() will evaluate to true because both newarr and arr refer to the same reversed array, and both sides of the comparison are converting the reversed array to a string and comparing them.
Comments
Post a Comment