Start learning free

LESSON 118 OF 671 · ARRAYS - CHALLENGES

First n Items in JavaScript: Copying with slice

Practice Read the full explanation

You get an array of integers nums and a number n. Return an array with the first n numbers of nums.

Keep them in the same order. When n is 0, return an empty array.

 

Example 1:

Input: nums = [3, 8, 1, 9, 4], n = 3
Output: [3, 8, 1]
Explanation: The first 3 numbers are 3, 8 and 1.
The 9 and the 4 are left out.

Example 2:

Input: nums = [5, 6, 7], n = 0
Output: []
Explanation: No numbers are taken, so the result is empty.

Example 3:

Input: nums = [2, 4], n = 2
Output: [2, 4]
Explanation: n is the length of nums, so every number is kept.

 

Constraints:

  • nums has 1 to 10 numbers.
  • Every number is an integer from -1000 to 1000.
  • n is from 0 to the length of nums.
STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · ARRAYS - CHALLENGES

    First N Items in JavaScript, explained

    Problem overview

    The method firstN receives an array of integers nums and a number n. It returns a new array with the first n numbers of nums, in the same order. When n is 0 the result is an empty array. The constraints say nums has 1 to 10 numbers and n is between 0 and the length of nums, so n never asks for more numbers than exist.

    Three examples show the range of cases:

    • nums = [3, 8, 1, 9, 4] and n = 3 gives [3, 8, 1]. The 9 and the 4 are left out.
    • nums = [5, 6, 7] and n = 0 gives []. No numbers are taken.
    • nums = [2, 4] and n = 2 gives [2, 4]. Here n equals the length, so every number is kept.

    Approach

    You could build the result by hand: make an empty array, loop n times and push nums[i] each time. That works, but JavaScript already has a method for copying a part of an array. slice(start, end) copies the items from index start up to, but not including, index end, into a new array.

    Because the end is left out, nums.slice(0, n) copies the items at indexes 0 to n - 1. That is exactly n items, the first n. The start is 0 because we begin at the front.

    Step by step

    1. Copy nums from index 0 up to index n with slice, and store the copy in a variable named result, declared with let.
    2. Return result.

    The edge cases need no extra code. With n = 0 the start and end are both 0, so the range is empty and slice returns []. With n equal to the length, the copy runs through the last item and returns every number.

    The solution in JavaScript

    class Solution {
        firstN(nums, n) {
            let result = nums.slice(0, n);
            return result;
        }
    }
    

    Details worth knowing

    The result is a new array, so the caller's nums stays as it was. Pushing a new number onto the result does not touch the original: with nums = [3, 8, 1, 9, 4], a result of [3, 8, 1] that gets a 0 pushed becomes [3, 8, 1, 0], while nums is still [3, 8, 1, 9, 4].

    The method returns the array itself, not text, and numbers stay numbers. If you print the arrays in your own file with console.log, they show as [ 3, 8, 1 ]. The lesson's tests compare the values.

    Testing

    Running the method on the three examples gives these results:

    [ 3, 8, 1 ]
    []
    [ 2, 4 ]
    

    Also try n equal to 1, which should give an array with only the first number, and an array with one number and n = 1, which should give that same single number back.

    Edge cases and common mistakes

    • Off by one with the end index. The end of slice is not included. nums.slice(0, n - 1) returns one number too few, and nums.slice(0, n + 1) returns one too many when there are enough numbers.
    • Using splice. nums.splice(0, n) also returns the first n numbers, but it removes them from nums. Use slice when the original must stay.
    • Forgetting to return. A method without a return gives back undefined, so the tests would see no array.
    • Special-casing zero. An if (n === 0) branch is not needed. The empty range already gives an empty array.
    Keep going with JavaScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free