Build a Traffic Light Sequencer - Build a Traffic Light Sequencer

Tell us what’s happening:

I am stuck at steps 4- 8. I did exactly what it asks for but somehow I didn’t pass. Please help!

Your code so far

const config1 = {
  fault: false,
  phases: [
    { color: "green", duration: 5 },
    { color: "yellow", duration: 2 },
    { color: "red", duration: 4 }
  ]
};

const config2 = {
  fault: false,
  phases: [
    { color: "red", duration: 3 },
    { color: "yellow", duration: -2 },
    { color: "green", duration: 6 }
  ]
};

const config3 = {
  fault: true,
  phases: [
    { color: "green", duration: 5 },
    { color: "yellow", duration: 2 },
    { color: "red", duration: 6 }
  ]
};

const config4 = {
  fault: false,
  phases: []
};

function runSequence(config, cycles) {
  if (config.phases.length === 0) {
    console.log("No phases found");
    return;
  }

  if (config.fault === true) {
    console.log("Faulted phase!");
    return;
  }

  for (let i = 0; i < cycles; i++) {
    for (const phase of config.phases) {
      if (phase.duration <= 0) {
        console.log("Invalid phase detected");
        return;
      }
      console.log(`Switching to ${phase.color} for ${phase.duration} s`);
    }
  }
}


runSequence(config1, 1);
runSequence(config1, 2);
runSequence(config2, 1);
runSequence(config3, 2);
runSequence(config4, 5);

function generateTimeline(config, cycles) {
  const timeline = [];
  let elapsed = 0;

  for (let i = 0; i < cycles; i++) {
  for (const phase of config.phases) {
    elapsed += phase.duration;
    timeline.push(elapsed);
  }
  }
  return timeline;
}

console.log(generateTimeline(config1, 1));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36 Edg/149.0.0.0

Challenge Information:

Build a Traffic Light Sequencer - Build a Traffic Light Sequencer

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-traffic-light-sequencer/69b83e35f19ba26ba1fa517a.md at main · freeCodeCamp/freeCodeCamp · GitHub

Note: Do not add any extra console.log() statements, as they may cause the tests to fail.

Remove any lines where you are calling the function to test it. Those will pollute the log and the automated tests will fail.

I was quite frustrated with sorting it out. Thanks a lot!