由for循环生成的二维JavaScript数组被最后一个循环结果

时间:2015-11-05 22:56:25

标签: javascript arrays matrix random

我试图使伪随机序列生成器只行Linear Feedback Shift Register。 我是用JavaScript做的,因为它是我所知道的唯一语言,我使用HTML来创建GUI。 用户应键入初始值并获取原理图和伪随机序列本身。 这是我的JavaScript代码:

var UserInput = document.getElementById('ulaz');
var Output = document.getElementById('izlaz');

//variable `data` is an array of objects which I used to store pictures of circuits
// and [taps][3] necessary for shift registers to give max possible length output
// before going into loop which is 2^n-1, where n (`bit` in my code) is number of 
//register blocks and number of digits in input value.

function pss (){
    var data = [
        {
            slika:"pic/2bit.png",
            tap:[0,1]
        },
        {
            slika:"pic/3bit.png",
            tap:[0,2]
        },
        {
            slika:"pic/4bit.png",
            tap:[0,3]
        },
        {
            slika:"pic/5bit.png",
            tap:[1,4]
        },
        {
            slika:"pic/6bit.png",
            tap:[0,5]
        },
        {
            slika:"pic/7bit.png",
            tap:[0,6]
        },
        {
            slika:"pic/8bit.png",
            tap:[1,2,3,7]
        },
        {
            slika:"pic/9bit.png",
            tap:[3,8]
        },
        {
            slika:"pic/10bit.png",
            tap:[2,9]
        },
        {
            slika:"pic/11bit.png",
            tap:[1,10]
        },
        {
            slika:"pic/12bit.png",
            tap:[0,3,5,11]
        },
        {
            slika:"pic/13bit.png",
            tap:[0,2,3,12]
        },
        {
            slika:"pic/14bit.png",
            tap:[0,2,4,13]
        },
        {
            slika:"pic/15bit.png",
            tap:[0,14]
        },
        {
            slika:"pic/16bit.png",
            tap:[1,2,4,15]
        },
        {
            slika:"pic/17bit.png",
            tap:[2,16]
        },
        {
            slika:"pic/18bit.png",
            tap:[6,17]
        },
        {
            slika:"pic/19bit.png",
            tap:[0,1,4,18]
        },
        {
            slika:"pic/20bit.png",
            tap:[2,19]
        },
        {
            slika:"pic/21bit.png",
            tap:[1,20]
        },
        {
            slika:"pic/22bit.png",
            tap:[0,21]
        },
        {
            slika:"pic/23bit.png",
            tap:[4,22]
        },
        {
            slika:"pic/24bit.png",
            tap:[0,2,3,23]
        },
        {
            slika:"pic/25bit.png",
            tap:[2,24]
        },
        {
            slika:"pic/26bit.png",
            tap:[0,1,5,25]
        },
        {
            slika:"pic/27bit.png",
            tap:[0,1,4,26]
        },
        {
            slika:"pic/28bit.png",
            tap:[2,27]
        },
        {
            slika:"pic/29bit.png",
            tap:[0,28]
        },
        {
            slika:"pic/30bit.png",
            tap:[0,3,5,29]
        },
        {
            slika:"pic/31bit.png",
            tap:[2,30]
        },
        {
            slika:"pic/32bit.png",
            tap:[1,5,6,31]
        }
    ];
    var first = UserInput.value.split("");
        for (k=0;k<first.length;k++) first[k] = +first[k]; 
               //first is just UserInput separated in one char strings than parsed to integers
    var bit = first.length - 2;
          // I subtracted 2 here so I can access objects from data
    var matrix = [first];
    var t = 0;
    var between;
    var z;

    for (i=1; i<Math.pow(2, bit+2)-1; i++){     //here is that 2^n-1. +2 is because i had -2 before. For loop is starting from 1 and ending with <2^n-1 because i already have first array of matrix
        for (j=0; j<data[bit].tap.length; j++){
            z = data[bit].tap[j];
            t = t ^ matrix[i-1][z];
        }      // this for makes "t" which is all taps XOR-ed. If user input was 101, tap would be [0,2] and t would be 1xor1=0
        between = matrix[i-1];
            console.log(between);
        between.unshift(t);
        between.pop();
        matrix[i] = between;
        t=0;    // here Im "shifting registers" or just placing t in front of last generated row and removing its last digit, thus generating new row 
    }   
console.log(matrix);
}

here是HTML,因此您可以运行它。

变量data是一个对象数组,我用来存储电路的图片和移位寄存器所需的taps,以便在进入2 ^ n-1的循环之前给出最大可能的长度输出,其中n(我的代码中为bit)是寄存器块的数量和输入值中的位数。

所以问题是:console.log(between);记录最后生成的行是正确的,除了,ofc,没有最后一行,因为它显示了最后生成的,但是console.log(matrix)应该记录完整的矩阵,显示所有被最后一行覆盖的行。 因此对于用户输入101,矩阵应该是

101
010
001
100
110
111
011

但只是

011
011
011 ...

如果console.log(between);之前的部分一切正常,我无法弄清楚它有什么问题......

P.S。代码没有完成它不会在HTML中显示解决方案,并且仍然需要部分函数从矩阵的最后一列创建一个数组,这是伪随机序列。

2 个答案:

答案 0 :(得分:1)

我意识到var之间是指与var matrix [i-1]相同的数组,而不是一个新的独立数组。

between = matrix[i-1];

所以,如果你只想存储矩阵[i-1]的值,而不是创建引用,你可以这样做:

between = JSON.parse(JSON.stringify(matrix[i-1]));

在js中复制某个变量中的数组时,默认情况下会创建该数组的引用。有很多方法可以避免这种情况,您可以找到许多示例here

答案 1 :(得分:0)

我不知道为什么,但我已经找到了解决方案(当我获得空闲时间时会调查更多)。

for (i=1; i<Math.pow(2, bit+2)-1; i++){     //here is that 2^n-1. +2 is because i had -2 before. For loop is starting from 1 and ending with <2^n-1 because i already have first array of matrix
    for (j=0; j<data[bit].tap.length; j++){
        z = data[bit].tap[j];
        t = t ^ matrix[i-1][z];
    }      // this for makes "t" which is all taps XOR-ed. If user input was 101, tap would be [0,2] and t would be 1xor1=0
    between = matrix[i-1];
    console.log(between);
    between.unshift(t);
    between.pop();
    // MODIFICATION
    var between_string = between;
    matrix[i] = between_string.join(); // Turn it to a string
    matrix[i] = matrix[i].split(','); // Turn it back to array to keep it working on the for loop above.
   // END MODIFICATION

    t=0;    // here Im "shifting registers" or just placing t in front of last generated row and removing its last digit, thus generating new row 
}   

现在,当你在控制台中将它打印出来时,它会向你显示一个双向数组,虽然它很奇怪,但有时候(在我的控制台上)它会显示数字,有时会混合字符串数字(尊重之间的原始值)。

编辑:我只尝试使用&#34; 101&#34;输入。

第二次编辑:好的,我感到惭愧,它返回[1,&#34; 0&#34;,&#34; 0&#34;](例子)的原因是因为分裂(&#39; ,&#34;)&#34; 1,0,0&#34;(只有两个数字前面有昏迷)。哈哈。遗憾。