//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).";
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.