<h1>
Assignment 2 - Austin Millett
</h1>
<br><b>
Enter Array Size:</b><input id="size" type="number"><button id="createArray">Create Array</button><br><br>
<b>Enter location:</b><input id="index" type="number"><br>
<b>Enter value to insert:</b><input id="insertValue" type="number"><button id="insertIntoArray">insert into Array</button><br><br>
<b>Enter value to find:</b><input id="searchValue" type="number"><button id="searchArray">Search Array</button>
JavaScript
var array;//Gloabalizing array
document.getElementById("createArray").addEventListener("click",
function() {
printResults("Array Output: ");
var size = document.getElementById("size").value;
array = new Array(size);
for(var i=0; i<size; i++) {
array[i] = Math.floor(Math.random() * 100) + 1;
}
printResults("" + array.join());
});
document.getElementById("insertIntoArray").addEventListener("click",
function() {
if(array == undefined) {
return;
}
var index = document.getElementById("index").value;
var number = document.getElementById("insertValue").value;
InsertIntoArray(array, index, number);
});
document.getElementById("searchArray").addEventListener("click", function() {
if(array == undefined) {
return;
}
var searchValue = document.getElementById("searchValue").value;
searchIntoArray(array, searchValue);
});
function searchIntoArray(arr, searchValue) {
var count = 0;
for(var i=0; i<arr.length; i++) {
count++;
if(arr[i] == searchValue) {
printResults("Value " + searchValue + " found at location: " + i + ", - there were " + count + " operations performed in this search");
printResults("The time complexity is O(n)");
return;
}
}
printResults("Value " + searchValue + " not found in array, there were " + count + " operations performed in this search");
printResults("Time complexity is O(n)");
}
function InsertIntoArray(arr, index, number) {
var count = 0;
for(var i=arr.length - 1; i>index; i--) {
arr[i] = arr[i-1];
count++;
}
arr[index] = number;
printResults("Value " + number + " inserted at location: " + index + ", " + count + " operations were performed in this search");
printResults("Time complexity is O(n)");
}
function printResults(str) {
var par = document.createElement("p");
var text = document.createTextNode(str);
par.appendChild(text);
document.getElementsByTagName('body')[0].appendChild(par);
}
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.