Command Palette

Search for a command to run...

Hectal
Phase 5: Arrays
JavaScript · Exercise 1 of 6Next
Beginner

First and last

Write a function firstAndLast(arr) that returns a new array holding the first and the last item of arr.

firstAndLast([3, 7, 9]) returns [3, 9]. A one-item array gives that item twice: firstAndLast(["a"]) returns ["a", "a"]. An empty array gives [].

Don't change the array you were given.

What Check tests

  1. firstAndLast([3, 7, 9]) returns [3, 9]
  2. A one-item array gives that item twice
  3. An empty array gives []
  4. The original array is not changed

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.