Assignment 2

by Jeremy Boss

HTML

<div class="display">

  Enter Array Size:
  <input type="number" id="tbArraySize" min="0" max="100" value="100" />
  <br />  
 
  Enter Location:
  <input type="number" id="tbInsertIndex" min="0" max="100" value="0" />   
   
  Enter Value To Insert:
  <input type="number" id="tbInsertValue" min="0" max="100" value="0" />
 <br /> 
 
 Enter Value To Find
  <input type="number" id="tbSearchValue" min="0" max="100" value="0" />
  <br />

  <table class="btnMmargins" height="25px">
    
        <input type="button" class="btnstyle" value="Create Array" id="btnCreate" />
     
        <input type="button" class="btnstyle" value="Insert Into Array" id="btnInsert" />
      
  
        <input type="button" class="btnstyle" value="Search Array" id="btnSearch" />
<br />
        
  </table>
    <div id="message">

  </div>
</div>

<br />
<div class="display">

  <br />
  <div class="container" id="output">

  </div>
</div>

CSS

.container {
  display: flex;
  flex-direction: row;
  align-content: flex-start;
  flex-wrap: wrap;
  height: 550px;
  margin: auto;
  padding: 15px;
}

.container div {
  height: 28px;
  width: 80px;
}

JavaScript

var Array = [];
var a = "";


document.getElementById("btnCreate").onclick = function() {
  CreateArray()
};

document.getElementById("btnInsert").onclick = function() {
  InsertIntoArray()
};

document.getElementById("btnSearch").onclick = function() {
  SearchArray()
};




function CreateArray() {
  Array = [];
  var arraySize = document.getElementById("tbArraySize").value;
  var insertVal = document.getElementById("tbInsertValue").value;

  for (var i = 0; i < arraySize; i++) {
    Array[i] = Math.floor(Math.random() * 101);
  }
  
  showArray();
}




function showArray() {
 
 for (var i = 0; i < Array.length; i++) {
    a += "<div><strong>" + i + ':</strong> ' + Array[i] + "</div>";
  }
  document.getElementById("output").innerHTML = a;
}




function wipe() {
  
  a = "";
  
  document.getElementById("output").innerHTML = "";
 
  document.getElementById("message").innerHTML = "";
}





function SearchArray() {
  var searchValue = document.getElementById("tbSearchValue").value;
  var match = 0;
  var indexZero = null;
  var comp = 0;
 
  for (var i = 0; i < Array.length; i++) {
    if (Array[i] == searchValue) {
      match++
    }
  }
  
  for (var i = 0; i < Array.length; i++) {
    comp++
    if (Array[i] == searchValue) {
      indexZero = i;
      break;
    }
  }

  if (indexZero != null) {
    document.getElementById("message").innerHTML = " Value " + searchValue + " found " + match + " time(s), there were " + comp + " comparison operations performed in this search " + searchValue + " was found first  at index " + indexZero + ". The time complexity of this algorithm is O(" + comp + ").";    
  }
  
  else {
    document.getElementById("message").innerHTML = " Value " + searchValue + " found " + match + " time(s), there were " + comp + " operations performed. The time complexity of this algorithm is O(" + comp + ").";
  }
}




function InsertIntoArray() {
  var insertVal = document.getElementById("tbInsertValue").value;
  var indexVal =...