JavaScript 二分查找 w/out 数组
JavaScript Binary search w/out array
我正在尝试使用 JavaScript 创建一个数字猜谜游戏来进行代码挑战,我对这门语言还很陌生,需要一些帮助。
游戏的前提是让电脑猜猜用户的号码在0到100之间。参数是使用if-else语句和while循环中的确认函数。建议使用将在循环中移动的二进制搜索(不使用和数组);通过询问用户他们的数字是否高于、低于或等于计算机当前的猜测来缩小计算机的猜测范围。
正如您在下面看到的,我能够启动 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;
<!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>
您可以将 while 循环与 confirm 语句结合使用,以简单的方式创建它。
备注:
- 我已将确认包装在辅助函数中以提高代码可读性。
- 我在询问它是否较高后跳过了检查它是否较低的提示,因为它是多余的。
样本
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();
这段代码实现了你的想法,你想要实现的很有趣!但是实现它的想法似乎并不方便。
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();
我正在尝试使用 JavaScript 创建一个数字猜谜游戏来进行代码挑战,我对这门语言还很陌生,需要一些帮助。
游戏的前提是让电脑猜猜用户的号码在0到100之间。参数是使用if-else语句和while循环中的确认函数。建议使用将在循环中移动的二进制搜索(不使用和数组);通过询问用户他们的数字是否高于、低于或等于计算机当前的猜测来缩小计算机的猜测范围。
正如您在下面看到的,我能够启动 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;
<!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>
您可以将 while 循环与 confirm 语句结合使用,以简单的方式创建它。
备注:
- 我已将确认包装在辅助函数中以提高代码可读性。
- 我在询问它是否较高后跳过了检查它是否较低的提示,因为它是多余的。
样本
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();
这段代码实现了你的想法,你想要实现的很有趣!但是实现它的想法似乎并不方便。
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();