-2

In the code below you see that it recognises arrayi as array - i. Is this system built into js? I was experementing and it didn't function when I wrote array(i) instead of arrayi. The question then extends beyond all to ask if you could do iarray, ariray, arrayii or array(i*i) (just want to figure out the syntax of how this works).

var array = []
var arrayAmount = prompt("Select number of array")

for (var i = 0; i < arrayAmount; i++) {
  var arrayi = array
  arrayi.push([prompt("Select name for array " + (i + 1)), ["sub-element 1", "sub-elemet 2"], ])
  console.log(arrayi)
}
console.log(array1)

Edit: I checked if the code would work if the for loop declares its own arrays instead of copying another array. Turns out it did not work and declared arrayi as arrayi instead of array1 or array2

Alex M.
  • 3
  • 2

1 Answers1

0

You're confusing several JS concepts. I'll make some assumptions to explain some things, but feel free to correct me and I'll adjust my answer.

I believe you meant to assign a value at a particular index in the array, but what you ended up doing is creating a variable arrayi that is a reference to array:

var array = [];
array[1] = "foo";
console.log(array); // [undefined, "foo"]

What you did instead was create a reference, which means two different variable names will evaluate to the same object/array:

var array = [];
var reference_to_array = array;

reference_to_array.push("foo");

console.log(array); // ["foo"]
console.log(reference_to_array); // ["foo"]

Your original question includes: "and it didn't function when I wrote array(i) instead of arrayi".

To which the answer is: array(i) should be changed to array[i] when working with arrays.

Instead of fixing your original issue, you ended up creating new variables.

To fix your original code:

var array = [];
// Added parsing to an integer
var arrayAmount = parseInt(prompt("How many items do you want to add to the array?"));

for (var i = 0; i < arrayAmount; i++) {
  // Method 1: directly access the index
  //array[i] = [ prompt("Select name for array " + (i + 1)), ["sub-element 1", "sub-element 2"] ];
  
  // Method 2 (Recommended): push a new element onto the end of the array
  array.push([ prompt("Select name for array " + (i + 1)), ["sub-element 1", "sub-element 2"] ]);

  // Log this element
  console.log(array[i]);
}
  • Ah, that was what I was trying to do before however in this case I am making multiple arrays (array1 array2...) which all holds separate elements. if in the code I wrote above is run, it will ask how many arrays it should make then asks names for those arrays. Lets go through the code. "Select number of array" I enter 2 "Select name for array 1" George "Select name for array " Katie console.log(array1) [George ["sub-element 1", "sub-elemet 2"], ] console.log(array2)[Katie ["sub-element 1", "sub-elemet 2"], ] – Alex M. Dec 05 '16 at 13:41
  • @AlexM. Aha! Well, it's definitely possible to create new arrays and use the index in the variable's name, but you would need to use `eval()` for it, which is unnecessary and really bad code. Instead, just use my solution: you still have multiple arrays and you can call them via `array[i]`, the only difference is you'll only have one variable name `array` *(and much safer code!)*. – Sébastien Vercammen Dec 05 '16 at 13:45
  • Ok thank you! Don't know much about code, probably obvious. – Alex M. Dec 05 '16 at 13:46