Array method .at() is a new ES 2022 feature that takes in the index an element in an array and returns the value of it (so the element itself). I am trying to implement .at() but for some reason it keeps logging the wrong value. Can someone please help me understand why?
This is how .at works. So basically this:
const arr = ["JavaScript", "HTML", "ES2022", "CSS", "Chrome", "W3"];
console.log(arr[1]) // output: HTML
Is the same as this:
console.log(arr.at(1)) //output: HTML
Here is my code implementation: It keeps outputting 'JavaScript' but I am expecting 'HTML'
const arr = ["JavaScript", "HTML", "ES2022", "CSS", "Chrome", "W3"];
Array.prototype.myAt = function (index) {
for (let index = 0; index < this.length; index++) {
return this[index];
}
};
let res = arr.myAt(1);
log(res); //output: JavaScript