Complete the code to create a simple array representing a list of fruits.
const fruits = ['apple', [1], 'banana']; console.log(fruits);
The array should contain fruit names. 'orange' fits correctly between 'apple' and 'banana'.
Complete the code to link nodes in a singly linked list.
function Node(value) {
this.value = value;
this.next = [1];
}
const first = new Node(1);
const second = new Node(2);
first.next = second;
console.log(first.next.value);When creating a new node, its next pointer should start as null to indicate the end of the list.
Fix the error in the tree node constructor to correctly assign children as an empty array.
function TreeNode(value) {
this.value = value;
this.children = [1];
}
const root = new TreeNode('root');
console.log(root.children.length);Children should be an empty array to hold multiple child nodes in a tree structure.
Fill both blanks to create a linked list node and link it to the next node.
function ListNode(value) {
this.value = value;
this.next = [1];
}
const node1 = new ListNode(10);
const node2 = new ListNode(20);
node1.next = [2];
console.log(node1.next.value);When creating a new node, next is initially null. Then node1.next is set to node2 to link them.
Fill all three blanks to create a tree node, add a child, and print the child's value.
function TreeNode(value) {
this.value = value;
this.children = [1];
}
const parent = new TreeNode('parent');
const child = new TreeNode('child');
parent.children.[2](child);
console.log(parent.children[[3]].value);Children is an empty array. Use push to add the child. The first child's index is 0.