Skip to main content

JavaScript Array Access — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Array Access lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Read the first element

    easy10 min

    Show the first element of an array of fruit names.

  2. 2

    Read the last element

    easy10 min

    Show the last element using the array length.

  3. 3

    Use at with a negative index

    easy10 min

    Show the second-to-last element using at().

  4. 4

    Replace an element

    easy10 min

    Replace the middle element with Cherry and show the whole list joined by commas.

  5. 5

    Read from a nested array

    medium10 min

    Show the value in the second row, first column of a grid.

Find the bug(5)

  1. 1

    Fix the off-by-one first element

    easy8 min

    Counting starts at 0, not 1.

  2. 2

    Fix the last element past the end

    easy8 min

    Using length as an index reads one place too far.

  3. 3

    Fix the index in quotes

    easy8 min

    A string is treated as a property name, not a position.

  4. 4

    Fix at used with a positive one

    easy8 min

    at(1) is the second element, not the last.

  5. 5

    Fix the single bracket on a grid

    easy8 min

    One pair of brackets gives a whole row, not a value.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace