//global variables for array and output
var array = [];
var d = "";
var o = "";
var s = "";
//function to fill array with values based on value in array size textbox
function createArray() {
//call function to clear current output display
clearDisplay();
//get arraySize value for array
var arraySize = parseInt(document.getElementById("tbArraySize").value);
//loop that fills array with random numbers based on the array size value
for (var i = 0; i < arraySize; i++) {
array[i] = Math.floor(Math.random() * 100 + 1);
}
//call function to display array
displayArray();
}
//function to clear the display
function clearDisplay() {
//set output variable to empty string
array = [];
d = "";
o = "";
s = "";
//set output div to empty string
document.getElementById("output").innerHTML = "";
document.getElementById("operations").innerHTML = "";
document.getElementById("search").innerHTML = "";
}
//function to display the array
function displayArray() {
//loop that adds the array value to output string d
for (var i = 0; i < array.length; i++) {
d += i + " : " + array[i] + "<br/>";
}
//attempt to remove undefined array elements
array.filter(Boolean);
document.getElementById("output").innerHTML = d;
}
//testing
function insertIntoArray() {
//get index value for insertion
var index = parseInt(document.getElementById("tbInsertIndex").value);
//get value to insert at index
var value = parseInt(document.getElementById("tbInsertValue").value);
//NEW CODE
insert(array, index, value);
displayArray();
}
//insert value into correct index while shifting current array elements to the right
function insert(array, index, value) {
//variable for operation counter
var opsInsert = 0;
d = "Inserting " + value + " at " + index + "<br/>";
//insert WITHOUT splice
for (i = array.length-1; i >= index; i--){
if (i > index) {
array[i] = array[i-1];
opsInsert++;
}
else if (i ==...
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.