JavaScript二进制搜索w / out数组

时间:2018-05-04 08:12:54

标签: javascript binary-search

我正在尝试使用JavaScript为代码挑战创建一个数字猜谜游戏,我对这种语言很新,需要一些帮助。

游戏的前提是让计算机猜测用户的数字在0到100之间。参数是使用if-else语句并在while循环中确认函数。有人建议使用将在循环中移动的二进制搜索(不使用和数组);通过询问用户他们的号码是否高于,低于或等于计算机当前猜测来缩小计算机的猜测范围。

如下所示,如果用户确认该语句为true,我可以启动if语句并给出结果。但现在我仍然坚持如何继续使用else if语句,继续循环,并成功实现二进制搜索。我已经启动了else if语句,但我知道这很可能不是可行的方法。我试图通过堆栈溢出和其他网站搜索如何使用二进制搜索,但是所有结果都是通过我不能做的数组使用它。任何帮助和建议将不胜感激。



var guessNumGame = function () {
	'use strict';
	var low = 0;
	var high = 100;
	var number;

	alert("Think of a number betwwen 0 and 100");
	while (low <= high) {
		number = Math.round((low + high) / 2);

		if (window.confirm("Is your number " + number + " ? Click OK for 
        Yes, CANCEL for No.") == true ) {

			 alert("You got it! Thanks for playing!");

		} //this is where I'm stuck on how to proceed and below is unfinished

        else if (window.confirm("Is your number higher or lower? Click OK 
        for higher, CANCEL for lower. ") < true) {

			 window.confirm("Is your number " + number / 2 + " ? Click OK 
             for Yes, CANCEL for No.") == true;

		}else {

			return guess;
		}

		 return; // this return is here just to avoid and infinite loop
	}

};

window.onload = guessNumGame;
&#13;
<!DOCTYPE HTML>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Pick a Number</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <h1>Pick a number for me to guess</h1>
    <script src="js/pick_number.js"></script>
  </body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您可以将while循环与confirm语句结合使用,以便以简单的方式创建它。

注意:

  1. 我已将确认包装在辅助函数中以提高代码可读性。
  2. 在询问它是否更高之后,我跳过提示检查它是否更低,因为它是多余的。
  3. 样品

    function guessNumber(number) {
      return confirm('Is ' + number + ' your number?');
    }
    
    function askIfHigher(number) {
      return confirm('Is your number greater than ' + number + '?');
    }
    
    function startGame() {
      // Initialise variables
      var low = 0,
        high = 100,
        mid,
        complete = false;
    
      // Repeat while we have not guessed the answer or binary search fails
      while (!complete && (low <= high)) {
        // Guess number
        mid = Math.floor((low + high) / 2);
        complete = guessNumber(mid);
        if(complete){
          // Guess is correct, exit while loop
          break;
        }
        // Guess is incorrect adjust binary search parameters as needed
        if (askIfHigher(mid)) {
          low = mid + 1;
        } else {
          high = mid - 1;
        }
      }
      if(!complete){
        // Binary search terminated without getting a result
        alert('You must have picked a number outisde of 0-100');
      }else{
        alert('Congratulations! your number was: '+mid);
      }
    
    }
    
    startGame();

答案 1 :(得分:0)

这段代码实现了你的想法,你想要实现的是非常有趣的!但实现它的想法似乎并不方便。

var guessNumGame = function () {
	'use strict';
	var low = 0;
	var high = 100;
	var number;

	alert("Think of a number betwwen 0 and 100");
	while (low <= high) {
    	number = Math.floor((Math.random() * (high - low + 1))) + low;
		if (window.confirm("Is your number " + number + " ? Click OK for Yes, CANCEL for No."))
			 return alert("YAAAY! I WON");
     	if (window.confirm("Is your number higher or lower? Click OK for higher, CANCEL for lower. ")) {
			low = number + 1;
		} else {
			high = number - 1;
		}
	}

};

guessNumGame();