I have an index.html with menu items
index.html:
<table>
<tr>
<td>Soups</td>
<td><label><input type="checkbox" class = "soups" name="selected" value="chicken_sp" />chicken_sp</label></td>
<td><label><input type="checkbox" class = "soups" name="selected" value="clam_ch" />clam_ch</label></td>
<td><label><input type="checkbox" class = "soups" name="selected" value="mushroom_sp" />mushroom_sp</label></td>
</tr>
<tr>
<td>Salads:</td>
<td><label><input type="checkbox" class = "salads" name="selected" value="potato_sd" />potato_sd</label></td>
<td><label><input type="checkbox" class = "salads" name="selected" value="caesars_ds" />caesars_ds</label></td>
<td><label><input type="checkbox" class = "salads" name="selected" value="carrot_sd" />carrot_sd</label></td>
</tr>
<tr>
<td>Entrees:</td>
<td><label><input type="checkbox" class = "entrees" name="selected" value="stuffed_mushrooms" />stuffed_mushrooms</label></td>
<td><label><input type="checkbox" class = "entrees" name="selected" value="chicken_wings" />chicken_wings</label></td>
<td><label><input type="checkbox" name="selected" class = "entrees" value="fried_calamari" />fried_calamari</label></td>
</tr>
<tr>
<td>Main course:</td>
<td><label><input type="checkbox" class = "main_course" name="selected" value="fish_mc" />fish_mc</label></td>
<td><label><input type="checkbox" class = "main_course" name="selected" value="beef_mc" />beef_mc</label></td>
<td><label><input type="checkbox" name="selected" class = "main_course" value="chicken_mc" />chicken_mc</label></td>
</tr>
<tr>
<td>Desserts:</td>
<td><label><input type="checkbox" class = "desserts" name="selected" value="chocolate_ds" />chocolate_ds</label></td>
<td><label><input type="checkbox" class = "desserts" name="selected" value="vanilla_ds" />vanilla_ds</label></td>
<td><label><input type="checkbox" class = "desserts" name="selected" value="strawberry_ds" />strawberry_ds</label></td>
</tr>
<tr>
<td>Fruits:</td>
<td><label><input type="checkbox" class = "fruits" name="selected" value="apple" />apple</label></td>
<td><label><input type="checkbox" class = "fruits" name="selected" value="orange" />orange</label></td>
<td><label><input type="checkbox" name="selected" class = "fruits" value="mango" />mango</label></td>
</tr>
</table>
<p>
<button id='display button'>Calculate combos</button>
Examples of what I need it to do The app needs to show the meal combination, when the user selects a menu item (at least one from each category) to create a meal. If the user selects more than one item from each category, then the app needs to show all combos for the selected items.
For example, if the user selected "chicken_sp", "caesar_sd", "chicken_wings", "beef_mc", "chocolate_ds" and "apple", the app should show:
chicken_sp + caesar_sd + chicken_wings + beef_mc + chocolate_ds + apple
If the user selects all of the above, and the second dessert ("strawberry_ds") and the second fruit ("orange"), the app should show:
chicken_sp + caesar_sd + chicken_wings + beef_mc + chocolate_ds + apple
chicken_sp + caesar_sd + chicken_wings + beef_mc + chocolate_ds + orange
chicken_sp + caesar_sd + chicken_wings + beef_mc + strawberry_ds + apple
chicken_sp + caesar_sd + chicken_wings + beef_mc + strawberry_ds + orange
I am trying to use cartesian product to calculate this. I got the solution for the cartesian here
my_js:
var soups = [];
var salads = [];
var entrees = [];
var main_course = [];
var desserts = [];
var fruits = [];
$("#display_button").click(function(event) {
event.preventDefault();
//putting selected checkbox values into arrays
var soups = $('input:checkbox:checked.soups').map(function () {
return this.value;
}).get();
var salads = $('input:checkbox:checked.salads').map(function () {
return this.value;
}).get();
var entrees = $('input:checkbox:checked.entrees').map(function () {
return this.value;
}).get();
var main_course = $('input:checkbox:checked.main_course').map(function () {
return this.value;
}).get();
var desserts = $('input:checkbox:checked.desserts').map(function () {
return this.value;
}).get();
var fruits = $('input:checkbox:checked.fruits').map(function () {
return this.value;
}).get();
console.log(salads)
});
//cartesian function
function cartesianProductOf() {
return _.reduce(arguments, function(a, b) {
return _.flatten(_.map(a, function(x) {
return _.map(b, function(y) {
return x.concat([y]);
});
}), true);
}, [ [] ]);
};
//calculating combinations
altert(cartesianProductOf(soups, salads, entrees, main_course, desserts, fruits));
The issue is that the cartesianProductOf function calculates the product only for two arguments (two arrays). How do I change it to calculate for 6?