Build a Traffic Light Sequencer - Build a Traffic Light Sequencer

Tell us what’s happening:

I am currently stuck on the runSequence function. I was somehow able to pass tests 4 and 5, but I made changes that disrupted the original pass. I am somehow getting the right outputs, but the undefined option and a phase object whose negative duration wasn’t omitted has disrupted any successful passes. I would appreciate some guidance to get back on the right track.

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) {
   
  for(let i = 0; i < cycles; i++) {
    
    for(const phase of config.phases) {
    
      if(phase.duration <= 0) {
        console.log("Invalid phase detected")
        
        
    }
      if(config.phases === undefined) {
        console.log("No phases found")
    }
      if(config.fault === true) {
        console.log("Faulted phase!")
    }
    
      if(config.phases !== undefined) {
        console.log(`Switching to ${phase.color} for ${phase.duration} s`)
    }
    
  }
  }
}
console.log(runSequence(config1, 2))



    

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

Hey @coder2026

The reason undefined is coming because your function runSequence() doesn’t return anything. Also if you are already logging inside the function then there is no need to log the function again.
Also, you are iterating through the loop and checking config.phases === undefined and config.fault === true inside the loop. There is no point to go through these checks again and again because they are not part of phases and their value wont be changing.
And lastly, there is no need to check for this if(config.phases !== undefined), as you have already checked it above, you should directly console the statement if your phase is valid.

Hi @coder2026,

Why is this code inside a loop?

  • 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.

And are you implementing the above instructions fully?

Happy coding

Thank you for your help so far! Only test case 8 for the runSequence function needs to be solved:. In the directions, I was a little confused at the second part of this step: Log No phases found and immediately return if config.phases is empty. I tried using return config or break, but somehow the test case does not resolve.

Finally, I am onto the generateTimeline function. For this function, I’m trying to understand one clear thing from the directions. Do I add a line of code to add the recursive sequence for the phase durations before pushing the result in, or is there a Math method that allows me to do this? The code below allows me to add each phase’s duration to the runningTotal array.

function runSequence(config, cycles) {
   
  for(let i = 0; i < cycles; i++) {
   if(config.fault === true) {
        console.log("Faulted phase!")
        break;
    }
   if(config.phases === undefined) {
        console.log("No phases found")
        
        
    }
   if(config.phases !== undefined) {
     for(const phase of config.phases) {
     if(phase.duration <= 0) {
        console.log("Invalid phase detected")
        continue

         
    }
     else {
        console.log(`Switching to ${phase.color} for ${phase.duration} s`)
  }
   }
   
   }
   
 
    }
    
}

function generateTimeline(config, cycles){
  const runningTotal = []
  for(let i = 0; i < cycles; i++) {
    if(config.phases !== undefined || config.fault === true || duration <= 0) {
     for(const phase of config.phases) {
      
      runningTotal.push(phase.duration)
  }
}
  }
  return runningTotal;
}




Hi @coder2026

  1. Does the fault status check need to run through a loop?

  2. Add the following console log to check the output:

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

Happy coding

Everything worked! Thanks a lot!!