Mental Model
Adding a new item to the end of a list means placing it right after the last item.
Analogy: Imagine a line of people waiting to buy tickets. When a new person arrives, they stand at the very end of the line.
[1] -> [2] -> [3] -> null
[1] -> [2] -> [3] -> null
[1] -> [2] -> [3] -> null
[1] -> [2] -> [3] -> [4] -> null
[1] -> [2] -> [3] -> [4] -> null
[1] -> [2] -> [3] -> [4] -> null
class ArrayList { constructor() { this.array = []; } insertAtEnd(value) { // Add value at the end of the array this.array[this.array.length] = value; } print() { // Print array elements with arrows let result = ''; for (let i = 0; i < this.array.length; i++) { result += this.array[i]; if (i !== this.array.length - 1) { result += ' -> '; } } result += ' -> null'; console.log(result); } } // Driver code const list = new ArrayList(); list.array = [1, 2, 3]; list.insertAtEnd(4); list.print();
this.array[this.array.length] = value;for (let i = 0; i < this.array.length; i++) {result += this.array[i];if (i !== this.array.length - 1) { result += ' -> '; }this.array[this.array.length] = value;
this.array[this.array.length] = value;
push() method do when used on a JavaScript array?push()push() method is designed to add elements to the end of an array.shift() remove elements, sort() orders elements, and reverse() changes order, but push() only adds at the end.push() adds elements at end [OK]5 at the end of array arr?push() to add elements at the end.add(), insertEnd(), and append() are not valid JavaScript array methods.let arr = [1, 2, 3]; arr.push(4); console.log(arr);
push(4) adds 4 at the end.10 at the end of array nums:let nums = [1, 2, 3]; nums.push = 10; console.log(nums);
nums.push instead of calling nums.push(10).arr? let arr = [10]; arr.push(20); arr.push(30, 40); arr.push();
arr.push(20) adds 20 -> [10, 20]. arr.push(30, 40) adds both 30 and 40 -> [10, 20, 30, 40]. arr.push() with no arguments adds nothing.Pass the test and this pattern moves unknown