0

The function does give the result with 15 sentences, but the expected result is to contain 30, including, for example: Training für das Polizei, Training für die Polizisten, Training für die Militär ...:

Current Result

Training für die Polizei
Training für das Polizisten
Training für das Militär
Training für das Sanität
Training für das Sanitäter
Trainings für die Polizei
Trainings für das Polizisten
Trainings für das Militär
Trainings für das Sanität
Trainings für das Sanitäter
Trainingseinheit für die Polizei
Trainingseinheit für das Polizisten
Trainingseinheit für das Militär
Trainingseinheit für das Sanität
Trainingseinheit für das Sanitäter

let ar = [
  ['Training', 'für', 'die', 'Polizei'],
  ['Trainings', '', 'das', 'Polizisten'],
  ['Trainingseinheit', '', '', 'Militär'],
  ['', '', '', 'Sanität'],
  ['', '', '', 'Sanitäter']
];

let result = [];

ar.forEach((el, index) => {
  let sentence = [el[0]];
  
  ar.forEach(el1 => {
    for(let i = 1; i < el1.length; i++) {
      if(el1[i]) {
        sentence[i] = el1[i]
      }
    }
    result.push(sentence.join(' '))
  })
});
console.log(result);
onit
  • 2,275
  • 11
  • 25
  • 1
    it looks like a cartesian product where the first words goes into the forst array and so on. – Nina Scholz Dec 01 '22 at 17:30
  • Apparently, @NinaScholz! Thanks for your comment. I'll probably get buried before I can find a solution to this one. Drama – onit Dec 02 '22 at 12:17
  • Am I missing something? Your question isn't consistent with your script (script doesn't include "Sanität" or "Sanitäter"); there isn't a complete example of the desired result, AND it seems as though the heart of this question concerns German language syntax - e.g., you want values for both "die" and "das", AND you want to substitute "für, die/das" wherever there are blank array values. From my perspective, your question lacks detail. Would you please edit your question to explain EXACTLY what you are trying to do, and your problem, and provide a complete example of a desired outcome – Tedinoz Dec 07 '22 at 01:46
  • @Tedinoz, thanks for your comment. You're right. Sorry about that. After I got beat up by it for a long time, I couldn't even think straight when I posted it. The words in German could be replaced by any words, so not German language related necessarily. After it's gone through and built `Training für die Polizei`, while there is `das`, then it should yield `Training für das Polizei` as well, so going back to the begininng while there are values in the adjacent columns. – onit Dec 07 '22 at 12:00
  • OK... _I think_ ;) So, you want to create sentences that combine every instance of words one, two, three and four. Hence, for example, you would get both `Training für die Polizei` and `Training für das Polizei`. – Tedinoz Dec 09 '22 at 00:20
  • That is correct, @Tedinoz! Thanks for your attention. – onit Dec 09 '22 at 12:31

1 Answers1

1

The solution to this problem is to create a nested loop for each of the four sets of words.

I did this by flattenning the array, and then excluding any "words" that didn't have a length longer than one.


