Ad
  • Custom User Avatar

    const arrLen = result.length; // one time length calculation
    while(arrLen < n) {} // Don't need to calculate array length, just check for condition
    while(result.length < n) {} // calculate array length on every loop, then check for condition

  • Custom User Avatar

    Definately not following the best practice around: 1) Creating pure function. 2) State mutation (arguments in this case). 3) Creating side effects.
    Also, returning a new array (return signature.slice) after mutating the original arrary dones not make sense. Doing this before and running the for loop on the copy array and then returning it, would have made this solution perfect !!