Answer the question
In order to leave comments, you need to log in
Fibbonacci array cycle or unpredictable WTF array behavior?
Everything is fine with this build.
var a = [];
var b = [];
var c = [];
a[0] = 1;
b[0] = 1;
for (var i = 0; i < 4; ++i) {
c = a;
console.log(c); // выводит [1] Ок!
a[0] = a[0] + b[0];
b = c;
console.log(c); // выводит [1] Ок!
}
var a = [];
var b = [];
var c = [];
a[0] = 1;
b[0] = 1;
for (var i = 0; i < 4; ++i) {
c = a;
console.log(c); // выводит [1] Ок!
for (var j = 0; j < a.length; ++j) {
a[j] = a[j] + b[j];
}
b = c;
console.log(c); // выводит [2] ПОЧЕМУ????
}
Answer the question
In order to leave comments, you need to log in
var a = [];
var b = [];
var c = [];
a[0] = 1;
b[0] = 1;
for (var i = 0; i < 4; ++i) {
c = a;
for (var j = 0; j < a.length; ++j) {
a[j] = a[j] + b[j]; //a[j] = c[j] = a[j] + b[j];
}
b = c; //b = c = a
console.log(c); // выводит [2], потому что c[0] = a[0] = b[0] + a[0] = 1 + 1 = 2
}
var a = "text", b = a;
b = "Other text";
console.log(a, b); //text, Other text
var arr = [], brr = arr;
arr.push("item");
console.log(a[0], b[0]); //item, item
array.slice()
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question