/**
Returns all infected parameters starting from a specific carrier
Those ones who are infected became carriers as well
@param houses Array of houses with people
@param carrier People/person infected at the beginning
@returns [Array]
*/
function get_infected(houses, carrier) {
carrier = new Set(carrier)
let n = 0
while (n != carrier.size) {
const rest = [...carrier.values()].slice(n)
n = carrier.size
houses.forEach(h =>
h.some(person => rest.includes(person)) &&
h.forEach(person => carrier.add(person))
)
}
return Array.from(carrier)
}
// We want to know who is infected in our area, 1 is infecting 0 and 4 but then 0 and 4 are infecting those one who are in the same substructure
const houses = [[0, 1, 'anne'], [0, 4], [2, 3], [1, 4], ['peter', 5, 6, 'anne']]
document.body.innerHTML =
`Houses: ${JSON.stringify(houses)}<br>
People infected:${get_infected(houses, [1, 0])}`
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.