what does this code tells the computer to do? P.s. The 'lights' is a boolean array.
for (int k = 1; k < lights.length; k++)
lights[k] = !lights[k];
what does this code tells the computer to do? P.s. The 'lights' is a boolean array.
for (int k = 1; k < lights.length; k++)
lights[k] = !lights[k];
It's basically toggling the boolean flags in the array (except the first one). A true
flag will be set to false
and vice-versa.
Note that any uninitialized items of a boolean
array will be false
in Java.
const lights = [false, false, false];
//toggling flags except the first one
for (let k = 1; k < lights.length; k++)
lights[k] = !lights[k];
console.log(lights);