# Bubblesort & prototypes

Hi. I am trying to complete this lesson on bubble sorting. The problem is we have to use prototyping. I’m not sure if I am implementing it correctly. The items do not sort correctly with the func bubbleSort.

console.log("Project"); // 1. Implement the ArrayList using the prototype. function ArrayList() { this.items = []; } ArrayList.prototype.insert = function(item) { this.items.push(item); } ArrayList.prototype.toString = function() { return this.items.join(); } // 2. Complete the createNonSortedRandomArray(size) below by returning an // ArrayList with the numbers 1, 2, ..., size in random order. // NOTE: there are multiple ways to achieve this. function createNonSortedRandomArray(size) { var elements = size; var array = new ArrayList(); for (var i=elements; i>0; i--) { array.insert(i); } return array; } //Implemented bubbleSort function for self instruction. ArrayList.prototype.bubbleSort = function() { var length = this.items.length; for (var i=0; i<length; i++) { for (var j=0; j<length-1; j++) { if (array[j] > array[j+1]) { this.swap(array, j, j+1); } } } } ArrayList.prototype.swap = function(array, index1, index2) { var aux = array[index1]; array[index1] = array[index2]; array[index2] = aux; } var array = new createNonSortedRandomArray(6); console.log(array.toString()); array.bubbleSort(); console.log(array.toString());
``````ArrayList.prototype.bubbleSort = function() {
var length = this.items.length;
for (var i=0; i<length; i++) {
for (var j=0; j<length-1; j++) {
if (array[j] > array[j+1]) {
this.swap(array, j, j+1);
}
}
}
}
``````

Where does `i` fit into all this?