loading...

re: Daily Challenge #149 - Fun with Lamps VIEW POST

FULL DISCUSSION
 

Javascript

const getResults = lamps => {
  const results = lamps.reduce(
    (acc, val, i) =>
      i % 2 === 0
        ? {
            a: val === 0 ? acc.a + 1 : acc.a,
            b: val === 1 ? acc.b + 1 : acc.b
          }
        : {
            a: val === 1 ? acc.a + 1 : acc.a,
            b: val === 0 ? acc.b + 1 : acc.b
          },
    { a: 0, b: 0 }
  );

  return results.a <= results.b ? results.a : results.b;
};

console.log(getResults([1, 0, 0, 1, 1, 1, 0]));
code of conduct - report abuse