InsertIntoArray

by Daniel Eberhart

HTML

<div id="count"></div>
<br>

<div id="bigo"></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" value="12">
    <br> Number:

    <br>

    <input type="text" name="number" id="number" value="88">
    <br>
    <br>

  </form>

  <input type="submit" id="click" value="Create and Randomize Array">
  <br>
  <br>

</div>

<div id="updated-array"></div>

JavaScript

//The Purpose of this program is to create an array with random numbers between 1 and 100 assigned to an index location between 1 and 1000, without using the push or splice function, as well as displaying the number of operations and the time complexity using Big O notation




var array = new Array(1000);

document.getElementById("click").onclick = function() {
  myFormSubmit()
};

function myFormSubmit() { //O(1)

  var index_entered = document.getElementById("index_value").value; //O(1) Doing once

  var number_entered = document.getElementById("number").value; //O(1) Doing once

  document.getElementById('updated-array').innerHTML = InsertIntoArray(array, index_entered, number_entered); //O(1) Doing once

}

function InsertIntoArray(array, index, number) { //O(1) Doing once



  for (var a = 0; a < array.length; a++) { // O(N) //do multiple times, hence N

    //array[a] = a;

    array[a] = Math.floor((Math.random() * 100) + 1); // O(1)  Doing once



  }





  //accept and assign user input into variable

  array[index] = number; // O(1) Doing once



  //printing output

  var updated_array = "";

  for (i = 0; i < 1000; i++ ) { // O(N) Doing multiple times



    updated_array += array[i] + " Is at Index Number: " + i + "<br>"; // O(1) //1000

  }

  return document.getElementById('updated-array').innerHTML = updated_array; // O(1)

}

// Count the number of operations performed on the array and output this to the screen.

document.getElementById('count').innerHTML = "Number of Operations Counted:7018";

// Time Complexity 

document.getElementById('bigo').innerHTML = "Big O Notation Time Complexity: O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(n) + O(n) justifies to: O(n).";