Inserting a Value without splice or push command

Inserting a Value without splice or push command



I am currently working on some code to insert user inputted variables into an array at a specified point WITHOUT using the splice or the push command. I decided to try to use a while command as that is what makes the most sense to me as I am very new to javascript. When I do try to display nothing comes up.


var array = ; // Flobal array to hold array
var d = ""; // Global string for output

function fillArray()
// call function to clear the display values
clearDisplay();
// simple loop hard coded to 100 to set array values
for (var i = 0; i < 100; 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;

//-------------------------------------------------------

//Scan array and insert into index
function insertArray()
var m = parseInt(document.getElementById("index").value);
var n = parseInt(document.getElementById("value").value);

while (i < m)
i++;

if (i == m)
array[i] == n;

displayArray();






"WITHOUT using the splice or the push command" <- why?

– Phil
Sep 4 '18 at 14:01






"I am very new to java" Please remember, Java and Javascript are not the same...this is Javascript

– George Jempty
Sep 4 '18 at 14:04






Do you want to overwrite the previous value at that index, or do you want to keep all the values and move those with a higher index?

– Bergi
Sep 4 '18 at 14:05






Could the problem be that you have not declared i. i is undefined, so i < m is always false, and i ==m is also false and nothing ever gets inserted?

– Wyck
Sep 4 '18 at 14:06


i


i


i < m


i ==m






Please make clear what type of problem you want to solve. Reading this code indicates you just want to do array[m] = n The loop is unnecessary overhead, you should instead check the boundaries of array if the index m is inside the range.

– TOAOGG
Sep 4 '18 at 14:06





2 Answers
2



Possible solution is to create new array every time and overwrite old one. We create two counters one for old array, second for new array. While coping items from old array to new one, when we'r at desired index we'r adding desired value from input and increases only new array counter.


function insertArray()
var m = parseInt(document.getElementById("index").value);
var n = parseInt(document.getElementById("value").value);
var c = 0, o = 0, l = array.length + 1, new_array = ;
while (c < l)
if (c === m)
new_array[c] = n;
else
new_array[c] = array[o];
o++;

c++;

array = new_array;
clearDisplay();
displayArray();




var array = ; // Flobal array to hold array
var d = ""; // Global string for output
function fillArray()
// call function to clear the display values
clearDisplay();
// simple loop hard coded to 10 to set array values
for (var i = 0; i < 10; 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; i++)
d += i + " : " + array[i] + "<br/>";

document.getElementById("output").innerHTML = d;

fillArray();
//-------------------------------------------------------
//Scan array and insert into index
function insertArray()
var m = parseInt(document.getElementById("index").value);
var n = parseInt(document.getElementById("value").value);
var c = 0, o = 0, l = array.length + 1, new_array = ;
while (c < l)
if (c === m)
new_array[c] = n;
else
new_array[c] = array[o];
o++;

c++;

array = new_array;
clearDisplay();
displayArray();


<p>Output: </p>
<input type="text" id="index">
<input type="text" id="value">
<button onClick="insertArray();">Insert</button>
<hr>
<div id="output">
</div>



You can use the following code to add elements to an array without using the splice or push command at the given index. See the implementation of insertArray function, I have commented out the wrong code and written a new line.




var array = ; // Flobal array to hold array
var d = ""; // Global string for output

function fillArray()
// call function to clear the display values
clearDisplay();
// simple loop hard coded to 100 to set array values
for (var i = 0; i < 100; 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] + " ";

document.getElementById("output").innerHTML = d;

//-------------------------------------------------------

//Scan array and insert into index
function insertArray()

var m = parseInt(document.getElementById("index").value);
var n = parseInt(document.getElementById("value").value);

var temp = array[m];
array[m] = n;
for (var i = m+1; i < 100; i++)
array[i] = temp;
temp = array[i];


/*
while (i < m)
i++;

if (i == m)
array[i] = n;

*/
displayArray();


fillArray();


<p>Output: </p>
<input type="text" id="index">
<input type="text" id="value">
<button onClick="insertArray();">Insert</button>
<hr>
<div id="output">
</div>



Please let me know if this is what you wanted.






Your code is replacing value at specified index not adding

– ponury-kostek
Sep 4 '18 at 14:30






@ponury-kostek That's what the OP has been trying to do in his code. I've only tried to fix his code if that's what he wanted.

– Ashish Patel
Sep 4 '18 at 14:38






Is there a way to push the remaining values? BTW I appreciate the help

– Grunt_Worker
Sep 5 '18 at 20:37






@Grunt_Worker can you please explain what do you mean by remaining values? You wish to add more items to the array apart from the given size? e.g currently the size of array is 100. do you want to add an item at the end of this array at index 100?

– Ashish Patel
Sep 5 '18 at 23:44






Here is what I am trying to accomplish array=[2,4,5,6] and say I am trying put a 3 on the second index so it would move integer over and remove the last integer in array so array=[2,3,4,5]. Is that a better way of saying it?

– Grunt_Worker
Sep 7 '18 at 8:22



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)