Build a Traffic Light Sequencer - Build a Traffic Light Sequencer

Tell us what’s happening:

Please help me solve this problem. Code works with examples but not correctly I guess…

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");
  }
  else if (config.fault == true)
  {
    console.log("Faulted phase!");
  }
  else
  {
    for (let i = 0; i < cycles; i++)
    {
      for (let phase in config["phases"])
      {
        if (config["phases"][phase]["duration"] <= 0)
        {
          console.log("Invalid phase detected");
        }
        else
        {
          console.log(`Switching to ${config["phases"][phase]["color"]} for ${config["phases"][phase]["duration"]} s`);
        }
      }
    }
  }
}

runSequence(config4, 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

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

Hi @adaspivo,

  • Log No phases found and immediately return if config.phases is empty.
  • Log Faulted phase! and stop the simulation early if config.fault is set to true.

Have you fully implemented the instructions?

Also, please comment out your function call before running the tests to comply with the Note.

Happy coding