Build a Traffic Light Sequencer - Build a Traffic Light Sequencer

Tell us what’s happening:

I am getting the right logs but, task 4 -8 seem impossible to pass. What is wrong with the code?

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.phases || config.fault){
    console.log("Faulty phase")
    return;
  }
  for(let t=1; t <= cycles; t++){
for(let i = 0; i<=config.phases.length - 1; i++){
let arrResult = config.phases[i];
if(arrResult.duration <= 0){
console.log("Invalid phase detected")
;
  }
  else{
console.log(`Switching to ${arrResult.color} for ${arrResult.duration} s`)
  }
}
  }
}
runSequence(config2, 1)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.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

Welcome to the forum @Emmysifire,

Are you logging the exact message asked in the instructions?

You should have a function named generateTimeline with two parameters: config and cycles.

Have you implemented this user story?

Happy coding

I have done and passed tasks of generateTimeline function. I have also effected the suggestions that you made but, tasks 4-8 are still pending.

We cannot test from a screenshot.

Please post your updated code, formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

Thank you.

Please comment out any function calls before you run the tests.

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.phases){

console.log("Faulted phase!")

return;

}

for(let t=1; t <= cycles; t++){

for(let i = 0; i<=config.phases.length - 1; i++){

let arrResult = config.phases[i];

if(arrResult.duration <= 0){

console.log(“Invalid phase detected”)

;

}

else{

console.log(`Switching to {arrResult.color} for {arrResult.duration} s`)

}

}

}

}

runSequence(config2, 1);

function generateTimeline(config, cycles){

let finalResult=[];

let count = 0;

for(let t=0; t<cycles; t++){

for(let i=0; i< config.phases.length; i++){

  let arrResult = config.phases\[i\].duration;

  count +=arrResult ;

finalResult.push(count)

}

}

return finalResult

}

console.log(generateTimeline(config1, 1))

Please follow the directions in my last post about how to format your code for the forum…and post all of your code.

Thank you.

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.phases){

    console.log("Faulted phase!")

    return;

  }

  for(let t=1; t <= cycles; t++){

for(let i = 0; i<=config.phases.length - 1; i++){

let arrResult = config.phases[i];

if(arrResult.duration <= 0){

console.log("Invalid phase detected")

;

  }

  else{

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

  }

}

  }

}

runSequence(config2, 1);

function generateTimeline(config, cycles){

  let finalResult=[];

  let count = 0;

  for(let t=0; t<cycles; t++){

    for(let i=0; i< config.phases.length; i++){

      let arrResult = config.phases[i].duration;

      count +=arrResult ;

finalResult.push(count)

    }

  }

 return finalResult

}

console.log(generateTimeline(config1, 1))
  • Log Faulted phase! and stop the simulation early if config.fault is set to true.

Is this instruction implemented correctly?

Done. code is working now

Thank you.

You’re welcome. Good job!