The code below is not running as I expect it to, how come that the console.log() (at the //THIS) is affected by the code after it? If i run the following code with the outcommented code, the results of the top loggings change. Opposed to running it without the outcommented code so it doesnt change.
some more info about the code below, next.shape[0] returns a double array that looks like this:
[0,0,0,0],
[0,0,0,0],
[0,0,1,0],
[1,1,1,0]
the state.array is also a double array of objects that's about 20x10. (As you might've guessed, I'm trying to build tetris). I'm trying to copy the shape in the above array into the this.state.array, so i created an tempField array so i can edit it and then paste it into the setState().
console.log(this.state.nextShape.shape[0])
var tempField = this.state.array.slice(0);
console.log("state array before: ")
console.log(this.state.array) // THIS
console.log("temp Field before: ")
console.log(tempField)// and THIS
// var tempPosition = this.state.currentPosition.slice(0);
// let z = 0;// for (let y = 0; y < this.state.nextShape.shape[0].length; y++) {
// for (let x = 0; x < this.state.nextShape.shape[0][y].length; x++) {
// if (this.state.nextShape.shape[0][y][x] == 1) {
// tempField[y][x + 3] = { occupied: false, color: this.state.nextShape.color };
// tempPosition[z] = [y, x + 3]
// z++;
// console.log(z)
// }
// }
// }
// console.log("temp Field after: ")
// console.log(tempField)
// this.setState({ array: tempField });
// this.setState({ currentPosition: tempPosition });
// this.setState((state, props) => ({ currentShape: state.nextShape },
// function () {
// this.setState({ nextShape: BlockTypes[Math.floor(Math.random() * BlockTypes.length)] });
// }));
In simpler terms how come a metaphorical piece of code like this:
var numberA = 0;
console.log(numberA);
numberA =+ 100;
console.log(numberA);
In my case (the first block of code), both return the same answer? And how do I fix it?