100 prisoners: Difference between revisions

→‎School example: Optimized algorithm (break after first prisoner, who didn't find its number)
(→‎{{header|JavaScript}}: Added Javascript school example)
(→‎School example: Optimized algorithm (break after first prisoner, who didn't find its number))
Line 3,127:
// Prisoners start outside the room.
// They can decide some strategy before any enter the room.
constlet findingsfound = new Array()0;
for (let prisoner = 1; prisoner <= prisonersCount; prisoner++, found++)
findings.pushif (!find(prisoner, drawers, strategy)) break;
 
// If all 100 findings find their own numbers then they will all be pardoned. If any don't then all sentences stand.
results.push((findings.filter(p => p == true).lengthfound == prisonersCount));
}