-1

I want to display the total number of possible unique word combinations.

This is my sample code:

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

How can I achieve this with JS or Jquery?

Oliver Ruehl
  • 147
  • 2
  • 8

2 Answers2

1

The number of combinations is wordlist1.length * wordlist2.length

To display the combinations:

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
  }
gabitzish
  • 9,535
  • 7
  • 44
  • 65
1

Example for ONE list... put the for-loop in a function and you've got it :-)

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("|"));
Tobi
  • 1,440
  • 1
  • 13
  • 26
  • Thanks all. It worked an I've learned something. Sorry if my questions are too basic for some users here. I have very limited time and I really appreciate your answers to even very basic things. I still have to get to a level where I can think of a quick solution myself. – Oliver Ruehl Apr 23 '12 at 12:16
  • 1
    That's no problem. We all started from level 0 :-) – Tobi Apr 23 '12 at 13:23