var insert_element= (function () {
function insert_element() { }
insert_element.prototype.element = function () {
var pos;
var c;
var n;
var value;
var array = [
100
];
n = parseInt(prompt("Enter number of elements in array"));
for(c = 0; c < n; c++) {
array[c] = parseInt(prompt("Enter " + n + " integers"));
}
for(var x = 0; x < n; x++) {
var span = document.createElement("span");
span.style.color = "Green";
span.innerText = "Enter " + x + " Element -> " + array[x] + "\n";
document.body.appendChild(span);
}
pos = parseInt(prompt("Enter the location where you wish to insert an element"));
if(pos <= array.length) {
value = parseInt(prompt("Enter the value to insert"));
for(c = n - 1; c >= pos - 1; c--) {
array[c + 1] = array[c];
}
array[pos - 1] = value;
var span1 = document.createElement("span1");
span1.style.color = "red";
span1.innerText = "Resultant array is \n";
document.body.appendChild(span1);
for(c = 0; c <= n; c++) {
var span = document.createElement("span");
span.style.color = "Blue";
span.innerText = "Array Element " + c + " -> " + array[c] + "\n";
document.body.appendChild(span);
}
} else {
alert("Enter location is greater than array lenght so Re-try");
}
};
return insert_element ;
})();
window.onload = function () {
var greeter = new insert_element();
greeter.element ();
}; |