Created
May 20, 2016 11:46
-
-
Save lesleh/3cb29222c0e14925fe6fd6da91e10f91 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
"use strict"; | |
const GuessTheNumber = require('./guess_the_number'); | |
module.exports = class GuessTheNumberSolver { | |
constructor() { | |
this.game = new GuessTheNumber(); | |
} | |
solve() { | |
let min = this.game.min; | |
let max = this.game.max; | |
let guessCount = 0; | |
let guess = -1; | |
let response = -1; | |
do { | |
guess = this.bisect(min, max); | |
response = game.guessAnswer(guess); | |
++guessCount; | |
} while(response != 0); | |
console.log(`The answer is ${guess}! Found in ${guessCount} guesses.`) | |
} | |
bisect(min, max) { | |
min + Math.round(min - max / 2.0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment