Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: How would I do this in sorted Order? This assignment here: https://jsf

ID: 3672840 • Letter: Q

Question

Question: How would I do this in sorted Order?

This assignment here: https://jsfiddle.net/debhxnqc/27/

The code is here:

Guess:

<input type="textbox" id="index" value="12">
<br/> Get Number:

<button id="click">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>

I was informed that this needs to be done in sorted order when pressing the 'generate button'.

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

Instead I was told it needs to show as:

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

In sorted order. How would I do that?

Explanation / Answer

<html>
<head>

</head>
<body>
Guess:

<input type="textbox" id="index" value="12">
<br/> Get Number:

<button id="click">Generate</button>
<div id="guess"></div>

<script>
var str = 'Guessed :';
var InsertGuess = function(max,min,finalStr,count) {

var numbers;
var guessnum = Math.floor(Math.random() *(max-min+1)+min);

if(document.getElementById('index').value > guessnum){
//finalStr = finalStr.concat(guessnum , ' too low <br>');
finalStr= finalStr.concat(guessnum,' ');
count=count+1;
return InsertGuess(max,guessnum,finalStr,count);
}else if(document.getElementById('index').value < guessnum) {
//finalStr = finalStr.concat(guessnum , ' too high<br>');
finalStr= finalStr.concat(guessnum,' ');
count=count+1;
return InsertGuess(guessnum,min,finalStr,count);
}else{
count=count+1;
numbers = finalStr.split(' ');
//sort descending order
numbers.sort(function(a, b){return b-a});

finalStr = '';
for(var i = 0; i< numbers.length; i++) {
     if(numbers[i] != '') {
     if(numbers[i] < guessnum)
   finalStr=finalStr.concat(numbers[i],' too low<br>');
     else(numbers[i] > guessnum)
   finalStr = finalStr.concat(numbers[i] , ' too high<br>');
     }

}
finalStr = finalStr.concat(guessnum , ' Got it !!!<br>');
finalStr = finalStr.concat('It took me '+ count +' tries ');
document.getElementById('guess').innerHTML= finalStr;

return ;
}
}

</script>
</body>
</html>

--output-----

240 too high
164 too high
134 too high
23 too high
5 too low
17 too high
7 too low
16 too high
12 Got it !!!
It took me 9 tries

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote