如何将数组值与 javascript 中的另一组数组值进行比较

How to compare array values with another set of array values in javascript

我正在尝试制作一个 playfair 密码方块生成器。

如果你知道那是什么,那么你就知道你有一个钥匙,然后进入一个方块,在这个方块中,不在钥匙中的字母表的其余部分按字母顺序输入(J 键也被删除) .

所以目前我有这个功能:

function square(usrInputKey){
    var alpha = ["A","B","C","D","E","F","G","H","I","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
    var key = usrInputKey.toUpperCase()
    keyword = key.split('')
    var keysquare=keyword
    count=keyword.length
    for (var p=0;p<alpha.length;p++) {
        //document.write(" Alpha: " + alpha[p] + "<br>")
        if ( keyword[0] != alpha[p] && keyword[1] != alpha[p] && keyword[2] != alpha[p] ) {
            keysquare[count]=alpha[p]
            count++
        }
    }
            return keysquare
}

该函数有关键字的输入,即测试perposes的单词键。

此函数有效,输出为:K,E,Y,A,B,C,D,F,G,H,I,L,M,N,O,P,Q,R,S, T,U,V,W,X,Z

但是只有关键字长度为3个字符才有效。我希望它能与任意数量的关键字一起使用,而不是对 3 或 have 和语句进行硬编码以获得不同的可能长度。

我试过 .every() 但它一次只检查数组字符并输出键,然后输出完整的字母表,包括键中的字母。

谁能解决这个问题?还是必须硬编码到特定的密钥长度?

我认为这是一个有效的例子。 join 和 console.log 是为了方便我。

function square(usrInputKey){
  var alpha =  ["A","B","C","D","E","F","G","H","I","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
  var key = usrInputKey.toUpperCase();
  keyword = key.split('');
  var newAlpha = [];
  for (var k = 0; k < alpha.length; k++) {
    found = false;
    for (var j = 0; j < key.length; j++) {
      if (alpha[k].indexOf(key[j]) > -1){
        found = true;
      }
    }
    if (!found){
      newAlpha.push(alpha[k]);
    }
  }
  newArray = keyword.concat(newAlpha);
  return newArray.join(',');
}
console.log(square('KEYWORD'));

此外,在适当的地方(行尾等)使用分号是个好主意

这是一个生成所需 table 以及一些 jasmine 测试的函数,只是因为:

describe('Key generator test', function () {

    var wordControl = {};
    var alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',  'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    beforeEach(function() {
        alphabet.forEach(function(letter) {
            wordControl[letter] = 0;
        });
    });

    function square(key) {
        var result = [];
        key = key.toUpperCase();
        key = key.replace('J', 'I');
        var keyArray = key.split('');
        keyArray.forEach(function(letter) {
            if(wordControl[letter] == 0) {
                result.push(letter);
                wordControl[letter] = 1;
            }
        });

        alphabet.forEach(function(letter) {
            if(wordControl[letter] == 0) {
                result.push(letter);
            }
        });

        return result;
    }

    it('should generate a table starting with tke key followed by the sorted alphabet', function() {
        var key = 'KAWABANGA';
        var generatedTable = square(key);
        expect(generatedTable.join('')).toEqual('KAWBNGCDEFHILMOPQRSTUVXYZ');
    });

    it('should replace J with I', function() {
        var key = 'JONAS';
        var generatedTable = square(key);
        expect(generatedTable.join('')).toEqual('IONASBCDEFGHKLMPQRTUVWXYZ');
    });

    it('should not fail due to key being more than 25 characters', function() {
        var key = 'QWERTYUIOPASDFGHJKLZXCVBNMMNBVCXZLKJHGFDSAPOIUYTREWQ';
        var generatedTable = square(key);
        expect(generatedTable.join('')).toEqual('QWERTYUIOPASDFGHKLZXCVBNM');
    });
});

我想您仍然需要将生成的数组转换为 5x5 矩阵才能继续 playfair 密码算法。如果您 运行 遇到任何麻烦,请告诉我。