So, I was trying this LeetCode problem = https://leetcode.com/problems/two-sum/
The solution that I wrote required me to sort an array, but I would need to return the indices related to the original array. Because of this, I created a constant to store the original and then search it later after I've found those indices.
My solution ended up not working because the sort method is altering that constant value. I don't know why this is happening, all I know is that the sort method alters the original array when called (that's why I needed to store it before sorting). I thought that if a created a constant it couldn't be altered afterward. That's why I think it's a bug or something
This is the code that I've been trying (I left the console.logs there for clarity)
To test you should use
nums = [3, 2, 4] target = 6 var twoSum = function(nums, target) { var max = nums.length - 1; var min = 0; const nums2 = nums; console.log(nums2) var nums1 = nums.sort(function(a,b){return a - b}); console.log(nums2) while (min != max){ if (nums1[min] + nums1[max] > target) { max -= 1; } else if (nums1[min] + nums1[max] < target) { min += 1; }; if (nums1[min] + nums1[max] == target) { return [nums2.indexOf(nums1[min]), nums2.indexOf(nums1[max])]; }; }; };
const. Yournumsandnums2are *the same object`, not two different ones that happen to hold the same values.