문제 링크 : First Bad Version
/**
* 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) {
for(let i=1; i<n+1; i++) {
if(isBadVersion(i)) {
return i === n ? n : i
}
}
};
};
/**
* 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) {
earliestGood = 0
while(n - earliestGood > 1) {
check = Math.floor((n - earliestGood) / 2) + earliestGood
if (isBadVersion(check)) {
n = check
} else {
earliestGood = check
}
}
return n;
};
};