currently, I am on a study binge for a couple of front-end job interviews. I do going through a bunch of problems, and I am currently stuck on a problem which is not hard at all but for some reason, I can't get it to work. Maybe you guys can take a look:
The question: Add a print function to the Array Prototype that prints all the contents in the array. ex. [1,2].print() -> 1,2
I wrote this function confidently, knowing it would work, but it didn't:
Array.prototype.print = () => {
let str = ''
for(let i = 0; i < this.length; i++) {
this[i+1] === undefined ? str += this[i] : str += `${this[i]}, `
}
return str
}
console.log([1,2].print())
When I ran the function I got nothing in return. My thinking was this
should have been tied with the array I was working on [1,2]
but it's not. Its tied to the window.
Any idea what I did wrong? The help would be appreciated.
Best, Ayaz