JavaScript · Level 1 Data

Arrays

Keep values in order in an array, add to it with push and read the last item the right way with at(-1).

  • [a, b, c] creates an array; positions start at 0
  • push, length and includes
  • arr[-1] is undefined; use arr.at(-1)

An array keeps values in order, starting at position 0. The trap: arr[-1] does not count from the end, it gives undefined. Use arr.at(-1).

const tabs = ["Home", "Blog"];
tabs.push("Contact");    // 3 (new length); tabs is now ["Home", "Blog", "Contact"]
tabs.length;             // 3
tabs.includes("Blog");   // true
tabs[-1];                // undefined
tabs.at(-1);             // "Contact"

Write firstAndLast(arr) that returns a new array with the first and last items, e.g. firstAndLast([3, 7, 9]) returns [3, 9].

solution.js
Test Suites Pending
Output