feat(curriculum): add traffic light sequencer lab
Checklist:
- [x] I have read and followed the contribution guidelines.
- [x] I have read and followed the how to open a pull request guide.
- [x] My pull request targets the
mainbranch of freeCodeCamp. - [x] I have tested these changes either locally on my machine, or GitHub Codespaces.
Closes #64122
This PR is part of Naomi's JS sprint to add lab traffic light sequencer to JS v9 loops.
Prototype: https://github.com/freeCodeCamp/CurriculumExpansion/pull/1147
Hi there,
Thanks for opening this pull request.
The linked issue is not open for contribution. If you are looking for issues to contribute to, please check out issues labeled help wanted or first timers only.
Join us in our chat room or our forum if you have any questions or need help with contributing.
The runSequence solution can be modified slightly by adjusting the wording in the user stories and hints, so that the use of break and continue is no longer mandatory.
Ref: https://github.com/freeCodeCamp/CurriculumExpansion/pull/1147#pullrequestreview-3955213495
function runSequence(config, cycles) {
if (config.phases.length === 0) {
console.log("No phases found");
return;
}
if (config.fault === true) {
console.log("Faulted phase!");
return;
}
for (let cycle = 1; cycle <= cycles; cycle++) {
for (let i = 0; i < config.phases.length; i++) {
let { color, duration } = config.phases[i];
if (duration <= 0) {
console.log("Invalid phase detected");
}
else {
console.log(`Switching to ${color} for ${duration} s`);
}
}
}
}
And we can remove the cycles parameter as well to avoid nested loops.
function runSequence(config) {
if (config.phases.length === 0) {
console.log("No phases found");
return;
}
if (config.fault === true) {
console.log("Faulted phase!");
return;
}
for (let i = 0; i < config.phases.length; i++) {
let { color, duration } = config.phases[i];
if (duration <= 0) {
console.log("Invalid phase detected");
}
else {
console.log(`Switching to ${color} for ${duration} s`);
}
}
}
@majestic-owl448 I had accepted your changes in the codespace while making changes and they are reflecting in the files as well I guess. I dont know why it's was still showing as required changes instead of outdated.
I know that sometimes the suggestions do that, so I checked the file and I was seeing the indentation was still not with 2 spaces
Hey @AdityaSingh-18, a few things still need to be addressed before this is ready.
1. generateTimeline user stories don't cover everything that's tested
Two behaviors are verified by the hints but not stated in the user stories:
generateTimeline(config3, 1)returns[5, 7, 13]: the fault flag is completely ignored. Nothing in the user stories tells learners thatgenerateTimelineshould not check for faults.generateTimeline(config2, 2)returns[3, 1, 7, 10, 8, 14]: negative durations are accumulated as-is, so cumulative time can go backwards (3 → 1). The current user story says "record the cumulative elapsed time," which a learner might read as meaning they should skip invalid phases.
The user stories need a bullet that explicitly states that generateTimeline processes all phases without validation, including negative durations and faulted configs.