Return the First and Last Elements in an Array

Published by Helen Yu in

Create a function that takes an array of strings and return the first and last elements as a new array.

Examples

firstLast(["one", "two"]) ➞ ["one", "two"]

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

firstLast(["zero"]) ➞ ["zero"]

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.