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
firstAndLast([3, 7, 9])returns[3, 9]- A one-item array gives that item twice
- An empty array gives
[] - 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.