A2

by Ebony McCoy

HTML

Array Size <input type="textbox" size='4' value="100" id='_size'>

<button onclick="document.getElementById('output1').innerHTML= getArray();">Create Array</button>

<br>
<br> Enter Index <input type="textbox" size="4" id='_index'> Number to Insert <input type="textbox" size="3" id='_content'>
<br>
<br>
<button onclick=InsertIntoArray()>Insert Into Array</button>

<br>
<br> Enter number to search <input type="textbox" size="4" id='_search'>
<button onclick=SearchArray()>Search</button>

<p id="output1"></p>
<p id="output2"></p>
<p id="output3"></p>
<p id="output4"></p>
<p id="output5"></p>
<p id="output6"></p>

JavaScript

var array = []; //define array



//create array with random numbers 
function getArray() {
  clearDisplay();
var length = document.getElementById('_size').value;
  for (var i = 0; i < length; i++) {
    array[i] = Math.floor((Math.random() * 100) + 1); //math for random numbers
  }
  return array;
	

}



//insert number at specified index
function InsertIntoArray() {

  var opt = 0; //operations variable
  var index = document.getElementById('_index').value;
  var number = document.getElementById('_content').value;
	var length = document.getElementById("_size").value;

  for (i = length - 1; i >= index; i--) {
    if (i > index) {
      array[i] = array[i - 1];
      opt++;
    } else if (i == index) {
      array[i] = number;
      opt++;
    }
  }

  document.getElementById('output1').innerHTML = array;
  document.getElementById('output2').innerHTML = number + ' was entered at index ' + index;
	document.getElementById('output3').innerHTML = "There were " + opt + " comparison operations completed in the insertion.  Time complexity: O(" + opt + ").";
  
}
clearDisplay();


function clearDisplay() {
  reset = "";
  document.getElementById("output1").innerHTML = "";
}
 function SearchArray() {
    var search = document.getElementById('_search').value;
		var firstIndex = array.indexOf(search);
		var opt = 0;
		
		for (var i = 0; i < array.length; i++) {
    opt++;
    if (array[i] == search) {
      firstIndex = i;
      break;
    }
		

  }
							
				if (firstIndex != null) {
    document.getElementById('output4').innerHTML = "The first instance of number " + search + " was found at index " + firstIndex; 
		document.getElementById('output5').innerHTML = "There were " + opt + " comparison operations completed in the search.  Time complexity: O(" + opt + ").";
  } else {
    document.getElementById('output6').innerHTML = "The number " + search + " was not found.  There were " + opt + " comparison operations completed in the search.  Time complexity: O(" + opt +...