我如何按排序顺序执行此操作?
此处的作业:https://jsfiddle.net/debhxnqc/27/
代码在这里:
Guess:
<input type="textbox" id="index" value="12">
<br/> Get Number:
<button id="click" onclick="InsertGuess(1000,0,'',0)">Generate</button>
<div id="guess"></div>
<script>
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;
}
}
</script>
我被告知在按下“生成”按钮时需要按排序顺序完成此操作。
**Example**: When pressing generate the result would show:
532 too high
47 too high
14 too high
8 too low
10 too low
11 too low
12 Got it !!!
It took me 7 tries
相反,我被告知需要显示为:
532 too high
147 too high
214 too high
98 too low
76 too low
45 too low
12 Got it !!!
It took me 7 tries
按排序顺序。我该怎么做?
答案 0 :(得分:0)
public double median(){
double input =0;
double answer =sum() * (input) / data.length;
return answer;
}
&#13;
var InsertGuess = function(max,min,finalStr,count) {
var sortedArray = [];
var guess = function (max,min,finalStr,count) {
var guessnum = Math.floor(Math.random() *(max-min+1)+min);
if(document.getElementById('index').value > guessnum){
sortedArray.push([guessnum , ' too low <br>']);
count=count+1;
return guess(max,guessnum,finalStr,count);
}else if(document.getElementById('index').value < guessnum) {
sortedArray.push([guessnum, ' too high <br>']);
count=count+1;
return guess(guessnum,min,finalStr,count);
}else{
count=count+1;
sortedArray.sort(function (a,b) {return b[0] - a[0]});
finalStr = finalStr.concat(sortedArray.join(''));
finalStr = finalStr.concat(guessnum , ' Got it !!!<br>');
finalStr = finalStr.concat('It took me '+ count +' tries ');
document.getElementById('guess').innerHTML= finalStr;
return ;
}
}
guess(max,min,finalStr,count);
}
&#13;