var lamps = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], // Array with lamps
steps = [2, 3, 8]; // Array with numbers with steps
function switchLamps(lamps, steps) {
var indicator = [], // Array which indicate, which lamps switch off/on
switchOff = 0;
for (var i = 0; i < lamps.length; i++) {
indicator.push(0); // Filling array "indicator" with 0
}
for (var n = 0; n <= steps.length; n++) { // Loop steps
var m = steps[n] - 1;
while (m < indicator.length) { // Loop indicator and switch on/off lamps
if (indicator[m] == 0) { // If lamp is off
indicator[m] = 1; // Switch on it
} else { // If lamps is on
indicator[m] = 0; // Off it
}
m += steps[n]; // Add step
}
}
for (var j = 0; j <= indicator.length; j++) { // Loop indicator to know which lamp is on or off
if (indicator[j] == 0) {
switchOff++; // Counter which know, how many lamps in off
}
}
return switchOff; // Return how many lamps is off
}
alert(switchLamps(lamps, steps));
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.
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!
Console
Debug your Fiddle with a minimal built-in JavaScript console.