Assignment 2(new)
HTML
<h1>
Time Complexity
</h1>
<p id = 'output'></p>
Enter Array Size: <input type="textbox" id="arraySize" value="100">
<input type="button" value="Create Array" onclick="CreateArray()" id="CreateArrayId">
<br/>
Enter Location: <input type="textbox" id="insertValue" value="">
Enter Value to Insert: <input type="textbox" id="insertIndex" value="">
<input type="button" onclick="InsertIntoArray()" value="Insert Into Array" id="InsertId"><br/>
<div id="output">
</div>
<div id="updateArray">
</div>
JavaScript
var Array = [];// global Array
var out = ""; // global output value
var opperation = 0;
// textbox inserts
document.getElementById("arraySize"). value = arraysize
var insertIndex = document.getElementById("insertIndex").value;
var insertValue = document.getElementById("insertValue").value;
var updateArray = document.getElementById("updateArray").innerHTML = InsertIntoArray(Array, insertIndex, insertValue);
//button event listener
var CreateArrayId = document.getElementById("CreateArrayId");
if(CreateArrayId){
CreateArrayId.addEventListener('click', CreateArray);
}
var InsertId = document.getElementById("InsertId");
if(InsertId){
InsertId.addEventListener('click',InsertIntoArray);
}
function CreateArray(){
var length = 100;
//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
ClearArray();
DisplayArray();
}
function DisplayArray(){
//loop creating index for display
for (var i = 0; i < Array.length; i++) {
out += i + " : " + Array[i] + " ";
}
//display output onto div
document.getElementById("output").innerHTML = out;
}
// create a clear screen for display
function ClearArray(){
d = "";
document.getElementById("output").innerHTML= "";
}
//insert textbox values into array
function InsertIntoArray(array,index,value){
//replace value with new value
var replacedVal = array[index];
//assign value new index
array[index] = value;
//count opperations by 1
opperation++;
for(var i=array.length-1; i>index+1;i--){
//shift value but 1
array[i] = array[i-1];
//count opperations by 1
opperation++;
}
if(index < array.length-1){
//assign replace value to array[index+1]
array[index+1] = replaceVal;
//count opperations by 1
opperation++;
}
return document.getElementById("output").innerHTML = UpdateArray
}
/*function...