Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

switching light bulbs problem javascript

function bulbs(arr){
  var switched = 0;
    for(var i = 0; i < arr.length; i++){
      var initial = arr[i] === 1,
          current = (switched % 2 === 1)? !initial:initial;
      if(current === false){
        switched++;
      }
    }
  return switched;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #switching #light #bulbs #problem #javascript
ADD COMMENT
Topic
Name
6+4 =