MOD 1 - LOAD AND INSERT

by SHELDON PASCIAK

HTML

<title>
    Module 1 - Assignment 1 and 2 - Sheldon Pasciak
</title>

LOAD ... fill array (and set search to random value based on 
<br/><br/>
INSERT random ... add a random # randomly within array
<br/><br/>
FIND first ... find first value through iteration 
<br/><br/>

<!--
    Big-O and Justification

    The InsertIntoArray has O(n) - it must shift ( length - location ) times, 
    When the location to add the integer is last, only 1 operation.
    When the location is first, n operations (worst case complexity O(n)

    The SearchArray function has O(n) because of the same iteration technique

    Although the array.IndexOf operation can be used, it similiarly uses iteration
and hides the operations being performed

    Design consideration - the insert function as implemented here results in the size of the array growing each time a value is added, rather than trimming/losing the last item
in the array.

-->

<input type="button" id="btnLoad" value="load" onclick="LoadArray()"/>

<input type="button" id="btnInsert" value="insert random value" onclick="InsertInto()"/>

<input type="number" value=0 style="width:100px;" id="searchValue"/> 

<input type="button" id="btnFind" value="find first" onclick="FindValue()"/>

<br/>

<p id="output"></p>
<p id="location"></p>
<p id="value"></p>
<p id="ops"></p>

<br/>


<!--

For assignments 1 and 2 your Big-O notation and justification will be written in the html section of JSFiddle.
 
Assignment 1 - Using your JSFiddle account and JavaScript you will create a program that will instantiate an integer array of size 1000. Fill each array element with a random integer between 1 and 100. You will need to research the random function to do this.
 
You will write a function with 3 arguments. The name of the function will be InsertIntoArray. Argument 1 is the array, argument 2 is the index of where you are going to insert a new number, argument 3 is the number to insert. The program should insert the new number at the index...

CSS

* {
    font: 12pt times new roman;
    }

JavaScript

//Module 1 - Assignment 1 & 2 - Sheldon Pasciak
//please view console window for addition output information during runtime

var randomIntegers = [];		// array of Integers
var ops = 0;					// global count of operations
var items = 1000;				// how many to load
var min = 1;					// min random value to fill
var max = 100;					// max random value to fill

//displays array in console window
function showArray(arr) { 
    console.log("Array length: %d",arr.length);
    console.log(arr);    
}

//display to console and window using document element (ops)
function showOperations() {    
	console.log("Operations: %d",ops);    
    document.getElementById("ops").innerHTML = "Operations: " + ops;    
}

//pick random number between min..max inclusive
function chooseRandom(min,max) { 
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

//inserts value, at location (0 based index), into array
//future implementation might use splice and concat functions of array to reduce this O(n)
function InsertIntoArray(array,location,value) {
    
    showArray(array);
    
    console.log("New value to insert: " + value);    
    console.log("Insert at location: " + location);
    
    ops = 1;
    
    startTime = Date.now();
    
    //shift elements right one 
    var rightIndex = array.length; // last item index +1 (adds to array)
    for ( ; rightIndex>location; rightIndex-- )    {
        array[rightIndex] = array[rightIndex-1]; //shift previous item right
        ops++;
    }
    
    showOperations(ops);
    
    array[location] = value; //set new value at location
    
    showArray(array);
    
}

// fills array with howMany integers of random numbers min..max
// O(n) .. single operation on array for each element of array
function fill(array,howMany,min,max) {    
    
    ops = 0;
    
    //initializes each member in array .. array length grows when index is used to assign it
    for (var i=0;i<howMany;i++){
     array[i] = chooseRandom(min,max);   
     ops++;
   ...