I'm trying to create a method on a LinkedList constructor that can takes in a function and call that on each node item. This is what I have so far:
this is my constructor:
function LinkedList() {
this.head = this.tail = null;
}
I have a addToTail method that I tested and works:
LinkedList.prototype.addToTail = function (item) {
let newNode = new ListNode(item);
if (this.tail) {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
} else {
this.head = newNode;
this.tail = this.head;
}
return this;
};
where:
function ListNode(item, prev, next) {
this.item = item;
this.next = next || null;
this.prev = prev || null;
}
Now I'm trying to call a function on each node item:
LinkedList.prototype.forEach = function (iterator) {
return this.map(iterator (item))
Can anyone explain to me why my forEach returns a []? Also i tried console.log(this) and also got []. What would be the best way to approach this? Thank you!
LinkedList.prototype.map. Since you (incorrectly) forward there, it should be obvious why it returns an array.