Frontend
Back to frontend track
Deprecated

Overview

JS Polyfill: Array.at() Method

The Array.at() method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

Practice

Code as you read.

Use the live workspace to test the prompt.

JavaScript polyfill workspace

Best in a separate tab.

Open StackBlitz

Question

Back to the future! - Array edition.

Polyfills are a way to add functionality to the JavaScript language by implementing methods that are not natively supported. This is useful for older browsers that do not support newer features of the language. Also, they are interview questions that are commonly asked in JavaScript interviews. In this exercise, you will implement a polyfill for the Array.at() method.

Objectives

  • Implement a polyfill for the Array.at() method.
  • The method should work for both positive and negative integers.
  • It should return undefined for out-of-bound indices.

Tests

Consider writing tests for the following scenarios:

  • The method returns the correct item for a positive index.
  • The method returns the correct item for a negative index.
  • The method returns undefined for an index that is out of the array's bounds.

You can also do a TDD approach to this problem if you prefer.

Resources to Refer