Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to apply values to pattern for all combinations in javascript?
    text
    copied!<p>I have following 2 dimensional array input:</p> <pre><code> var arr = [ ['A', ['Sun','Moon']], ['B', ['Cat','Dog']], ['C', ['John','Peter','Zora']] ]; </code></pre> <p>Using that input, I want all combinations applied to a given pattern in javascript: </p> <p>The placeholders in the pattern are in following format: $(<em>name</em>)</p> <p><em>Here are some sample patterns with result:</em></p> <pre><code> var pattern1 = "$(A) / $(A)"; /* Expected Result "Sun / Sun" "Sun / Moon" "Moon / Sun" "Moon / Moon" */ var pattern2 = "$(A)--$(B)"; /* Expected Result "Sun--Cat" "Sun--Dog" "Moon--Cat" "Moon--Dog" */ var pattern3 = "$(C) + $(B)"; /* Expected Result "John + Cat" "John + Dog" "Peter + Cat" "Peter + Dog" "Zora + Cat" "Zora + Dog" */ var pattern4 = "$(A) - $(A) * ( $(B) + $(C) )"; /* Expected Result "Sun - Sun * ( Cat + John )" "Sun - Sun * ( Cat + Peter )" "Sun - Sun * ( Cat + Zora )" "Sun - Sun * ( Dog + John )" "Sun - Sun * ( Dog + Peter )" "Sun - Sun * ( Dog + Zora )" "Sun - Moon * ( Cat + John )" "Sun - Moon * ( Cat + Peter )" "Sun - Moon * ( Cat + Zora )" "Sun - Moon * ( Dog + John )" "Sun - Moon * ( Dog + Peter )" "Sun - Moon * ( Dog + Zora )" "Moon - Sun * ( Cat + John )" "Moon - Sun * ( Cat + Peter )" "Moon - Sun * ( Cat + Zora )" "Moon - Sun * ( Dog + John )" "Moon - Sun * ( Dog + Peter )" "Moon - Sun * ( Dog + Zora )" "Moon - Moon * ( Cat + John )" "Moon - Moon * ( Cat + Peter )" "Moon - Moon * ( Cat + Zora )" "Moon - Moon * ( Dog + John )" "Moon - Moon * ( Dog + Peter )" "Moon - Moon * ( Dog + Zora )" */ </code></pre> <p>The pattern can be any combination (with repeating placeholders) and any length.</p> <p>Can someone help me with an algorithm in javascript?</p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload