-1

我想显示可能的唯一单词组合的总数。

这是我的示例代码:

var wordlist1 = ["Goldener", "Stählerner", "Purpurner", "Strahlender", "Elektrischer", "Taumelnder"];       
var wordlist2 = ["Indianer", "Ast", "Dachs", "Wolfshund", "Schäferhund", "Lupus", "Schakal"] ;

如何使用 JS 或 Jquery 实现这一点?

4

2 回答 2

1

组合数为 wordlist1.length * wordlist2.length

要显示组合:

for (i = 0; i < wordlist1.length; i++)
  for (j = 0; j < wordlist2.length; j++){
    // do something like alert(wordlist1[i] + " " + wordlist2[j]);
    // or append the combinations somewhere
  }
于 2012-04-23T11:41:20.697 回答
1

一个列表的示例...将 for 循环放入一个函数中,您就知道了:-)

var wordlist1 = ["Goldener", "Stählerner", "Purpurner", "Strahlender", "Elektrischer", "Taumelnder"];
var wordlist2 = ["Indianer", "Ast", "Dachs", "Wolfshund", "Schäferhund", "Lupus", "Schakal"];

var uniqueWords = [];

for (var i = 0; i < wordlist1.length; i++) {
    var isUnique = true;

    for (var j = 0; j < uniqueWords.length; j++) { 
        if (wordlist1[i] == uniqueWords[j]) {
          isUnique = false;
          break;
        }
    }

    if (isUnique)
        uniqueWords.push(wordlist1[i]);
}

alert(uniqueWords.join("|"));
于 2012-04-23T11:42:29.870 回答