0

So, below you can find my (very basic and unoptimized) solution to Leetcode's challenge 189 'Rotate Array'. The target of this challenge is posed as follows: Given an array, rotate the array to the right by k steps, where k is non-negative.

Now, my solution is not accepted as somehow the global variable nums remains unchanged after the function call. Why is that? Somehow nums is treated as local variable and doesn't change the passed-in global variable nums. I get that it's probably because of how javascript treats variable scopes but I don't seem to find resources that can help me understand this example.

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = function(nums, k) {
    array2 = Array.from(nums)
    nums.map((num) => {
        array2[(nums.indexOf(num)+k)%nums.length] = num
    })
    nums = Array.from(array2)
    console.log(nums) // returns expected answer
};
Your input
[1,2,3,4,5,6,7]
3

Your stdout
[5,6,7,1,2,3,4]

Your answer
[1,2,3,4,5,6,7]

Expected answer
[5,6,7,1,2,3,4]

3
  • 1
    "Somehow nums is treated as local variable" - that's because you declared it as a function parameter Commented Jan 23, 2022 at 15:43
  • That..makes sense, function parameters are treated as local variables inside functions then. Still, I don't get how my answer isn't accepted as I am clearly modifying the local variable as stated in the exercise, no? Commented Jan 23, 2022 at 15:46
  • What the exercise means by "modify nums in-place" is that you should mutate (modify) the array object that is held by nums (ie that is passed in for the nums parameter), not that you should assign (modify) the local nums variable which has no effect outside of the function. Commented Jan 23, 2022 at 15:49

2 Answers 2

2

The map function does not modify the original array. Additionally, you seem to be using it incorrectly.

You are using map to set the values of array2 and then setting nums to a copy of array2. This will not modify the original object contained in nums.

Of course, if you log nums within the function, it will give you the updated value, but this value is scoped within the function and won't be accessible outside.

Instead of array methods, for this type of impure programming, you should use a for loop.

var rotate = function(nums, k) {
    array2 = Array.from(nums);
    for (const i in nums) nums[i] = array2[(i+k)%nums.length];
};
Sign up to request clarification or add additional context in comments.

Comments

1

Your approach is correct, but here is the issue:

var rotate = function(nums, k) {
    array2 = Array.from(nums)
    nums.map((num) => {
        array2[(nums.indexOf(num)+k)%nums.length] = num //Making changed to array2
    })
    nums = Array.from(array2) //Completely changing the reference of nums
    console.log(nums) // returns expected answer
};

let nums =  [1,2,3,4,5,6,7];
rotate(nums,3); 
console.log(nums); //Original nums still as it is

If you look at the above code, nums is actually not changing. Reason being you are creating a completely new array array2. Inside your .map(), you are making all sorts of changing to it and assigning that back again to nums. But the reference of the original nums is unchanged.

You should actually be making change to your nums and using array2 for help:

var rotate = function(nums, k) {
        array2 = Array.from(nums)
        array2.forEach((num) => {
        nums[(array2.indexOf(num)+k)%array2.length] = num
        })
        console.log(nums) // returns expected answer
    };

let nums =  [1,2,3,4,5,6,7];
    rotate(nums,3);
    console.log(nums);

PS: .map() is when you want a new transformed array returned. Otherwise you can simply use .forEach, or a for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.