// Define profiles with their interests
const profiles = [
{ name: 'Alice', interests: ['hiking', 'music', 'movies'] },
{ name: 'Bob', interests: ['sports', 'music', 'coding'] },
{ name: 'Charlie', interests: ['traveling', 'photography', 'movies'] },
{ name: 'Dana', interests: ['hiking', 'cooking', 'movies'] },
];
// Define the interests of the user looking for a match
const userProfile = { name: 'Eve', interests: ['hiking', 'movies', 'coding'] };
// Generator function to find matches
function* findMatches(user, profiles) {
for (const profile of profiles) {
const commonInterests = profile.interests.filter(interest => user.interests.includes(interest));
if (commonInterests.length > 0) {
yield { match: profile.name, commonInterests };
}
}
return 'No more matches available';
}
// Create an instance of the generator
const matchGenerator = findMatches(userProfile, profiles);
// Function to get matches and display them
function getMatches(generator) {
let result = generator.next();
while (!result.done) {
console.log(`Match found: ${result.value.match}`);
console.log(`Common interests: ${result.value.commonInterests.join(', ')}`);
result = generator.next();
}
console.log(result.value); // 'No more matches available'
}
// Run the function to get and display matches
getMatches(matchGenerator);
yaml
Copy code
Match found: Alice
Common interests: hiking, movies
Match found: Dana
Common interests: hiking, movies
No more matches available