Tell us what’s happening:
I have been stuck on tests 4-8. I am getting the correct answers, but the tests still aren’t passing. I tried removing the console.log()s throughout, but it didn’t make a difference.
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) {
for (let i = 1; i <= cycles; i++) {
if (config.phases.length === 0) {
console.log("No phases found");
return;
} else if (config.fault === true) {
console.log("Faulted phase!");
break;
} else {
for (let i = 0; i < config.phases.length; i++) {
if (config.phases[i].duration <= 0) {
console.log("Invalid phase detected");
} else {
console.log(`Switching to ${config.phases[i].color} for ${config.phases[i].duration} s`);
}
}
}
}
}
runSequence(config1, 1);
runSequence(config1, 2);
runSequence(config2, 1);
runSequence(config3, 2);
runSequence(config4, 5);
function generateTimeline (config, cycles) {
let array = [];
let time = 0;
if (config.phases.length === 0) {
return config.phases;
} else {
for (let x = 0; x < cycles; x++){
for (let i = 0; i <= 2; i++) {
time = config.phases[i].duration + time;
array.push(time);
}
}
return array;
}
}
generateTimeline(config2, 2);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) 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