forked from codesONLY/JavaScriptONLY
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstBadVersion.js
More file actions
36 lines (35 loc) · 783 Bytes
/
Copy pathfirstBadVersion.js
File metadata and controls
36 lines (35 loc) · 783 Bytes
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
/**
* Definition for isBadVersion()
*
* @param {integer} version number
* @return {boolean} whether the version is bad
* isBadVersion = function(version) {
* ...
* };
*/
/**
* @param {function} isBadVersion()
* @return {function}
*/
var solution = function(isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function(n) {
let start = 1;
let end = n;
let mid;
let res = -1;
while(start <= end){
mid = end - parseInt((end - start)/2)
if(isBadVersion(mid)){
res = mid;
end = mid - 1;
}else{
start = mid + 1;
}
}
return res
};
};