sorry, i tried to find a reference in the course but couldn’t, i’m hoping someone can give me a quick reason:
why can’t i just use:
function LinkedList() {
this.head = null;
this.size = 0;
(and then this.head
(& this.length/size
) as references in the code)
instead of that given below?
it doesn’t work without the functions and initializations seperated.
Your code so far
function LinkedList() {
let length = 0;
let head = null;
this.head = () => head;
this.size = () => length;
function Node(element) {
this.element = element;
this.next = null;
}
this.add = element => {
const node = new Node(element);
if (head) {
let current = head;
while (current.next !== null) {
current = current.next;
}
current.next = node;
}
else {
head = node;
}
length++;
};
}
let list = new LinkedList()
//console.log(list)
//console.log(list.length())
list.add('elbow')
//console.log(list.length())
//console.log(list.size)
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36
Challenge: Create a Linked List Class
Link to the challenge: