The idea is that any customer that orders a three topping pizza with a unique combination of toppings in all stores for that month will receive a coupon for a free pizza by email (if they provide their email address). Each month they generate a file containing a JSON representation of all the pizzas ordered that month. The toppings for each pizza are sorted in alphabetical order in this form:
monthly_data = [{"email": "email1@example.com", "toppings":
["Mushrooms","Pepperoni","Peppers"]},
{"email": "email2@example.com", "toppings":
["Cheddar","Garlic","Oregano"]},
{"email": "email3@example.com", "toppings": ["Bacon","Ham","Pineapple"]},
{"email": "", "toppings": ["Parmesan","Tomatoes"]},
{"email": "email4@example.com", "toppings":
["Mushrooms","Pepperoni","Peppers"]},
{"email": "", "toppings": ["Cheddar","Tomatoes"]},
{"email": "email5@example.com", "toppings": ["Bacon","Ham","Pineapple"]},
{"email": "email6@example.com", "toppings": ["Beef","Parmesan"]},
{"email": "", "toppings": ["Onions","Pepperoni"]},
{"email": "", "toppings": ["Bacon","Ham","Pineapple"]}]
function printWinners2(inputArray) {
let hashTable = new Map();
// Iterate through the array, with "order" being each item in the
//array.
inputArray.map((order)=>{
if(order.toppings !== null){
if((order.toppings.length === 3) && (order.email !== '')){
let toppingsAsString = order.toppings.toString().toLowerCase();
//console.log(toppingsAsString)
let matchingValue = hashTable.get(toppingsAsString)
/*hashTable.set(toppingsAsString,{email: order.email,
duplicate: false});*/
//console.log(hashTable.get(toppingsAsString))
if(matchingValue){
//console.log(matchingValue)
matchingValue.duplicate = true
//console.log(matchingValue)
} else{
hashTable.set(toppingsAsString,{email: order.email,
duplicate: false});
}
}
}
})
hashTable.forEach((value) => {
if (!value.duplicate) {
// Print out the email.
console.log(value.email);
}
});
}
printWinners2(monthly_data) //email2@example.com [printed answer as expected]
//But I want to test that algorithm with a different dataset.
//what if I use this array for testing
monthly_data = [{"email": "email1@example.com", "toppings":
["Artichoke","Shrimp","Bacon"]},
{"email": "email2@example.com", "toppings":
["Cheese"]},
{"email": "email3@example.com", "toppings": ["BACON","Ham","Pineapple"]},
{"email": "email5@example.com", "toppings": ["Bacon","Ham","Pineapple"]},
{"email": "email6@example.com", "toppings": ["Shrimp","Bacon","Artichoke"]},
{"email": "email7@example.com", "toppings":
["Artichoke","Chicken","Spinach"]},
{"email": "", "toppings": ["Onions","Pepperoni","Mushrooms"]},
{"email": "email8@example.com", "toppings": ["Mushrooms","Pepperoni","Onions"]},
{"email": "", "toppings": null}
]
The expected answer should be only one
email7@example.com
but it is printing:
email1@example.com email6@example.com email7@example.com email8@example.com
any solution?