MOD 2 - BINARY SEARCH - VER 2

by SHELDON PASCIAK

HTML

<title>
    Module 2 - Assignment 1 - Sheldon Pasciak - VERSION 2 implementing new requirements
    
</title>

<!--

*************
VERISON 2 ***
************* IMPLEMENTS changes/additions to the assignment (array) *****

Programming parameters:
 
1 - This will be coded in Javascript.
2 - You should create a dimensioned array of 1000 elements.
3 - You should fill in the elements from 1 to 1000 in order prior to implementing the search

    uses binary search algorithm to successively half range of values to
choose from.  

    min and max ranges are adjusted based on high or low result of last guess

Once you enter the number it will guess the number and do a comparison with the number you entered. It will output the results of the guess and continue to do this until it gets the correct answer. This is what the output of the program will look like (if I enter 329)
 
Guessed 500 - too high.
Guessed 250 - too low.
Guessed 375 - too high.
Guessed 313 - too low.
Guessed 344 - too high.
Guessed 329 - Got It! 
It took me 6 Tries.
 
You can probably figure out how my algorithm works. You will want to create an algorithm that is efficient (lowest possible O).


-->

<input type="number" style="width:100px;" id="yourNumber" placeholder="1...1000" value=329 /> 

<input type="button" id="btnRandom" value="random" onclick="createRandomPick()" />

<input type="button" id="btnStartGuessing" value="start guessing" onclick="startGuessing()" />

<br/>

<p id="output"></p>

CSS

* {
    
    font : 12pt verdana;
}

JavaScript

//Module 2 - Assignment 1
//should take maximum of 10 tries for any number
//2^10 = 1024

var min = 1;
var max = 1000;
var currentGuess = 0;
var userNumber = 0;
var tries = 0;

//pick random number between min..max inclusive
function chooseRandom(min,max) { 
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

//used to display results
function addMessage(msg) { 
    console.log(msg);
    document.getElementById("output").innerHTML +=  msg + "<br />";
}

function clearMessage() {
    console.log("\n\n\n\n");
    document.getElementById("output").innerHTML =  "";
}

function guess(arr) {    

    tries++;
    
    currentGuess = Math.floor ( ((min+max)/2) );  
    
    if (currentGuess > arr[userNumber-1]) {
        addMessage("Guessed " + currentGuess + " - too high.");
        max = currentGuess;                
    }
    
    if (currentGuess < arr[userNumber-1]) {
        addMessage("Guessed " + currentGuess + " - too low.");
        min = currentGuess + 1;		        
	}  
    
    if (currentGuess == arr[userNumber-1]) {   
        addMessage("Guessed " + currentGuess + " - Got It!");
        addMessage("It took me " + tries + " tries.");
        return tries;
    }         
   
    if (tries>1000) return; // fail safe for Math issues
    
    guess(arr);
    
}

//picks a random number 1...1000 for use as target value
function createRandomPick() { 
    document.getElementById("yourNumber").value = chooseRandom(1,1000);   
    startGuessing();
}

//onclick of btnStartGuessing
function startGuessing() {  
        
    var arr=[];

	for (var i=0;i<1000;i++){
	    arr[i]=i+1;
	}

    tries = 0;
    min = 1;
    max = 1000;
    
    clearMessage();
    
	userNumber = Math.floor(document.getElementById("yourNumber").value);
        
    if (userNumber<min || userNumber>max) {
        alert("Your value is out of range! min: " + min + " max: " + max); 
        return ;
    }
        
	guess(arr);
    
}