<title>
Module 2 *** VERSION 2 *** - Assignment 1 - Sheldon Pasciak - VERSION 2
</title>
<!--
Uses binary search algorithm to successively half ranges to find value in sortedArray
min and max ranges (INDEXES of sorted array) are adjusted based on high or low result of last guess.
Hopefully this demonstrates that by using a sorted array, the search time to find
any element is ( O(sqrt(n) )
-->
fill array with strings
<input type="button" id="btnStrings" value="ABC" onclick="fillStrings()" /><br />
<input type="text" style="width:100px;" id="searchValue" placeholder="search for..." /> <br />
<input type="button" id="btnRandom" value="select a random value" onclick="createRandomPick()" /> <br />
<br />
<input type="button" id="btnStartGuessing" value="start guessing" onclick="startGuessing()" />
<br/>
<p id="output"></p>
CSS
* {
font : 12pt verdana;
}
JavaScript
//Module 2 - Assignment 1 *** VERSION 2 *** - find VALUE in sorted array O(sqrt(n))
var sortedArray = [];
var min=0;
var max=999;
var target = "";
var currentGuess = 0;
//pick random number between mi..mx inclusive
function randomNumber(mi,mx) {
var value = Math.floor((Math.random()*(mx-mi+1))+mi);
return value;
}
//creates a random string of 'len' using 'low char' ... ' high char' (65..90) A..Z
function randomString(len,lowC,highC) {
var str = "";
for (var i=0;i<len;i++) {
str += String.fromCharCode( randomNumber(lowC,highC) );
}
return str;
}
//used to display results in console and document element output
function addMessage(msg) {
console.log(msg);
document.getElementById("output").innerHTML += msg + "<br />";
}
//used to empty display
function clearMessage() {
console.log("\n\n-----\n\n");
document.getElementById("output").innerHTML = "";
}
//fills array with random string and sorts using built in array sort
function fillArrayStrings(array,count) {
var len = 5; len = prompt("How many characters in each string? ",len);
var str = "";
for (i=0;i<count;i++) {
// create a random string of 8 characters with A...Z random letters
str = randomString( len, 65, 90 );
array[i] = str;
}
//uses array class sort
array.sort();
addMessage(array);
}
function fillStrings() {
sortedArray=[];
clearMessage();
var howMany = prompt("How many random strings in the array? ",1000);
fillArrayStrings(sortedArray,howMany);
createRandomPick(sortedArray);
}
//picks a random value from the array of values (i.e. list of numbers, names, etc)
function createRandomPick() {
//sets the search target to be a random element of the array
document.getElementById("searchValue").value =
sortedArray[randomNumber(0,sortedArray.length-1)];
}
//onclick of btnStartGuessing
function startGuessing() {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.