0

For background, I am trying to assign a list of names to a set of tables that can be sat at. tableQuantity tells us how many tables there are, and tableSize tells us how many seats. I am trying to assign the names from the array guestNames into seating tables, which I would like to be represented by the array of arrays seatAssignments. So, for example, if I asked for seatAssignment[0][3], I should get the person sitting at the first table (index 0) in the 4th seat (index 3).

However I have tried it, all tables end up being copies of whatever the last table is. So, seatAssignment[0] should display an array of the first table, but instead it, along with every other index, displays the last table.

Why is the data being overwritten? Relevant code is below. I have included the alerts to see where things go wrong. alert(currentTable) always shows the correct table that was most recently assigned, but by the time alert(seatAssignments) comes around, I am given x duplicates of the last table.

for (i = 0; i < tableQuantity; i++) {  
  for (j = 0; j < tableSize; j++) {
    currentTable[j] = guestNames[i + (j * tableQuantity)];
    seatAssignments[i] = currentTable;
  }
  alert(currentTable);
}
alert(seatAssignments);
Chuck
  • 27
  • 6
  • TLDR ;) If you move away from indexed loops to `Array.forEach()`, life will be much simpler. – Scott Marcus Feb 16 '19 at 18:01
  • I will have to learn how to do that. I am still pretty new to programming! Can you give me a simple example? – Chuck Feb 16 '19 at 18:02
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – Scott Marcus Feb 16 '19 at 18:03
  • 1
    Why are `i` and `j` declared globally? Can you reproduce the issue at a stacksnippets? See https://stackoverflow.com/help/mcve – guest271314 Feb 16 '19 at 18:07
  • 1
    Why are you assigning `currentTable` to `seatAssignments[i]` several times in the inner for loop? You only need to do it once. Also you might have to reset your array `currentTable = []`, because if the second row only has 2 entries and the one before had 3 entries, currentTable.length will still be 3 – Alberti Buonarroti Feb 16 '19 at 18:09
  • @ScottMarcus Don't move to `forEach` (which will complicate control flow), move to `for … of`. – Bergi Feb 16 '19 at 18:44
  • But `tableQuantity`/`Size` are presumably numbers here @Bergi – P Varga Feb 16 '19 at 18:56
  • @Bergi I find `.forEach` incredibly easy and compared to using indexes, it's much simpler. Plus, `.for...of` may not be supported in the OPs uses. – Scott Marcus Feb 16 '19 at 19:04
  • @ScottMarcus [`forEach` has many issues](https://stackoverflow.com/a/50844413/1048572), and isn't be supported by old browsers as well – Bergi Feb 16 '19 at 19:12
  • @Bergi `.forEach` is ECMA 5. `.for....of` is ECMA 6, so `.forEach()` has better support. Also, we're talking about iterating Arrays here, so the benefit of `.for...of` being able to iterate non-array types is irrelevant. Lastly, the performance test referenced in the link you posted is no longer available. I can't imagine the `for of` is significantly faster than `forEach()`. – Scott Marcus Feb 16 '19 at 19:18

0 Answers0