Javascript 把猜谜游戏变成降序游戏
此脚本的工作结果是随机数。例如点击按钮时;您将得到一个如下所示的列表:Javascript 把猜谜游戏变成降序游戏,javascript,Javascript,此脚本的工作结果是随机数。例如点击按钮时;您将得到一个如下所示的列表: 579 too high 520 too high 392 too high 273 too high 90 too high 58 too high 24 too high 6 too low 20 too high 19 too high 13 too high 9 too low 12 Got it !!! 它将显示高和低混合的随机数。 我如何将此程序制作成一个排序列表,以显示: 579 t
579 too high
520 too high
392 too high
273 too high
90 too high
58 too high
24 too high
6 too low
20 too high
19 too high
13 too high
9 too low
12 Got it !!!
它将显示高和低混合的随机数。
我如何将此程序制作成一个排序列表,以显示:
579 too high
520 too high
392 too high
173 too high
120 too high
90 too high
87 too high
50 too high
39 too high
12 too high
9 too low
4 Got it !!!
我如何使这个程序按降序运行?
代码如下:
猜测:
获取号码:
<button id="click" onclick="InsertGuess(1000,0,'',0)">Generate</button>
<div id="guess"></div>
var InsertGuess = function(max,min,finalStr,count) {
var str = 'Guessed :';
var guessnum = Math.floor(Math.random() *(max-min+1)+min);
if(document.getElementById('index').value > guessnum){
finalStr = finalStr.concat(guessnum , ' too low <br>');
count=count+1;
return InsertGuess(max,guessnum,finalStr,count);
}else if(document.getElementById('index').value < guessnum) {
finalStr = finalStr.concat(guessnum, ' too high <br>');
count=count+1;
return InsertGuess(guessnum,min,finalStr,count);
}else{
count=count+1;
finalStr = finalStr.concat(guessnum , ' Got it !!!<br>');
finalStr = finalStr.concat('It took me '+ count +' tries ');
document.getElementById('guess').innerHTML= finalStr;
return ;
}
}
生成
var InsertGuess=函数(最大值、最小值、最终值、计数){
var str='猜测:';
var guessnum=Math.floor(Math.random()*(max-min+1)+min);
if(document.getElementById('index').value>guessnum){
finalStr=finalStr.concat(guessnum,“过低
”);
计数=计数+1;
返回InsertGuess(max、guessnum、finalStr、count);
}else if(document.getElementById('index')。值);
计数=计数+1;
返回InsertGuess(guessnum、min、finalStr、count);
}否则{
计数=计数+1;
finalStr=finalStr.concat(guessnum,“明白了!!!
”);
finalStr=finalStr.concat('It take me'+count+'trues');
document.getElementById('guess')。innerHTML=finalStr;
返回;
}
}
我搜索了如何按降序和升序排列;然而,这是行不通的 将变量(finalStr)设为数组而不是串接字符串,我们将其称为finalArr。然后,不再使用concat,而是将每一行作为一个数组元素推送
finalArr.push(guessnum , ' too low <br>');
您可能必须查看该排序函数,因为它可能无法很好地处理数字(例如,它可能将11视为小于2)。但我至少让你开始了。问题是什么?你的问题措辞很糟糕,请再试一次。我的问题是如何让这个程序按降序排列?当然。对于降序,使用finalArr.sort(函数(a,b){返回b-a});
finalArr.sort();