Module 1 A2

You will write a function with 2 arguments. The name of the function will be SearchArray. Argument 1 is the array, argument 2 is the value you are searching for within the array. Each time the program compares 2 numbers you must count it as an operation (only count comparisons). Output the total number of operations. State using Big O Notation your time complexity and be prepared to justify your answer. This should be output in your actual JSFiddle

by Jenni Meiklejohn

HTML

Big O Notation
<br/> Algorithm is O(N)
<br/> Find:
<input type="number" id="searchValue" value="1" />
<br/>
<input type="button" value="Create Array" onClick="CreateArray();" />
<input type="button" value="Search" onClick="searchArray();" />
<br/>
<div id="output1"></div>
<div id="output2"></div>
<div id="output3"></div>
<div id="output"></div>

JavaScript

var array = new Array(size);
var size = 1000
var count = 0;
var out;

//creates and populates array of random values
function CreateArray() {
  var d = "";
  for (var i = 0; i < size; i++) {
    array[i] = Math.floor(Math.random() * (100 + 1))
  }
  d = " ";
  for (var i = 0; i < size; i++) {
    d += i + " : " + array[i] + "<br/>"
  }
  document.getElementById("output").innerHTML = d;
}
//search array for "value" 
function searchArray() {
  var index;
  var value = parseInt(document.getElementById("searchValue").value);
  for (var i = 0; i < size; i++) {
    if (index) {}
    count++;
    if (array[i] == value) {
      index = i;
    }
  }

  out = "Search Value: " + value;
  document.getElementById("output1").innerHTML = out;
  out = "Found at Index: " + index;
  document.getElementById("output2").innerHTML = out;
  out = "Total Operations: " + count;
  document.getElementById("output3").innerHTML = out;
}