-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises.js
100 lines (77 loc) · 2.15 KB
/
exercises.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
96
97
98
99
100
"use strict";
function Clock () {
};
Clock.TICK = 5000;
Clock.prototype.printTime = function () {
var hours = this.date.getHours();
var minutes = this.date.getMinutes();
var seconds = this.date.getSeconds();
console.log(hours + " : " + minutes + " : " + seconds);
};
Clock.prototype.run = function () {
this.date = new Date();
this.printTime();
window.setTimeout(function () {
this._tick();
}.bind(this), Clock.TICK
);
}
Clock.prototype._tick = function () {
this.date.setTime(this.date.getTime() + Clock.TICK);
this.run();
};
var clock = new Clock();
// clock.run();
//########################
var readline = require('readline');
var reader = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function addNumbers (sum, numsLeft, completionCallback) {
if (numsLeft > 0){
reader.question("What is your number?", function (answer) {
sum += parseInt(answer);
console.log("Temporary sum: " + sum );
addNumbers(sum, numsLeft - 1, completionCallback)
});
} else {
completionCallback(sum);
}
};
addNumbers(0, 3, function (sum) {
console.log("Total Sum: " + sum);
});
// it's okay if this part is magic; it just says that we want to
// 1. output the prompt to the standard output (console)
// 2. read input from the standard input (again, console)
console.log("Last program line");
var readline = require("readline");
var reader = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askIfGreaterThan(el1, el2, callback) {
reader.question("Is " + el1 + " greater than " + el2 + " ?", function (answer) {
if (answer === 'yes') {
callback(true);
} else {
callback(false);
}
};
};
function innerBubbleSortLoop(arr, i, madeAnySwaps, outerBubbleSortLoop) {
if (i == arr.length - 1) {
outerBubbleSortLoop(madeAnySwaps);
} else {
askIfGreaterThan(arr[i], arr[i+1], function(boolean) {
if (boolean === true) {
var store = arr[i];
arr[i] = arr[i+1];
arr[i+1] = store;
}
};)
}
};
function absurdBubbleSort (arr, sortCompletionCallback) {
};