0

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?

Vega
  • 27,856
  • 27
  • 95
  • 103
  • please add what does not work. – Nina Scholz Jun 18 '20 at 15:55
  • @NinaScholz: The comments at the end of the code give both the expected answer and the incorrect answer being generated. – Scott Sauyet Jun 18 '20 at 15:57
  • 1
    I'm assuming this is homework, which is fine, as you're not asking us to solve it for you but only asking for help debugging a solution you've already written. But next time, please clearly mark it as homework. – Scott Sauyet Jun 18 '20 at 15:58
  • The problem is that you're not distinguishing arrays of topping with the same values but different orders. You see, for instance, `["Onions","Pepperoni","Mushrooms"]` as different from `["Mushrooms","Pepperoni","Onions"]`. So you're going to think of a way to make these compare as equal. – Scott Sauyet Jun 18 '20 at 16:00
  • @ScottSauyet I completed the project which was to output the Gmail of a unique order. But there was a question that I have to test it with different dataset, so when I start testing, it was giving me the correct result for some dataset but It is not giving me the correct answer in this dataset – Fahim Al Sami Jun 18 '20 at 16:06
  • Yes, my most recent comment demonstrates *why* your answer is wrong for some cases. Can you think of a way to fix it? – Scott Sauyet Jun 18 '20 at 16:09
  • @ScottSauyet can you give me some idea how can I prove these instances are the same? – Fahim Al Sami Jun 18 '20 at 16:10
  • Don't think of it as *proving* that they're the same, but of *transforming* them into a consistent format. How could you do that with an array of strings? You're already doing a transformation in `order.toppings.toString().toLowerCase()`. Is there some canonical format that you can create that either version would transform into? – Scott Sauyet Jun 18 '20 at 16:26
  • https://stackoverflow.com/questions/6229197/how-to-know-if-two-arrays-have-the-same-values what I can do is sort them before comparing them. I found a similar problem in the link attached. – Fahim Al Sami Jun 18 '20 at 16:32
  • That sounds exactly right! If you get stuck, you might open up another question. – Scott Sauyet Jun 18 '20 at 16:47

0 Answers0