Permalink
Browse files

Always declare vars at the beginning of each function

  • Loading branch information...
1 parent bd8605a commit 2da8a8af13d85dd43b1351537227d260606438ce @fhd committed Apr 16, 2012
View
1 TODO.md
@@ -5,7 +5,6 @@
- Hash Map
- Binary Tree
- Clean up
- - Always declare vars at the beginning of a function
- Use the module pattern
- Use Sinon.JS for mocks, stubs and spies in all tests
- Remove unnecessary tests, add missing tests
View
54 public/js/dataStructures/display.js
@@ -1,13 +1,28 @@
-var boxes = [];
+var boxes = [],
+ dataStructures = {
+ init: function(ds) {
+ if (typeof currentAlgorithmFile != "undefined")
+ prettyPrint(); // Prettify
+
+ if (ds instanceof Stack)
+ createStackOperations(ds);
+ else if (ds instanceof Queue)
+ createQueueOperations(ds);
+ else if (ds instanceof LinkedList)
+ createLinkedListOperations(ds);
+
+ setInterval(createDrawFunction($("#canvas")[0], draw), 10);
+ }
+ };
function draw(context, width, height) {
context.clearRect(0, 0, width, height);
context.fillStyle = "rgb(0, 0, 0)";
context.font = "12px sans";
context.textBaseline = "top";
- var padding = 5,
- textPadding = 5;
$.each(boxes, function(_, box) {
+ var padding = 5,
+ textPadding = 5;
context.strokeRect(box.pos + padding, padding, box.width,
box.height);
context.fillText(box.content, box.pos + padding + textPadding,
@@ -16,13 +31,14 @@ function draw(context, width, height) {
}
function moveBoxes(distance, startIndex, callback) {
- var boxesToMove = boxes.slice(startIndex);
+ var boxesToMove = boxes.slice(startIndex),
+ boxMover;
$.each(boxesToMove, function(_, box) {
box.goal = box.pos + distance;
});
- var boxMover = setInterval(function() {
+ boxMover = setInterval(function() {
var finished = true;
$.each(boxesToMove, function(_, box) {
if (typeof box.goal == "undefined")
@@ -73,12 +89,14 @@ function addBox(index, value, callback) {
}
function removeBox(index, callback) {
+ var removedBox;
+
if (boxes.length == 0) {
callback();
return;
}
- var removedBox = boxes.splice(index, 1)[0];
+ removedBox = boxes.splice(index, 1)[0];
if (index < boxes.length)
moveBoxes(-1 * removedBox.width, index, callback);
else
@@ -188,32 +206,16 @@ function createLinkedListOperations(linkedList) {
});
deleteButton.click(function() {
- var value = deleteInput.val();
+ var value = deleteInput.val(),
+ index;
+
if (!value || !linkedList.search(value))
return;
- var index = linkedList.toArray().indexOf(value);
+ index = linkedList.toArray().indexOf(value);
linkedList.delete(linkedList.search(value));
disableOperations();
removeBox(index, enableOperations);
});
}
-
-var dataStructures = (function() {
- return {
- init: function(ds) {
- if (typeof currentAlgorithmFile != "undefined")
- prettyPrint(); // Prettify
-
- if (ds instanceof Stack)
- createStackOperations(ds);
- else if (ds instanceof Queue)
- createQueueOperations(ds);
- else if (ds instanceof LinkedList)
- createLinkedListOperations(ds);
-
- setInterval(createDrawFunction($("#canvas")[0], draw), 10);
- }
- };
-})();
View
4 public/js/dataStructures/linkedList.js
@@ -32,8 +32,8 @@ function LinkedList() {
* It is not part of the original data structure.
*/
this.toArray = function() {
- var array = [];
- var x = head;
+ var array = [],
+ x = head;
while (x != null) {
array.push(x.key);
x = x.next;
View
8 public/js/sorting/bubbleSort.js
@@ -1,8 +1,10 @@
function bubbleSort(array) {
- for (var i = 0; i < array.length; i++) {
- for (var j = array.length; j > i; j--)
+ var i, j, temp;
+
+ for (i = 0; i < array.length; i++) {
+ for (j = array.length; j > i; j--)
if (array[j] < array[j - 1]) {
- var temp = array[j];
+ temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
View
63 public/js/sorting/display.js
@@ -1,31 +1,5 @@
-var array = [];
-for (var i = 0; i < 30; i++)
- array[i] = i + 1;
-
-Array.prototype.shuffle = function() {
- var s = [];
- while (this.length)
- s.push(this.splice(Math.random() * this.length, 1));
- while (s.length)
- this.push(s.pop());
- return this;
-};
-
-function draw(context, width, height) {
- context.clearRect(0, 0, width, height);
- context.fillStyle = "rgb(0, 0, 0)";
- context.font = "12px sans";
- context.textBaseline = "top";
- var barWidth = 5;
- $.each(array, function(index, value) {
- context.fillRect(index * barWidth * 2 + barWidth,
- ((array.length + 1) - value) * barWidth,
- barWidth, barWidth * value);
- });
-};
-
-var sorting = (function() {
- return {
+var array = [],
+ sorting = {
init: function(stack) {
if (typeof currentAlgorithmFile != "undefined") {
prettyPrint(); // Prettify
@@ -36,9 +10,9 @@ var sorting = (function() {
});
$("#sort").click(function() {
- var controls = $("#algorithms, #shuffle, #sort");
+ var controls = $("#algorithms, #shuffle, #sort"),
+ worker = new Worker("js/sorting/worker.js");
controls.attr("disabled", "true");
- var worker = new Worker("js/sorting/worker.js");
worker.onmessage = function(event) {
var data = event.data;
array = data.array;
@@ -55,5 +29,30 @@ var sorting = (function() {
setInterval(createDrawFunction($("#canvas")[0], draw), 10);
}
}
- };
-})();
+ },
+ i;
+
+function draw(context, width, height) {
+ context.clearRect(0, 0, width, height);
+ context.fillStyle = "rgb(0, 0, 0)";
+ context.font = "12px sans";
+ context.textBaseline = "top";
+ $.each(array, function(index, value) {
+ var barWidth = 5;
+ context.fillRect(index * barWidth * 2 + barWidth,
+ ((array.length + 1) - value) * barWidth,
+ barWidth, barWidth * value);
+ });
+};
+
+for (i = 0; i < 30; i++)
+ array[i] = i + 1;
+
+Array.prototype.shuffle = function() {
+ var s = [];
+ while (this.length)
+ s.push(this.splice(Math.random() * this.length, 1));
+ while (s.length)
+ this.push(s.pop());
+ return this;
+};
View
6 public/js/sorting/heapsort.js
@@ -20,16 +20,18 @@ function maxHeapify(array, root) {
}
function buildMaxHeap(array) {
+ var i;
array.heapSize = array.length;
- for (var i = Math.round(array.length / 2); i >= 0; i--) {
+ for (i = Math.round(array.length / 2); i >= 0; i--) {
maxHeapify(array, i);
update(array);
}
}
function heapsort(array) {
+ var i;
buildMaxHeap(array);
- for (var i = array.length - 1; i >= 1; i--) {
+ for (i = array.length - 1; i >= 1; i--) {
exchange(array, 0, i);
array.heapSize--;
maxHeapify(array, 0);
View
8 public/js/sorting/insertionSort.js
@@ -1,7 +1,9 @@
function insertionSort(array) {
- for (var i = 1; i < array.length; i++) {
- var key = array[i];
- for (var j = i - 1; j >= 0 && array[j] > key; j--)
+ var i, key, j;
+
+ for (i = 1; i < array.length; i++) {
+ key = array[i];
+ for (j = i - 1; j >= 0 && array[j] > key; j--)
array[j + 1] = array[j];
array[j + 1] = key;
update(array);
View
10 public/js/sorting/mergeSort.js
@@ -2,22 +2,26 @@ function merge(array, start, middle, end) {
var array1 = array.slice(start, middle),
array2 = array.slice(middle, end),
j = 0,
- k = 0;
- for (var i = start; i < end; i++)
+ k = 0,
+ i;
+
+ for (i = start; i < end; i++)
if (j < array1.length && (k == array2.length || array1[j] <= array2[k]))
array[i] = array1[j++];
else
array[i] = array2[k++];
}
function mergeSort(array, start, end) {
+ var middle;
+
if (start == undefined)
start = 0;
if (end == undefined)
end = array.length;
if (start < end - 1) {
- var middle = Math.floor((start + end + 1) / 2);
+ middle = Math.floor((start + end + 1) / 2);
mergeSort(array, start, middle);
mergeSort(array, middle, end);
merge(array, start, middle, end);
View
9 public/js/sorting/quicksort.js
@@ -7,9 +7,10 @@ function exchange(array, i, j) {
function partition(array, start, end) {
var last = end - 1,
pivot = array[last],
- i = start - 1;
+ i = start - 1,
+ j;
- for (var j = start; j < last; j++)
+ for (j = start; j < last; j++)
if (array[j] <= pivot)
exchange(array, ++i, j);
@@ -18,13 +19,15 @@ function partition(array, start, end) {
}
function quicksort(array, start, end) {
+ var split;
+
if (start == undefined)
start = 0;
if (end == undefined)
end = array.length;
if (start < end - 1) {
- var split = partition(array, start, end);
+ split = partition(array, start, end);
update(array);
quicksort(array, start, split);
quicksort(array, split + 1, end);
View
9 public/js/sorting/randomizedQuicksort.js
@@ -7,9 +7,10 @@ function exchange(array, i, j) {
function partition(array, start, end) {
var last = end - 1,
pivot = array[last],
- i = start - 1;
+ i = start - 1,
+ j;
- for (var j = start; j < last; j++)
+ for (j = start; j < last; j++)
if (array[j] <= pivot)
exchange(array, ++i, j);
@@ -24,13 +25,15 @@ function randomizedPartition(array, start, end) {
}
function randomizedQuicksort(array, start, end) {
+ var split;
+
if (start == undefined)
start = 0;
if (end == undefined)
end = array.length;
if (start < end - 1) {
- var split = randomizedPartition(array, start, end);
+ split = randomizedPartition(array, start, end);
update(array);
randomizedQuicksort(array, start, split);
randomizedQuicksort(array, split + 1, end);
View
8 public/js/sorting/worker.js
@@ -9,15 +9,17 @@ function update(array) {
}
function makeNumeric(array) {
- for (var i = 0; i < array.length; i++)
+ var i;
+ for (i = 0; i < array.length; i++)
array[i] = parseInt(array[i]);
}
onmessage = function(event) {
- var array = event.data.array;
+ var array = event.data.array,
+ algorithm;
makeNumeric(array);
importScripts(event.data.file);
- var algorithm = eval(event.data.functionName);
+ algorithm = eval(event.data.functionName);
algorithm(array);
postMessage({"array": array, finished: true});
}

0 comments on commit 2da8a8a

Please sign in to comment.