Return the First and Last Elements in an Array

Published by Matt in

Create a function that takes an array of values and returns the first and last values in a new array.

Examples

firstLast([5, 10, 15, 20, 25]) ➞ [5, 25]

firstLast(["edabit", 13, null, false, true]) ➞ ["edabit", true]

firstLast([undefined, 4, "6", "hello", null]) ➞ [undefined, null]

Notes

  • Test input will always contain a minimum of two elements within the array.
  • Don't forget to return the result.
  • If you get stuck on a challenge, find help in the Resources tab.
  • If you're really stuck, unlock solutions in the Solutions tab.
Watch a quick demo on how Edabit works.