Big-O Assignment 2 Part 1
by arosado417
HTML
Index:
<input type="textbox" id="index" value="2"/>
<input type="button" value="Create Array" onclick="createArray()" />
<br/>Value:
<input type="textbox" id="value" value="100" />
<input type="button" id="insert" value="Insert Into Array" onclick="insertIntoArray()"/>
<br />
<div id="output">
</div>
JavaScript
var array = []; // Global array to hold array
var d = ""; // Global string for output
function createArray() {
// call function to clear the display values
clearDisplay();
// simple loop hard coded to 100 to set array values
for (var i = 0; i < 100; i++) {
array[i] = Math.floor(Math.random() * 100 + 1);
}
// call function to display the array
displayArray();
}
function clearDisplay() {
//Global string d is used to hold display
d = "";
// The div element named output is used to display output
document.getElementById("output").innerHTML = "";
}
function displayArray() {
// simple loop to add array values to string d
for (var i = 0; i < array.length -1 ; i++) {
d += i + ' : ' + array[i] + "<br/>";
}
document.getElementById("output").innerHTML = d;
}
function insertIntoArray() {
clearDisplay();
// get value of index of value to insert
var i = parseInt(document.getElementById("index").value);
// get actual value to insert at index i
var v =
parseInt(document.getElementById("value").value);
var opt = 0;
d = "inserting " + v + " at " + i + "<br/>";
// Students cannot use this function, but this is how to
// insert using built in splice function of Javascript
arry = array.splice(i, 0, v);
/* for( i= array.length - 1; i >= index; i--){
if
(i> index){
array[i] = array[i-1];
opt++;
} else if (i == index) {
array[i] = value;
opt++;
}
*/
// Display the array
displayArray();
}