I’m working on the freeCodeCamp’s 25+5 clock project. Though the app works as expected from the functional pov still I’m unable to pass the remaining 4-5 tests in the test suite.
Link to the project
Relevant portion of the code for the application:
interval;
constructor(props) {
super(props);
this.state = {
breakLength: 5,
sessionLength: 25,
timer: 1500,
timerType: "Session",
timerState: "stopped"
};
}
componentDidMount() {
this.setState({
breakLength: 5,
sessionLength: 25,
timer: 1500,
timerType: "Session",
timerState: "stopped"
});
}
componentDidUpdate(prevProps, prevState) {
if (prevState.timer === 0 && prevState.timerType === "Session") {
this.setState({
timer: prevState.breakLength * 60,
timerType: "Break"
});
} else if (prevState.timer === 0 && prevState.timerType === "Break") {
this.setState({
timer: prevState.sessionLength * 60,
timerType: "Session"
});
}
}
breakDecrement = () => {
if (this.state.breakLength < 2) return;
this.setState((prevState) => ({
breakLength: prevState.breakLength - 1
}));
};
breakIncrement = () => {
if (this.state.breakLength > 59) return;
this.setState((prevState) => ({
breakLength: prevState.breakLength + 1
}));
};
sessionDecrement = () => {
if (this.state.sessionLength < 2) return;
this.setState((prevState) => ({
sessionLength: prevState.sessionLength - 1,
timer: prevState.timer - 60
}));
};
sessionIncrement = () => {
if (this.state.sessionLength > 59) return;
this.setState((prevState) => ({
sessionLength: prevState.sessionLength + 1,
timer: prevState.timer + 60
}));
};
startTimer = () => {
this.interval = setInterval(() => {
this.setState(
(prevState) => ({ timer: prevState.timer - 1, timerState: "start" }),
() => {
if (this.state.timer === 0) {
this.buzzer();
this.stopTimer();
this.startTimer();
}
}
);
}, 1000);
};
stopTimer = () => {
clearInterval(this.interval);
};
toggleTimer = () => {
if (this.state.timerState === "stopped") {
this.startTimer();
} else if (this.state.timerState === "start") {
this.stopTimer();
}
this.setState((prevState) => ({
timerState: prevState.timerState === "stopped" ? "start" : "stopped"
}));
};
resetTimer = () => {
this.stopTimer();
this.setState({
breakLength: 5,
sessionLength: 25,
timer: 1500,
timerType: "Session"
});
this.audioBeep.pause();
};
buzzer = () => {
if (this.state.timer === 0) {
this.audioBeep.play();
}
};
clockify = () => {
const { timer } = this.state;
if (timer < 0) return "00:00";
let minutes = Math.floor(timer / 60);
let seconds = timer - minutes * 60;
seconds = seconds < 10 ? "0" + seconds : seconds;
minutes = minutes < 10 ? "0" + minutes : minutes;
return minutes + ":" + seconds;
};
Three out of five failed tests(12,14,1,) throw this ‘Timer has not reached 00:00.’, but the timer does reach 00:00 (you can check that out for yourself). Can’t point out which part of the code is causing this problem.
The app fulfills all the required criteria in application but still I can’t passs the remaining 4-5 tests in the test suite. I sense a few more amends will do the thing but I’ve been unable to figure them out so far.
What can I do to pass the remaining tests in the test suite?