//create a program that will instantiate an integer array of size 1000
var array = new Array(1000); //O(1) constant
//when 'Update Array' is clicked call myFormSubmit function
document.getElementById("click").onclick = function() {myFormSubmit()}; //O(1)
//form function called and stores form input as variables
function myFormSubmit() { //O(1)
var index_entered = document.getElementById("index").value; //O(1)
var number_entered = document.getElementById("number").value; //O(1)
document.getElementById('updated-array').innerHTML = InsertIntoArray(array, index_entered, number_entered); //O(1)
}
function InsertIntoArray(array, index, number) { //O(1)
//Fill each array element with a random integer between 1 and 100.
//add a number between 1 and 100 to each element in array
// 100 gives random inclusive of 0..99
// +1 makes inclusive of 100
for(var a=0; a<array.length;a++){ // O(N) //do multiple times // 2002
//array[a] = a;
array[a] = Math.floor((Math.random() * 100) + 1); // O(1) //1000
}
//remove last element from array
//array.pop(); // O(n)
//alternate with splice (index, how many) index-1 is last index
//array.splice(-1,1); // O(n)
//alternate using array.length
array.length = array.length -1; // O(1)
//replace element (index, how many items to remove starting at index, values to add)
array.splice(index, 0, number); // O(n)
//accept user input as number
array[index] = number; // O(1)
//printing output
var updated_array = "";
for (i = 0; i < array.length; i++) { // O(N) //2002
updated_array += array[i] + " = Value of array index " + i + "<br>"; // O(1) //1000
}
return document.getElementById('updated-array').innerHTML = updated_array; // O(1)
}
function SearchArray(array, find) { // O(1)
for(var a=0; a<array.length;a++){ // O(N) //2002
array[a] = Math.floor((Math.random() *...
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.