Assignment 2
Assign2
by dhizzybusy
HTML
Enter Array Size: 100
Enter Location : Enter Value to Insert:
Enter Value to Find:
JavaScript
var array = []; // Global array to hold array
var d = ""; // Global string for output function
fillArray() // 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; i++) { d += i + ' : ' + array[i] + "
"; } document.getElementById("output").innerHTML = d; }
function insertIntoArray() { clearDisplay(); // get value of index of value to insert
var i = parseInt(document.getElementById("location").value); // get actual value to insert at index i
var v = parseInt(document.getElementById("value").value);
var display = array; var copyArray = [];
for (var index = 0; index < display.length; index++) { copyArray[index] = display[index]; } display[i] = v;
for (var a = i + 1; a < display.length; a++) { display[a] = copyArray[a - 1] } array = display; displayArray(); } function searchIntoArray() { clearDisplay(); var sval = parseInt(document.getElementById("SearchArray").value); var b = array.indexOf(sval); if (b == -1) { d += sval + " Was not found !" + "
"; } else { d += sval + " Was found at position " + b + "
"; } document.getElementById("output").innerHTML = d; }