0

Let say I have 4 arrays of strings with different lengths in Javascript like the following:

const a1 = ['A', 'The', 'This', 'That'];
const a2 = ['vehicle', 'auto', 'car'];
const a3 = ['looks', 'appears', 'seems to be'];
const a4 = ['beautiful', 'good', 'pretty',  'awesome'];

And my issue is to build most possible sentences by combining the words in each array with the next one. At the end, I would have an array of sentences like this:

const sentences = [
   'A vehicle looks beautiful',
   'A vehicle looks good',
   'A vehicle looks pretty',
   'A vehicle looks awesome',
   'A vehicle appears beautiful',
   'A vehicle appears good',
   'A vehicle appears pretty',
   'A vehicle appears awesome',
   'A vehicle seems to be awesome',
   ...,
   'That car seems to be beautiful'
];

So how can I achieve that result ?

Manu
  • 1,065
  • 14
  • 21
  • Have you tried with loops? Like `for(... of ...)` or `.forEach()` ? – Jeremy Thille Nov 16 '20 at 15:41
  • @JeremyThille yes, many tries, but I'm getting nowhere with this. May be to difficult for me – Manu Nov 16 '20 at 15:44
  • 1
    The solution is effectively the duplicate of my question mentioned on top. https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript – Manu Nov 16 '20 at 15:49

0 Answers0