Overview
Implement at(array, index) that returns the element at the given index, supporting negative indices.
Examples
at([1,2,3], 0); // 1 at([1,2,3], -1); // 3
Solution
Reveal solution
function at(array, index) {
const i = index < 0 ? array.length + index : index;
return array[i];
}array-prototype-at.js
Array.prototype.at
easycodingJavaScriptArrays
Overview
Implement at(array, index) that returns the element at the given index, supporting negative indices.
Examples
at([1,2,3], 0); // 1 at([1,2,3], -1); // 3
Solution
Reveal solution
function at(array, index) {
const i = index < 0 ? array.length + index : index;
return array[i];
}NameTopicDifficulty
103 of 103 problems