var array = []; // Global array to hold array
var a = ""; // Global string for output
function fillArray() {
// call function to clear the display values
clearDisplay();
// simple loop that is hard coded to 100 to set array values
for (var k = 0; k < 100; k++) {
array[k] = Math.floor(Math.random() * 100 + 1);
}
// This is the call function to display the array
displayArray();
}
function clearDisplay()
{
//Global string a is used to hold display
a = "";
// The div element named output is used to display output
document.getElementById("output").innerHTML = "";
}
function displayArray()
{
// simple loop to add array values to string k
for (var k = 0; k < array.length - 1; k++) {
a += k + ' : ' + array[k] + "<br/>";
}
document.getElementById("output").innerHTML = a;
}
function insertIntoArray() {
clearDisplay();
// get value of "index" of value to insert
var k = parseInt(document.getElementById("index").value);
// get actual "value" to insert at index k
var v =
parseInt(document.getElementById("value").value);
a = "inserting " + v + " at " + k + "<br/>";
arry = array.splice(k, 0, v);
//if change arry to array it deletes the array but leaves insert
// Display the array
displayArray();
}
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.