//Assignment 1 and 2
var array = []; // Global array
var d = ""; // Global string
var o = "0"; //Counter
var n = "10" //Global variable
var s = "not searched yet"; //Variable for search output
function fillArray() {
//function to clear the display values
clearDisplay();
// simple loop hard coded to 1000 to set array values
for (var i = 0; i < n; i++) {
array[i] = Math.floor(Math.random() * 100 + 1);
o++;
}
// call function to display the array
displayArray();
// call function to display the number of operations
displayOperations();
}
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; i++) {
d += i + ' : ' + array[i] + "<br/>";
o++;
}
document.getElementById("output").innerHTML = d;
}
function displayOperations() {
//clear the old value
document.getElementById("operations").innerHTML = "";
//display the new count
document.getElementById("operations").innerHTML = o;
}
function searchArray() {
var i = parseInt(document.getElementById("index").value);
var v = parseInt(document.getElementById("value").value);
for (var i = 0; i < array.length; i++) {
o++;
if (array[i] == v) {
s = "Last " + v + " found at # " + i + "<br/>";
displaySearchResults(s);
o++;
}
displayOperations();
}
}
function displaySearchResults() {
document.getElementById("result").innerHTML = s;
}
function insertIntoArray() {
clearDisplay();
// get value of index of value to insert
var i = parseInt(document.getElementById("index").value);
// get actual value to insert at index i
var v =
parseInt(document.getElementById("value").value);
//new index
var ni = i;
var nv = v;
d = "Inserting " + v + " at " + i + " " + "<br/>";
for (var...
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.