Code Lab

Remove by index

EasyArrays7 min

Write a function removeAt(items, index) that returns a new array without the element at index index. Do not change the original array and do not use splice. If the array has no such index, return an unchanged copy of it.

Examples

removeAt([10, 20, 30, 40], 1)

Returns [ 10, 30, 40 ]

removeAt([1, 2, 3], 3)

Returns [ 1, 2, 3 ]

The indexes here are 0, 1 and 2.
removeAt(['a', 'b'], -1)

Returns [ 'a', 'b' ]

The array has no index -1.

Asked at Microsoft · Source

Loading the editor...
  1. InputremoveAt([ 10, 20, 30, 40 ], 1)
    Expected[ 10, 30, 40 ]
  2. InputremoveAt([ 1, 2, 3 ], 3)
    Expected[ 1, 2, 3 ]
  3. InputremoveAt([ 'a', 'b' ], -1)
    Expected[ 'a', 'b' ]
  4. The original array stays the same

Hidden tests run on Submit.