-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevenMoreBinarySearch.js
More file actions
61 lines (55 loc) · 1.77 KB
/
Copy pathevenMoreBinarySearch.js
File metadata and controls
61 lines (55 loc) · 1.77 KB
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
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// requires an array in numerical order
const arr = Array.from({ length: 100 }, (_, index) => index * 2 + Math.floor(Math.random() * 2));
arr.sort((a, b) => a - b);
// finds num in arr, if it is there and arr is in numerical order
function binarySearch (num, arr) {
let start = 0;
let end = arr.length -1;
while (start <= end) {
let mid = Math.floor((start + end)/2);
if (num === arr[mid]) {
console.log(`Eureka! your number ${num} is in the array at index ${mid}`)
rl.close()
} else if (num > arr[mid]) {
start = mid + 1;
} else {
end = mid -1;
}
}
console.log(`alas it ain't here`)
rl.close();
}
function pickNumber () {
rl.question("enter an integer between 0 and 1000: ", (input) => {
let userNumber = Number(input);
if (isNaN(userNumber) ||
userNumber < 0 ||
userNumber > 1000 ||
!Number.isInteger(userNumber)) {
console.log("your number does not fit the qualifications, please try again: ")
pickNumber()
} else {
binarySearch(userNumber, arr);
}
})
}
function ui () {
rl.question("Would you like to search the random array for you number? ", (input) => {
let userInput = input.toLowerCase();
if (userInput === "y" || userInput === "yes") {
pickNumber()
} else if (userInput === "n" || userInput === "no") {
console.log("okay bye")
rl.close();
} else {
console.log("must respond yes or no, please try again")
ui()
}
})
}
ui();