2

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])]; }; }; }; 
5
  • Also possibly relevant: Const in javascript? When to use it and is it necessary and Copy array by value Commented Oct 2, 2019 at 19:45
  • hey, thanks!! these are pretty helpful. I don't think it's actually a duplicate, because in that thread what it's being discussed is the matter of reassigning a value to a constant through declaration. In my example, I shouldn't be able to change the value of nums2 if it is a constant. I'm not saying that what I assigned to nums2 can't be changed, and that, I think, is the subject of the other thread. At least, I couldn't find it Commented Oct 2, 2019 at 19:58
  • 1
    You are not reassigning you constant - it's an array, so it's an object. Mutating an object is possible, even if it's a const. Your nums and nums2 are *the same object`, not two different ones that happen to hold the same values. Commented Oct 2, 2019 at 20:00
  • 1
    You're not altering the const value, you're altering what it references. Commented Oct 2, 2019 at 20:01
  • To me it is weird that happens this way. It is the first time that I made any post or even logged in, but I should have just researched a little bit more, cause the links VLAZ posted could actually fix my problem... thanks Commented Oct 2, 2019 at 22:39

1 Answer 1

0

Two things here:

  1. const nums2 = nums; does not copy the array, it just gives you a non-reassignable reference (e.g. you cannot assign a new value to nums2 with =, but the contents of num2 is up for grabs)
  2. nums.sort(...) sorts in place, thus it does modify the original array (see Array.prototype.sort). Since nums2 is a reference to nums, it shows those changes to nums in the second console.log

nums = [3, 2, 4] target = 6 var twoSum = function(nums, target) { var max = nums.length - 1; var min = 0; // force a copy of the array // note: this is a shallow copy const nums2 = nums.slice(); 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])]; }; }; }; twoSum(nums, target);

Sign up to request clarification or add additional context in comments.

3 Comments

This would probably work just fine. Thanks!!!
@RodrigoMallmann If this actually gives you an answer you find to be valuable, you can also mark it as answering your question. stackoverflow.com/help/someone-answers
Sorry, I'm a total noob! hahaha. Now I think I've done it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.