Build a Traffic Light Sequencer - Build a Traffic Light Sequencer

Tell us what’s happening:

Examples work, but no checkmark. Please give me directions…

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

runSequence(config4, 5)

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,

This syntax is incorrect. This is not actually returning. Please look it up.

Here’s an excerpt from the instructions:

* 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`.

Are these instructions fully implemented in your code?

Please comment this out before you run the tests.

Happy coding

I’m sorry I don’t quite fully understand. If I code it like this:

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

It still shows incorrect.

Also do I need to assign the correct phases to a variable and use return to fully be able to complete it according to the instructions?

full code:

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

runSequence(config4, 5)

Hi @adaspivo

Comment out the function call then check your code.

Happy coding