Любой покупатель, который заказывает пиццу с тремя топпингами с уникальной комбинацией начинок для всех пицц во всех магазинах в течение этого месяца, получит купон на бесплатную пиццу по электронной почте (если он предоставит свой адрес электронной почты).
Вот два метода определения победителя. Но ни один из этих методов не работает должным образом. Я не уверен, в чем ошибка. Также какой набор данных я должен использовать для тестирования этих реализаций? Как я могу решить эту проблему с помощью запросов к базе данных?
// Here is the json array and functions:
const inputArray = [{"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 printWinners1(inputArray) {
// Perform a QuickSort on the array.
inputArray.sort((a, b) => {
// Convert each toppings array to a string and do a string comparison
return a.toppings.toString().localeCompare(b.toppings.toString());
});
let previousEmail = '';
let previousToppingsAsString = '';
let previousToppingCount = 0;
let numberOfSimilarOrders = 0;
// Iterate through the array, with "order" being each item in the array.
inputArray.map((order) => {
let toppingsAsString = order.toppings.toString();
if (toppingsAsString === previousToppingsAsString) {
numberOfSimilarOrders++;
} else {
if ((numberOfSimilarOrders === 1) &&
(previousToppingCount === 3) &&
(previousEmail) !== '') {
// Print out the email.
console.log(previousEmail);
}
previousToppingsAsString = toppingsAsString;
previousEmail = order.email;
previousToppingCount = order.toppings.length;
numberOfSimilarOrders = 1;
}
});
}
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.length === 3) && (order.email !== '')) {
let toppingsAsString = order.toppings.toString();
let matchingValue = hashTable.get(toppingsAsString);
if (matchingValue) {
// This key was already in the hash table.
// matchingValue is a reference to the object in the hash table.
matchingValue.duplicate = true;
} else {
// Insert into the hash table, using the toppings as the key and an object containing the email as the value.
hashTable.set(toppingsAsString, {
email: order.email,
duplicate: false
});
}
}
});
// Iterate through the values in the hash table, with "value" being each value.
hashTable.forEach((value) => {
if (!value.duplicate) {
// Print out the email.
console.log(value.email);
}
});
}
printWinners1(inputArray)