Assignment 2(new)!
by Ebony McCoy
HTML
<html>
<body>
<h1>
Time Complexity
</h1>
Enter Array Size: <input type="number" id="arraySize" value='100'>
<input type="button" value="Create Array" onclick="CreateArray()"><br> Enter value to Insert: <input type="number" id="insertValue" value=""> Enter Index to Insert: <input type="number" id="insertIndex" value="">
<input type="button" onclick="InsertIntoArray()" value="Insert Into Array" id="InsertId"><br/> Enter Value to Search: <input type="number" id="search" value="">
<input type="button" onclick="SearchValue()" value="Search for Value">
<div id="output2">
</div>
<div id="output">
</div>
</body>
</html>
JavaScript
var Array = []; // global Array
var out = ""; // global output value
function ClearArray() {
out = "";
document.getElementById("output").innerHTML = "";
}
function DisplayArray() {
//loop creating index for display
for (var i = 0; i < Array.length; i++) {
out += i + " : " + Array[i] + "<br>";
}
//display output onto div
document.getElementById("output").innerHTML = out;
}
function CreateArray() {
ClearArray();
var length = document.getElementById('arraySize').value;
//loop the length of array
for (var i = 0; i < length; i++) {
//fill array with random numbers
Array[i] = Math.floor(Math.random() *
100 + 1);
}
//call clear and display finctions
DisplayArray();
}
//insert textbox values into array
function InsertIntoArray() {
ClearArray();
var size = document.getElementById("arraySize").value;
var index = document.getElementById("insertIndex").value;
var value = document.getElementById("insertValue").value;
var opperation = 0;
for (i = size - 1; i >= index; i--) {
if (i > index) {
Array[i] = Array[i - 1];
opperation++;
} else if (i == index) {
Array[i] = value;
opperation++;
}
}
document.getElementById('output2').innerHTML = "The Value " + value + " has been inserted into index " + index + ". " + opperation + " operations were preformed. With the time complexity of: O(" + opperation + ").";
DisplayArray();
}
function SearchValue() {
var search = document.getElementById('search').value;
var match = 0;
var firstIndex = null;
var opperation = 0;
for (var i = 0; i < Array.length; i++) {
if (Array[i] == search) {
match++;
}
}
for (var i = 0; i < Array.length; i++) {
opperation++;
if (Array[i] == search) {
firstIndex = i;
break;
}
}
if (firstIndex != null) {
document.getElementById("output2").innerHTML = "The Number " + search + " was found " + match + " times. There were " + opperation + " comparison...