-3

Preparing for an interview and practicing some LC. Doing this question:

https://leetcode.com/problems/move-zeroes/

My solution is as follows:

//filter out 0s using filter and includes methods
//check length differnece between initial and filtered array
//difference = num of 0s. push this amount to end of initial array. 
//make new array with dif as size n then fill with 0s and add to initial with spread operator 

var moveZeroes = function(nums) {
    
    let zero = [0];
    let initLength = nums.length;
    nums = nums.filter(num => !zero.includes(num))
    
    let lengthDif = initLength - nums.length
    let zeroArr = new Array(lengthDif)
    zeroArr.fill(0)
    nums = [...nums,...zeroArr]
   
    
};

The debugger shows that nums has a value that is correct after running but LC is saying that my code is incorrect. Am I missing something here or is LC just buggy?

Giovanni
  • 7
  • 7
  • 1
    "Note that you must do this in-place without making a copy of the array." – ASDFGerte May 29 '22 at 16:45
  • The problem is you didn't read question carefully. It says that you have to manipulate only with inital array i.e. traditional sorting algorithm – Jaood_xD May 29 '22 at 17:03
  • thx didn't realize filter made a new array – Giovanni May 29 '22 at 17:30
  • The function doesn't return or print anything. It overwrites the reference to the argument. How do you expect LeetCode to check your result? Your function doesn't have any observable side-effects or results. (That would be my first question in an interview) – jabaa May 29 '22 at 17:40
  • instructions said not to return anything – Giovanni May 29 '22 at 17:51
  • Are you able to write a unit test for your function? How could you check the result? You probably should read [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) From the interviewer side, the follow up questions are probably more interesting than your code. – jabaa May 29 '22 at 17:57
  • i checked through the debugger. i understand what you're saying i just thought LC was doing something over my head to test results since the function isn't meant to return anything. i get now that i wasn't supposed to make a new array with filter, amend to it and rewrite the input. thanks for the link ill check it out as well – Giovanni May 29 '22 at 18:09

0 Answers0