Skip to content
Browse files

Declare vars in client-side JS late

  • Loading branch information...
1 parent 2809bf9 commit 3f4d8fe532be39cb20e55b74da851f034a8cdf03 @fhd committed Jul 2, 2012
View
25 public/js/dataStructures/display.js
@@ -3,24 +3,23 @@ var dataStructures = {};
(function(dataStructures, display) {
function draw(context, width, height) {
$.each(dataStructures.boxes, function(_, box) {
- var padding = 5,
- textPadding = 5;
+ var padding = 5;
context.strokeRect(box.pos + padding, padding, box.width,
box.height);
+ var textPadding = 5;
context.fillText(box.content, box.pos + padding + textPadding,
padding + textPadding);
});
}
function moveBoxes(distance, startIndex, callback) {
- var boxesToMove = dataStructures.boxes.slice(startIndex),
- boxMover;
+ var boxesToMove = dataStructures.boxes.slice(startIndex);
$.each(boxesToMove, function(_, box) {
box.goal = box.pos + distance;
});
- boxMover = setInterval(function() {
+ var boxMover = setInterval(function() {
var finished = true;
$.each(boxesToMove, function(_, box) {
if (typeof box.goal === "undefined")
@@ -61,11 +60,11 @@ var dataStructures = {};
.attr("id", "pushInput").attr("size", "3"),
pushButton = $("<input/>").attr("type", "button")
.attr("id", "pushButton").attr("value", "Push"),
+ pushOperation = $("<div/>").attr("class", "operation")
+ .append(pushInput).append(pushButton),
popButton = $("<input/>").attr("type", "button")
.attr("id", "popButton").attr("value", "Pop"),
popOutput = $("<label/>").attr("id", "popOutput"),
- pushOperation = $("<div/>").attr("class", "operation")
- .append(pushInput).append(pushButton),
popOperation = $("<div/>").attr("class", "operation")
.append(popButton).append(popOutput);
@@ -93,11 +92,11 @@ var dataStructures = {};
.attr("id", "enqueueInput").attr("size", "3"),
enqueueButton = $("<input/>").attr("type", "button")
.attr("id", "enqueueButton").attr("value", "Enqueue"),
+ enqueueOperation = $("<div/>").attr("class", "operation")
+ .append(enqueueInput).append(enqueueButton),
dequeueButton = $("<input/>").attr("type", "button")
.attr("id", "dequeueButton").attr("value", "Dequeue"),
dequeueOutput = $("<label/>").attr("id", "dequeueOutput"),
- enqueueOperation = $("<div/>").attr("class", "operation")
- .append(enqueueInput).append(enqueueButton),
dequeueOperation = $("<div/>").attr("class", "operation")
.append(dequeueButton).append(dequeueOutput);
@@ -126,12 +125,12 @@ var dataStructures = {};
.attr("id", "insertInput").attr("size", "3"),
insertButton = $("<input/>").attr("type", "button")
.attr("id", "insertButton").attr("value", "Insert"),
+ insertOperation = $("<div/>").attr("class", "operation")
+ .append(insertInput).append(insertButton),
deleteButton = $("<input/>").attr("type", "button")
.attr("id", "deleteButton").attr("value", "Delete"),
deleteInput = $("<input/>").attr("type", "text")
.attr("id", "deleteInput").attr("size", "3"),
- insertOperation = $("<div/>").attr("class", "operation")
- .append(insertInput).append(insertButton),
deleteOperation = $("<div/>").attr("class", "operation")
.append(deleteInput).append(deleteButton);
@@ -204,14 +203,12 @@ var dataStructures = {};
}
dataStructures.removeBox = function(index, callback) {
- var removedBox;
-
if (!dataStructures.boxes.length) {
callback();
return;
}
- removedBox = dataStructures.boxes.splice(index, 1)[0];
+ var removedBox = dataStructures.boxes.splice(index, 1)[0];
if (index < dataStructures.boxes.length)
moveBoxes(-1 * removedBox.width, index, callback);
else
View
4 public/js/dataStructures/queue.js
@@ -1,5 +1,7 @@
function Queue() {
- var head = 0, tail = 0, elements = [];
+ var head = 0,
+ tail = 0,
+ elements = [];
this.enqueue = function(e) {
elements[tail] = e;
View
3 public/js/dataStructures/stack.js
@@ -1,5 +1,6 @@
function Stack() {
- var top = 0, elements = [];
+ var top = 0,
+ elements = [];
this.stackEmpty = function() {
return (top === 0);
View
6 public/js/sorting/bubbleSort.js
@@ -1,8 +1,6 @@
function bubbleSort(array) {
- var i, j, temp;
-
- for (i = 0; i < array.length; i++) {
- for (j = array.length; j > i; j--)
+ for (var i = 0; i < array.length; i++) {
+ for (var j = array.length; j > i; j--)
if (array[j] < array[j - 1]) {
temp = array[j];
array[j] = array[j - 1];
View
27 public/js/sorting/display.js
@@ -1,18 +1,9 @@
var sorting = {};
(function(sorting, display) {
- var array = [], i;
-
- function draw(context, width, height) {
- $.each(array, function(index, value) {
- var barWidth = 5;
- context.fillRect(index * barWidth * 2 + barWidth,
- ((array.length + 1) - value) * barWidth,
- barWidth, barWidth * value);
- });
- };
+ var array = [];
- for (i = 0; i < 30; i++)
+ for (var i = 0; i < 30; i++)
array[i] = i + 1;
Array.prototype.shuffle = function() {
@@ -34,9 +25,10 @@ var sorting = {};
});
$("#sort").click(function() {
- var controls = $("#algorithms, #shuffle, #sort"),
- worker = new Worker("js/sorting/worker.js");
+ var controls = $("#algorithms, #shuffle, #sort");
controls.attr("disabled", "true");
+
+ var worker = new Worker("js/sorting/worker.js");
worker.onmessage = function(event) {
var data = event.data;
array = data.array;
@@ -50,6 +42,15 @@ var sorting = {};
});
});
+ function draw(context, width, height) {
+ $.each(array, function(index, value) {
+ var barWidth = 5;
+ context.fillRect(index * barWidth * 2 + barWidth,
+ ((array.length + 1) - value) * barWidth,
+ barWidth, barWidth * value);
+ });
+ };
+
display.init(draw);
};
})(sorting, display);
View
8 public/js/sorting/heapsort.js
@@ -9,7 +9,7 @@ var heapsort = (function() {
var left = (root === 0) ? 1 : 2 * root,
right = left + 1,
largest = (left < array.heapSize && array[left] > array[root])
- ? left : root;
+ ? left : root;
if (right < array.heapSize && array[right] > array[largest])
largest = right;
@@ -21,18 +21,16 @@ var heapsort = (function() {
}
function buildMaxHeap(array) {
- var i;
array.heapSize = array.length;
- for (i = Math.round(array.length / 2); i >= 0; i--) {
+ for (var i = Math.round(array.length / 2); i >= 0; i--) {
maxHeapify(array, i);
update(array);
}
}
return function(array) {
- var i;
buildMaxHeap(array);
- for (i = array.length - 1; i >= 1; i--) {
+ for (var i = array.length - 1; i >= 1; i--) {
exchange(array, 0, i);
array.heapSize--;
maxHeapify(array, 0);
View
8 public/js/sorting/insertionSort.js
@@ -1,9 +1,7 @@
function insertionSort(array) {
- var i, key, j;
-
- for (i = 1; i < array.length; i++) {
- key = array[i];
- for (j = i - 1; j >= 0 && array[j] > key; j--)
+ for (var i = 1; i < array.length; i++) {
+ var key = array[i];
+ for (var j = i - 1; j >= 0 && array[j] > key; j--)
array[j + 1] = array[j];
array[j + 1] = key;
update(array);
View
9 public/js/sorting/mergeSort.js
@@ -3,10 +3,9 @@ var mergeSort = (function() {
var array1 = array.slice(start, middle),
array2 = array.slice(middle, end),
j = 0,
- k = 0,
- i;
+ k = 0;
- for (i = start; i < end; i++)
+ for (var i = start; i < end; i++)
if (j < array1.length
&& (k === array2.length || array1[j] <= array2[k]))
array[i] = array1[j++];
@@ -15,15 +14,13 @@ var mergeSort = (function() {
}
return function(array, start, end) {
- var middle;
-
if (typeof start === "undefined")
start = 0;
if (typeof end === "undefined")
end = array.length;
if (start < end - 1) {
- middle = Math.floor((start + end + 1) / 2);
+ var 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
@@ -8,10 +8,9 @@ var quicksort = (function() {
function partition(array, start, end) {
var last = end - 1,
pivot = array[last],
- i = start - 1,
- j;
+ i = start - 1;
- for (j = start; j < last; j++)
+ for (var j = start; j < last; j++)
if (array[j] <= pivot)
exchange(array, ++i, j);
@@ -20,15 +19,13 @@ var quicksort = (function() {
}
return function(array, start, end) {
- var split;
-
if (typeof start === "undefined")
start = 0;
if (typeof end === "undefined")
end = array.length;
if (start < end - 1) {
- split = partition(array, start, end);
+ var split = partition(array, start, end);
update(array);
quicksort(array, start, split);
quicksort(array, split + 1, end);
View
9 public/js/sorting/randomizedQuicksort.js
@@ -8,10 +8,9 @@ var randomizedQuicksort = (function() {
function partition(array, start, end) {
var last = end - 1,
pivot = array[last],
- i = start - 1,
- j;
+ i = start - 1;
- for (j = start; j < last; j++)
+ for (var j = start; j < last; j++)
if (array[j] <= pivot)
exchange(array, ++i, j);
@@ -26,15 +25,13 @@ var randomizedQuicksort = (function() {
}
return function(array, start, end) {
- var split;
-
if (typeof start === "undefined")
start = 0;
if (typeof end === "undefined")
end = array.length;
if (start < end - 1) {
- split = randomizedPartition(array, start, end);
+ var split = randomizedPartition(array, start, end);
update(array);
randomizedQuicksort(array, start, split);
randomizedQuicksort(array, split + 1, end);
View
8 public/js/sorting/worker.js
@@ -1,7 +1,6 @@
(function() {
function makeNumeric(array) {
- var i;
- for (i = 0; i < array.length; i++)
+ for (var i = 0; i < array.length; i++)
array[i] = parseInt(array[i]);
}
@@ -16,11 +15,10 @@
}
onmessage = function(event) {
- var array = event.data.array,
- algorithm;
+ var array = event.data.array;
makeNumeric(array);
importScripts(event.data.file);
- algorithm = eval(event.data.functionName);
+ var algorithm = eval(event.data.functionName);
algorithm(array);
postMessage({"array": array, finished: true});
};

0 comments on commit 3f4d8fe

Please sign in to comment.
Something went wrong with that request. Please try again.