Tell us what’s happening:
im stuck in step #4 . any suggestion?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.fault) {
console.log("Faulted phase!");
return;
}
if (!config.phases || config.phases.length === 0) {
console.log("No phases found");
return;
}
for (let i = 0; i < cycles; i++) {
for (let j = 0; j < config.phases.length; j++) {
const phase = config.phases[j];
if (phase.duration <= 0) {
console.log("Invalid phase detected");
} else {
console.log(`Switching to ${phase.color} for ${phase.duration} s`);
}
}
}
}
function generateTimeline(config,cycles){
const allPhases = Array(cycles).fill(config.phases).flat();
let total = 0;
return allPhases.reduce((timeline,phase)=>{
total+=phase.duration;
timeline.push(total);
return timeline;
},[]);
}
runSequence(config1, 1);
runSequence(config1,2);
runSequence(config2,1);
runSequence(config3, 2);
runSequence(config4, 5);
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