Weird Array Problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Weird Array Problem

I have the following code: var possibilities = [["X"],["O"],["e"]]; var newPossibilities = []; for (var i=0; i<3; i++) { for (var j=0; j<possibilities.length; j++) { newPossibilities.push(possibilities[j]); } } //newPossibilities is now [["X"],["O"],["e"],["X"],["O"],["e"],["X"],["O"],["e"]]; newPossibilities[0].push("A"); console.log(newPossibilities); //output: [["X", "A"],["O"],["e"],["X", "A"],["O"],["e"],["X", "A"],["O"],["e"]] I'm wondering why the output is [["X", "A"],["O"],["e"],["X", "A"],["O"],["e"],["X", "A"],["O"],["e"]] and not [["X", "A"],["O"],["e"],["X"],["O"],["e"],["X"],["O"],["e"]]. Can someone help me?

5th Jul 2018, 3:29 PM
Rahm
Rahm - avatar
1 Answer
5th Jul 2018, 3:39 PM
Rahm
Rahm - avatar