Assignment 2 - part two

Time Complexity and Big-O Notation

by F Fornos

HTML

<h3>
Step 1: Create the array.
</h3>
<input type="button" value="Create Array" onClick="baseArray();" />
<br/>
<h3>
Part 2: Enter a value to search for: 
</h3>
<input size="3" type="textbox" id="value" value="" /> 

<input type="button" value="Search" onClick="SearchArray();" />
<br/> ____________________________________________________
<div id="output">

JavaScript

var operationCount;
var array = new Array(100);
var report; // handles the output format
var initialList = "";
function baseArray() {

	var baseList = "";
	for (var i = 0; i < array.length; i++) {
		array[i] = Math.ceil(Math.random() * 100);
		baseList +=  i + ":" + array[i]  + ", ";
	}
	report = baseList;
  document.getElementById("output").innerHTML = report;
}

function SearchArray() {
		//on search, holds the index if the value specified is found
	var foundAt;

	  //operation count will reset every time a new array is populated
 	operationCount = 0;

    //value to search for, entered by user
  var toFind = parseInt(document.getElementById("value").value);

		//loop through the array until the value is found
	for (var i = 0; i < array.length; i++) {
    if (foundAt) {
    	continue;
    }
    operationCount += 1;
		if( toFind == array[i]){
          foundAt = i;
     }
	}
  //outputs the initial array on final report
  initialList = initialList || report;
  
  report = "Searched for: " + toFind + "<br/>";
  report += "Value found at index: " + foundAt + "<br/> <h5>(Note: If index returns <u>undefined</u>, the value is NOT present in the array)</h5>";
  report += "Number of operations taken: " + operationCount + "<br/>" + "____________________________________________________" + "<br/>";
  report += initialList;
  document.getElementById("output").innerHTML = report;
}