JSFiddle - React, Tailwind, and code Playground

by Oleh Aloshkin

JavaScript

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));