-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.js
95 lines (80 loc) · 2.18 KB
/
callbacks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function Clock () {
}
Clock.TICK = 5000;
Clock.prototype.printTime = function () {
console.log(
this.currentTime.getHours() +
':' + this.currentTime.getMinutes() +
':' + this.currentTime.getSeconds()
);
};
Clock.prototype.run = function () {
this.currentTime = new Date();
this.printTime();
setInterval(this._tick.bind(this), Clock.TICK);
};
Clock.prototype._tick = function () {
this.currentTime.setTime(this.currentTime.getTime() + Clock.TICK);
this.printTime();
};
// var clock = new Clock();
// clock.run();
var readline = require('readline');
var reader = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var addNumbers = function (sum, numsLeft, completionCallback) {
if (numsLeft > 0) {
reader.question("Gimme a number! ", function (response) {
var parsedNum = parseInt(response);
sum += parsedNum;
console.log(sum);
addNumbers(sum, --numsLeft, completionCallback)
});
} else {
completionCallback(sum);
}
};
// addNumbers(0, 3, function (sum) {
// console.log("Total Sum: " + sum);
// reader.close();
// });
var absurdBubbleSort = function (arr, sortCompletionCallback) {
var outerBubbleSortLoop = function (madeAnySwaps) {
if (madeAnySwaps) {
innerBubbleSortLoop(arr, 0, false, outerBubbleSortLoop);
} else {
sortCompletionCallback(arr);
}
}
outerBubbleSortLoop(true);
};
var askIfLessThan = function (el1, el2, callback) {
reader.question("Is " + el1 + " smaller than " + el2, function (response) {
if (response === "y") {
callback(true);
} else {
callback(false);
}
});
};
var innerBubbleSortLoop = function (arr, i, madeAnySwaps, outerBubbleSortLoop) {
if (i < arr.length - 1) {
askIfLessThan(arr[i], arr[i+1], function (isLessThan) {
if (!isLessThan) {
var temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
madeAnySwaps = true;
}
innerBubbleSortLoop(arr, ++i, madeAnySwaps, outerBubbleSortLoop);
});
} else {
outerBubbleSortLoop(madeAnySwaps);
}
}
absurdBubbleSort([3, 2, 1], function (arr) {
console.log("Sorted array: " + JSON.stringify(arr));
reader.close();
});