function buildSentences(){

  let ar = [
    ['Training', 'für', 'die', 'Polizei'],
    ['Trainings', '', 'das', 'Polizisten'],
    ['Trainingseinheit', '', '', 'Militär'],
    ['', '', '', 'Sanität'],
    ['', '', '', 'Sanitäter'],
  ]

  var numWords = ar[0].length

  // Logger.log("DEBUG Number of Words="+numWords)
  
  // build array of words
  var words= new Array // flattened array
  var word = new Array // array without blanks

  // create array for each word set
  for (var w = 0;w<numWords;w++){
    // Logger.log("DEBUG: w="+w)
    var words = ar.map(function(e){return e[w]})
    word[w] = words.filter(word => word.length > 1);
    // Logger.log(word[w]) // DEBUG
  } 

  // create an array to hold each sentence
  var sentence = []
  // create an array to hold the complete results
  var results=[]

  // Create sentences
  // created nested loops for each of the words
  // word one
  for (var a=0;a<word[0].length;a++){
    var word1 = word[0][a]
    // Logger.log("DEBUG: word1 = "+word1)
    
    // word two
    for (var b=0;b<word[1].length;b++){
      var word2 = word[1][b]
      // Logger.log("DEBUG: word2 = "+word2)
  
      // word three
      for (var c=0;c<word[2].length;c++){
        var word3 = word[2][c]
        // Logger.log("DEBUG: word3 = "+word3)
    
        // word four
        for (var d=0;d<word[3].length;d++){
          var word4 = word[3][d]
          // Logger.log("DEBUG: word4 = "+word4)
          // build the sentence
          sentence.push(word1)
          sentence.push(word2)
          sentence.push(word3)
          sentence.push(word4)
          // copy the completed sentence to the results array
          results.push(sentence.join(' '))
          // clear the senteance array for the next sentence
          sentence = []
      }
    }
  }
}
Logger.log("number of results:"+results.length)
Logger.log(results)

}

RESULTS

Training für die Polizei,
Training für die Polizisten,
Training für die Militär,
Training für die Sanität,
Training für die Sanitäter,
Training für das Polizei,
Training für das Polizisten,
Training für das Militär,
Training für das Sanität,
Training für das Sanitäter,
Trainings für die Polizei,
Trainings für die Polizisten,
Trainings für die Militär,
Trainings für die Sanität,
Trainings für die Sanitäter,
Trainings für das Polizei,
Trainings für das Polizisten,
Trainings für das Militär,
Trainings für das Sanität,
Trainings für das Sanitäter,
Trainingseinheit für die Polizei,
Trainingseinheit für die Polizisten,
Trainingseinheit für die Militär,
Trainingseinheit für die Sanität,
Trainingseinheit für die Sanitäter,
Trainingseinheit für das Polizei,
Trainingseinheit für das Polizisten,
Trainingseinheit für das Militär,
Trainingseinheit für das Sanität,
Trainingseinheit für das Sanitäter


ADDENDUM

Building the array for the referenced answer (createCombinations)

function mod02(){

  let ar = [
    ['Training', 'für', 'die', 'Polizei'],
    ['Trainings', '', 'das', 'Polizisten'],
    ['Trainingseinheit', '', '', 'Militär'],
    ['', '', '', 'Sanität'],
    ['', '', '', 'Sanitäter'],
  ]

  var numWords = ar[0].length
  var words = [], word = [], v = []
  for (var w = 0;w<numWords;w++){
    words = ar.map(function(e){return e[w]})
    word[w] = words.filter(word => word.length > 1)
    v.push(word[w])
  } 
  // Logger.log(v) // DEBUG
}

This creates the following array:

[[Training, Trainings, Trainingseinheit], [für], [die, das], [Polizei, Polizisten, Militär, Sanität, Sanitäter]]

Tedinoz
  • 5,911
  • 3
  • 25
  • 35
  • Amazing and THANKS! I forgot to state that the number of elements in a row is dynamic, meaning this is 4, but it could be 6, or 3... Shame on me. I'll duel with it to find an exit. Thanks a lot! – onit Dec 10 '22 at 21:19
  • Ended up using a combination of your solution and [this one](https://stackoverflow.com/a/26704398/11832197) so as to process `word` dynamically. Thanks! – onit Dec 10 '22 at 23:02
  • _I forgot to state_ Yes, I did wonder about that. _Ended up using ... this one_ That's OK by the design of their array is very different to yours. If you changed your array design, then I'd be very interested in seeing it. If not, then why not submit an answer to your own question - it might help someone in the future. – Tedinoz Dec 11 '22 at 10:05
  • Hey! I've filtered out the blanks and the array is then passed as a parameter to the function in the solution I linked above. – onit Dec 12 '22 at 14:13