You will write a function with 2 arguments. The name of the function will be SearchArray. Argument 1 is the array, argument 2 is the value you are searching for within the array. Each time the program compares 2 numbers you must count it as an operation (only count comparisons). Output the total number of operations. State using Big O Notation your time complexity and be prepared to justify your answer. This should be output in your actual JSFiddle
var array = new Array(size);
var size = 1000
var count = 0;
var out;
//creates and populates array of random values
function CreateArray() {
var d = "";
for (var i = 0; i < size; i++) {
array[i] = Math.floor(Math.random() * (100 + 1))
}
d = " ";
for (var i = 0; i < size; i++) {
d += i + " : " + array[i] + "<br/>"
}
document.getElementById("output").innerHTML = d;
}
//search array for "value"
function searchArray() {
var index;
var value = parseInt(document.getElementById("searchValue").value);
for (var i = 0; i < size; i++) {
if (index) {}
count++;
if (array[i] == value) {
index = i;
}
}
out = "Search Value: " + value;
document.getElementById("output1").innerHTML = out;
out = "Found at Index: " + index;
document.getElementById("output2").innerHTML = out;
out = "Total Operations: " + count;
document.getElementById("output3").innerHTML = out;
}
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.