Last active
March 22, 2019 09:34
-
-
Save TenzenIga/a99aad780d3fd74c3cf7db2fc2971d63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findNumber(number) { | |
let max = 1024; | |
let min = 0; | |
if(number > max || number < min){ | |
console.log(`Number must be in range ${min} - ${max}`) | |
return; | |
} | |
let guess; | |
let step = 0; | |
while(max >= min){ | |
guess = (max + min) /2; | |
step++; | |
if( guess == number){ | |
console.log(`Your number is ${guess}.\nFound number in ${step} steps`) | |
return guess; | |
}else if(guess > number){ | |
console.log(`Your number is below ${guess}`) | |
max = guess | |
}else{ | |
console.log(`Your number is above ${guess}`) | |
min = guess | |
} | |
} | |
} | |
console.log(findNumber(42)); | |
console.log(findNumber(73)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment