Assignment 2

data structures

by zazagalaxy

HTML

<div class="divBG">

  Array size:
  <input type="number" id="arraySize" min="0" max="100" value="50" /> Search for:
  <input type="number" id="searchNum" min="0" max="100" value="0" />
  <br />
  <br /> Location:
  <input type="number" id="location" min="0" max="100" value="0" /> Value to insert:
  <input type="number" id="insertValue" min="0" max="100" value="0" />


  <!-- Create buttons within table for center alignment-->
  <table align="center" class="btnmargins" height="100px">
    <tr>
      <td>
        <input type="button" class="btnstyle" value="Create Array" id="createArray" />
      </td>
      <td>
        <input type="button" class="btnstyle" value="Insert into Array" id="insert" />
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <input type="button" class="btnstyle" value="Search" id="btnSearch" />
      </td>
    </tr>
  </table>
  <!-- End Table -->
  <!-- Begin div displays -->
  <div id="message">

  </div>
</div>
<!-- End input div -->
<br />
<!-- Begin array div -->
<div class="divBG">

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

  </div>
</div>

JavaScript

var numArray = [];
var d = ""; 


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

document.getElementById("insert").onclick = function() {
  insertArrayVal()
};

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


function createArray() {
  numArray = [];
  var size = document.getElementById("arraySize").value;
  var insertVal = document.getElementById("insertValue").value;

  clearDisplay(); 

  for (var i = 0; i < size; i++) {
    numArray[i] = Math.floor(Math.random() * 101);
  }
 
  displayArray();
}

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

function clearDisplay() {

  d = "";
  
  document.getElementById("output").innerHTML = "";
 
  document.getElementById("message").innerHTML = "";
}


function searchArray() {
  var searchVal = document.getElementById("searchNum").value;
  var match = 0;
  var firstLocation = null;
  var ops = 0;

  for (var i = 0; i < numArray.length; i++) {
    if (numArray[i] == searchVal) {
      match++
    }
  }
    
  
    for (var i = 0; i < numArray.length; i++) {
      ops++
      if (numArray[i] == searchVal) {
        firstLocation = i;
        break;
      }
    }
  

  if (firstLocation != null) {
    document.getElementById("message").innerHTML = "Your number, " + searchVal + ", has been found " + match + " time(s) in the array. There were " + ops + " operations performed to find " + searchVal + " for the first time at index " + firstLocation + ". The time complexity of this algorithm is O(" + ops + ").";
  } else {
  document.getElementById("message").innerHTML = "Your number, " + searchVal + ", has been found " + match + " time(s) in the array. There were " + ops + " operations performed. The time complexity of this algorithm is O(" + ops + ").";
  }
}


function insertArrayVal() {
  var insertNum =...