Skip to content

Latest commit

 

History

History
39 lines (23 loc) · 1.07 KB

File metadata and controls

39 lines (23 loc) · 1.07 KB

Challenge #9: 🚦 Switch the lights

They are turning on the Christmas lights 🎄 in the city and, as every year, they have to be fixed!

The lights are of two colors: 🔴 and 🟢 . For the effect to be appropriate, they must always alternate. That is, if the first light is red, the second must be green, the third red, the fourth green, etc.

We have been asked to write a function adjustLights that, given an array of strings with the color of each light, return the minimum number of lights that need to be changed for the colors to alternate.

adjustLights(["🟢", "🔴", "🟢", "🟢", "🟢"]);
// -> 1 (you change the fourth light to 🔴)

adjustLights(["🔴", "🔴", "🟢", "🟢", "🔴"]);
// -> 2 (you change the second light to 🟢 and the third to 🔴)

adjustLights(["🟢", "🔴", "🟢", "🔴", "🟢"]);
// -> 0 (they are already alternating)

adjustLights(["🔴", "🔴", "🔴"]);
// -> 1 (you change the second light to 🟢)

Solution

JavaScript

~ points

TypeScript

~ points