Module1 walkthrough
by Ryan Brown
HTML
<div id="operations1"></div>
<br>
<div id="operations2"></div>
<br>
<div id="find"></div>
<br>
<div id="find-option"></div>
<br>
<div id="form-array">
<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="click" value="Update Array">
<br>
<br>
</div>
<div id="updated-array"></div>
CSS
#click {
font-size: inherit;
color: #FFF;
background-color: #000;
width: 10em;
height: 3em;
}
JavaScript
//create a program that will instantiate an integer array of size 1000
var array = new Array(1000); //O(1) constant
//when 'Update Array' is clicked call myFormSubmit function
document.getElementById("click").onclick = function() {
myFormSubmit()
}; //O(1)
//form function called and stores form input as variables
function myFormSubmit() { //O(1)
var index_entered = document.getElementById("index").value; //O(1)
var number_entered = document.getElementById("number").value; //O(1)
document.getElementById('updated-array').innerHTML = InsertIntoArray(array, index_entered, number_entered); //O(1)
}
function InsertIntoArray(array, index, number) { //O(1)
//Fill each array element with a random integer between 1 and 100.
//add a number between 1 and 100 to each element in array
// 100 gives random inclusive of 0..99
// +1 makes inclusive of 100
for (var a = 0; a < array.length; a++) { // O(N) //do multiple times // 2002
//array[a] = a;
array[a] = Math.floor((Math.random() * 100) + 1); // O(1) //1000
}
//remove last element from array
//array.pop(); // O(n)
//alternate with splice (index, how many) index-1 is last index
//array.splice(-1,1); // O(n)
//alternate using array.length
array.length = array.length - 1; // O(1)
//replace element (index, how many items to remove starting at index, values to add)
array.splice(index, 0, number); // O(n)
//accept user input as number
array[index] = number; // O(1)
//printing output
var updated_array = "";
for (i = 0; i < array.length; i++) { // O(N) //2002
updated_array += array[i] + " = Value of array index " + i + "<br>"; // O(1) //1000
}
return document.getElementById('updated-array').innerHTML = updated_array; // O(1)
}
function SearchArray(array, find) { // O(1)
for (var a = 0; a < array.length; a++) { // O(N) //2002
array[a] = Math.floor((Math.random() * 100) + 1); // O(1)
}
a = array.indexOf(find); // O(n)
return a != -1...