Enter Array Size:
<input type="number" id="tbArraySize" value="100" />
<input type="button" id="btnCreateArray" value="Create Array" onclick="createArray()" />
<br /> Enter Location:
<input type="number" id="tbArrayIndex" />
<br/> Enter Value to Insert:
<input type="number" id="tbArrayValue" />
<input type="button" id="btnInsertValue" value="Insert Into Array" onclick="insertIntoArray()" />
<br /> Enter Value to find:
<input type="number" id="tbArraySearch" value="0" />
<input type="button" value="Search Array" id="btnSearchValue" onclick="searchValue()" />
<br/>
<div id="output">
</div>
<div id="counter">
</div>
JavaScript
var array = []; // Global array to hold array
var count = 0;
var d;
function createArray() {
// call function to clear the display values
clearDisplay();
var size = parseInt(document.getElementById("tbArraySize").value);
array = new Array(size);
// simple loop
for (var i = 0; i <= size; i++) {
array[i] = Math.floor(Math.random() * 100) + 1;
}
// call function to display the array
displayArray();
}
function clearDisplay() {
//Global string d is used to hold display
d = "";
// The div element named output is used to display output
document.getElementById("output").innerHTML = "";
}
function displayArray() {
// simple loop to add array values to string d
for (var i = 0; i < array.length - 1; i++) {
d += i + ' : ' + array[i] + "<br/>";
}
document.getElementById("output").innerHTML = d;
}
function insertIntoArray(arr, index, num) {
// get value of index of value to insert
var n = parseInt(document.getElementById("tbArrayIndex").value);
// get actual value to insert at index i
var v = parseInt(document.getElementById("tbArrayValue").value);
var length = array.length;
d = "Value " + v + " inserted at location " + n + ".<br/>" + "There are " + count + " operations in this insertion" + "<br/>";
for(i = length -1; i >= v; i--){
array[i] = array[i-1];
count++;
}
array[n] = v;
displayArray();
}
/*
for (i = length - 1; i < n; i--) {
count++;
array[i] = array[i - 1];
}
array[n] = v; {
count++;
} {
displayArray();
}
}
*/
function searchValue(arr, value) {
clearDisplay();
var length = array.length;
var s = parseInt(document.getElementById("tbArraySearch").value);
var v = parseInt(document.getElementById("tbArrayValue").value);
for (var i = 0; i < length; i++) {
count++;
if (array[i] == s) {
d = "After " + count + " operations, you were unsuccessful in finding " + s + ".<br/> The time complexity is O(n).";
} else {
d = "Jackpot ! You found " + s + "at...
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.