RB_JSFiddle_A2
Assignment2
by Ryan Brown
HTML
<div id="operationsA"></div>
<br>
<div id="operationsB"></div>
<br>
<div id="find"></div>
<br>
<form>
Index:
<br>
<input type="text" name="index" id="index" value="3">
<br> Number:
<br>
<input type="text" name="number" id="number" value="0">
<br>
<br>
</form>
<input type="submit" id="update" value="Update Array">
<br>
<br>
<div id="updated-array"></div>
CSS
#update {
width: 15em;
height: 5em;
color: darkblue;
background-color: beige;
}
JavaScript
var array = new Array(1000);
document.getElementById("update").onclick = function() {
myFormSubmit()
};
function myFormSubmit() {
var index_entered = document.getElementById("index").value;
var number_entered = document.getElementById("number").value;
document.getElementById('updated-array').innerHTML = InsertIntoArray(array, index_entered, number_entered);
}
function InsertIntoArray(array, index, number) {
//Fill each element with a random 1-100
for (var a = 0; a < array.length; a++) {
array[a] = Math.floor((Math.random() * 100) + 1 );
InsertIntoArray[a]= InsertIntoArray[a-1]
// inseryArry();
}
array[index] = number;
/* function insertArry (int InsertIntoArray[], int index, int value){
for(int i=0; i< inputArray.length-1; i++) {
if (i == index){
for (int j = InsertIntoArray.length-1; j >= index; j-- ){
InsertIntoArray[j]= InsertIntoArray[j-1];
}
InsertIntoArray[index]=value;
}
}
return InsertIntoArray;
} */
// array.length = array.length + 1;
//array.splice(index, 0, number);
//output
var updated_array = "";
for (i = 0; i < array.length; i++) {
updated_array += array[i] + " = Value of array index " + i + "<br>";
}
return document.getElementById('updated-array').innerHTML = updated_array;
}
function SearchArray(array, find) {
for (var a = 0; a < array.length; a++) {
array[a] = Math.floor((Math.random() * 100) + 1);
}
a = array.indexOf(find);
return a != -1 ? "Searched for " + find + " and found it at index: " + a : find + " Not found";
}
document.getElementById('find').innerHTML = "A2 " + SearchArray(array, 50) + ". Time Complexity: O(1) + O(1) + O(1) + O(n) + O(n). Total number of operations on search array: 4007";
// Count the number of operations performed on the array
document.getElementById('operationsA').innerHTML = "A1 Number of Operations: 7018";
// Time Complexity using Big...