Skip to content

Commit 71a5a06

Browse files
committed
When launching index.js it runs scripts configured in there
1 parent 57151f8 commit 71a5a06

File tree

6 files changed

+49
-36
lines changed

6 files changed

+49
-36
lines changed

hanoi-towers.js renamed to algorithms/hanoi-towers.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Hanoi {
1+
export default class Hanoi {
22

33
constructor(discs) {
44
this.towerA = discs;
@@ -35,11 +35,3 @@ class Hanoi {
3535
}
3636
}
3737

38-
const theTower = new Hanoi([12, 9, 8, 7, 4, 3, 2, 1]);
39-
40-
theTower.tracker = (...towers) => {
41-
towers.forEach(tower => console.log(tower));
42-
console.log('----------------------------');
43-
};
44-
45-
theTower.assemble();

algorithms/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Hanoi from './hanoi-towers';
2+
3+
const theTower = new Hanoi([12, 9, 8, 7, 4, 3, 2, 1]);
4+
5+
theTower.tracker = (...towers) => {
6+
towers.forEach(tower => console.log(tower));
7+
console.log('----------------------------');
8+
};
9+
10+
theTower.assemble();

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require('import-export');
2+
3+
console.log('Launching scripts...');
4+
5+
require('./sorting/index');
6+
require('./algorithms/index');

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "A set of scripts with implementations of various data structures and algorithms in JavaScript.",
55
"main": "index.js",
66
"scripts": {
7+
"start": "node index.js",
78
"test": "echo \"Error: no test specified\" && exit 1"
89
},
910
"repository": {

sorting/bubble-sort.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,36 @@
33
* It has a running time of O(n²) in the worst case.
44
* It is used mostly when learning algorithms.
55
*/
6-
const theArray = [72, 14, 34, 5, 24, 14, 53, 61, 38, 110, 39];
6+
export default class BubbleSort {
7+
checkIfSwap(array, i, j) {
8+
let x = array[i];
9+
let y = array[j];
710

8-
const checkIfSwap = (array, i, j) => {
9-
x = array[i];
10-
y = array[j];
11-
12-
if (x > y) {
13-
temp = x;
14-
x = y;
15-
array[i] = x;
16-
array[j] = temp;
17-
console.log(`Swapped! ${x} -> ${temp}`);
11+
if (x > y) {
12+
let temp = x;
13+
x = y;
14+
array[i] = x;
15+
array[j] = temp;
16+
console.log(`Swapped! ${x} -> ${temp}`);
17+
}
1818
}
19-
}
20-
const bubbleSort = arrayToSort => {
21-
const length = arrayToSort.length;
22-
let temp, x, y;
2319

24-
for (var i = 0; i < length - 1; i++) {
25-
console.log(`previous order: ${arrayToSort}`);
20+
sort(arrayToSort) {
21+
const length = arrayToSort.length;
22+
let temp, x, y;
23+
24+
for (var i = 0; i < length - 1; i++) {
25+
console.log(`previous order: ${arrayToSort}`);
2626

27-
for (var j = i + 1; j < length; j++) {
28-
console.log(`comparing ${arrayToSort[i]} against ${arrayToSort[j]} at position ${j}`);
29-
checkIfSwap(arrayToSort, i, j);
27+
for (var j = i + 1; j < length; j++) {
28+
console.log(`comparing ${arrayToSort[i]} against ${arrayToSort[j]} at position ${j}`);
29+
this.checkIfSwap(arrayToSort, i, j);
30+
}
31+
32+
console.log(`new order: ${arrayToSort}`);
3033
}
3134

32-
console.log(`new order: ${arrayToSort}`);
35+
return arrayToSort;
3336
}
37+
}
3438

35-
return arrayToSort;
36-
};
37-
38-
console.log(`original array: ${theArray}`);
39-
const sortedArray = bubbleSort(theArray);
40-
console.log(`sorted array: ${sortedArray}`);

sorting/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import BubbleSort from './bubble-sort';
2+
const theArray = [72, 14, 34, 5, 24, 14, 53, 61, 38, 110, 39];
3+
4+
console.log(`original array: ${theArray}`);
5+
const sortedArray = new BubbleSort().sort(theArray);
6+
console.log(`sorted array: ${sortedArray}`);

0 commit comments

Comments
 (0)