-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (65 loc) · 2.37 KB
/
index.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
const readline = require("readline");
const arrayHelper = require("./src/helpers/array-helper");
const binarySearch = require('./src/search/binary-search');
const insertionSort = require('./src/sort/insertion-sort');
const quickSort = require('./src/sort/quick-sort')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let array = null;
let searchedValue = null;
async function generateAndSortArray(size) {
let array = await generateArray(size);
return sortArray(array);
}
async function generateArray(size) {
console.log('generationg a random array of', size, 'items...');
const array = await arrayHelper.generateRandomArrayOfNumbers(size);
console.log('generated array:', array, 'length:', array.length);
return array;
}
async function sortArray(array) {
console.log('sorting the generated array...');
const sortedArray = await quickSort.sort(array)
console.log('array afte sorting', sortedArray, 'length:', sortedArray.length);
return sortedArray;
}
function findIndexValueInArray(value, array) {
const index = binarySearch.findIndexOfNumberInSortedArray(value, array);
if(index >= 0) {
console.log(value, 'found at index', index);
} else {
console.error(value, 'not found');
}
}
doFirstQuestion = () => {
rl.question("Lets create a random array. Please enter how many items it will have \n", async function(arraySizeInput) {
const arraySizeAsNumber = parseInt(arraySizeInput);
if(isNaN(arraySizeAsNumber)) {
console.error(arraySizeInput, 'is not a valid number');
rl.close();
}
array = await generateAndSortArray(arraySizeAsNumber);
doNextQuestion();
});
}
doNextQuestion = async () => {
rl.question("\n\nEnter a number to be found in the array\n", function(searchedValueInput) {
const searchedValueAsNumber = parseInt(searchedValueInput);
if(isNaN(searchedValueAsNumber)) {
console.error(searchedValueInput, 'is not a valid number');
rl.close();
}
searchedValue = searchedValueAsNumber;
return rl.close();
});
}
rl.on("close", function() {
if(array && searchedValue) {
findIndexValueInArray(searchedValue, array);
}
console.log("\nBYE BYE !!!");
process.exit(0);
});
doFirstQuestion();