Everything works fine when I test manually., but there are so many failed tests. How should I fix?`
// !! IMPORTANT README:
// You may add additional external JS and CSS as needed to complete the project, however the current external resource MUST remain in place for the tests to work. BABEL must also be left in place.
/***********
INSTRUCTIONS:
- Select the project you would
like to complete from the dropdown
menu.
- Click the "RUN TESTS" button to
run the tests against the blank
pen.
- Click the "TESTS" button to see
the individual test cases.
(should all be failing at first)
- Start coding! As you fulfill each
test case, you will see them go
from red to green.
- As you start to build out your
project, when tests are failing,
you should get helpful errors
along the way!
************/
// PLEASE NOTE: Adding global style rules using the * selector, or by adding rules to body {..} or html {..}, or to all elements within body or html, i.e. h1 {..}, has the potential to pollute the test suite's CSS. Try adding: * { color: red }, for a quick example!
// Once you have read the above messages, you can delete all comments.
class Timer extends React.Component {
constructor(props){
super(props)
}
state={break:5, session:25, timer:false, sessionRunning:true};
interval=null; sessionTime=this.state.session*60; breakTime=this.state.break*60;
displayTime=(time)=>{
let min=parseInt(time/60); let sec=time%60; var displayMin, displaySec;
if(min<10){displayMin=`0${min}`} else {displayMin=min};
if(sec<10){displaySec=`0${sec}`} else {displaySec=sec};
return `${displayMin}:${displaySec}`;
}
breakUp=()=>{
if(this.state.break<60 && this.interval===null){
this.setState({break:this.state.break+1});
}
}
breakDown=()=>{
if(this.state.break>1 && this.interval===null) {this.setState({break:this.state.break-1}); }
}
sessionUp=()=>{
let timeLeftEle=document.getElementById('time-left');
if(this.state.session<60 && this.interval===null){this.setState({session:this.state.session+1},()=>{
timeLeftEle.innerText=this.displayTime(this.state.session*60);
});}
}
sessionDown=()=>{
let timeLeftEle=document.getElementById('time-left');
if(this.state.session>1 && this.interval===null){this.setState({session:this.state.session-1},()=>{
timeLeftEle.innerText=this.displayTime(this.state.session*60);
});}
}
startPause=()=>{
let timeLeftEle=document.getElementById('time-left');
let beep=document.getElementById('beep');
let timeLeft=timeLeftEle.innerText.split(":")[0]*60+timeLeftEle.innerText.split(":")[1]*1;
let startStop=document.getElementById('start_stop');
if(startStop.innerHTML==='<i class="fa-solid fa-play"></i>'){
startStop.innerHTML='<i class="fa-solid fa-pause"></i>'
} else {
startStop.innerHTML='<i class="fa-solid fa-play"></i>'
}
if(this.interval===null){
this.interval=setInterval(()=>{
timeLeft--;
timeLeftEle.innerText=this.displayTime(timeLeft);
if(timeLeft<1 && this.state.sessionRunning){
this.setState({sessionRunning:false});
timeLeft=this.state.break*60;
beep.play();
} else if (timeLeft<1 && !this.state.sessionRunning){
this.setState({sessionRunning:true});
timeLeft=this.state.session*60;
beep.play();
}
},1000)
} else {
clearInterval(this.interval);
this.interval=null;
}
}
resetTime=()=>{
let startStop=document.getElementById('start_stop');
let beep=document.getElementById('beep');
beep.pause();
beep.currentTime=0;
let timeLeftEle=document.getElementById('time-left');
startStop.innerHTML='<i class="fa-solid fa-play"></i>';
if(this.interval!==null){
clearInterval(this.interval);
this.interval=null;
}
this.setState({break:5, session:25, sessionRunning:true},()=>{
timeLeftEle.innerText=this.displayTime(this.state.session*60);
});
}
render(){
return (
<div id='wrapper'>
<div id='description'>25+5 Clock</div>
<div id='break' className='lengthWrapper'>
<div className='title' id='break-label'>break length</div>
<div className='length' id='break-length'>{this.state.break}</div>
<div className='arrowWrapper'>
<a className='arrow' id='break-increment' onClick={this.breakUp}><i class="fa-solid fa-arrow-up"></i></a>
<a className='arrow' id='break-decrement' onClick={this.breakDown}><i class="fa-solid fa-arrow-down"></i></a>
</div>
</div>
<div id='session' className='lengthWrapper'>
<div className='title' id='session-label'>session length</div>
<div className='length' id='session-length'>{this.state.session}</div>
<div className='arrowWrapper'>
<a className='arrow' id='session-increment' onClick={this.sessionUp}><i class="fa-solid fa-arrow-up"></i></a>
<a className='arrow' id='session-decrement' onClick={this.sessionDown}><i class="fa-solid fa-arrow-down"></i></a>
</div>
</div>
<div id='display'>
<div id='timer-label'>{this.state.sessionRunning?'Session':'Break'}</div>
<div id='time-left'>{this.displayTime(this.state.session*60)}</div>
<audio id='beep' src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/audio/BeepSound.wav" preload='auto'>Cannot access the audio</audio>
<div id='buttonWrapper'>
<button id='start_stop' onClick={this.startPause}><i class="fa-solid fa-play"></i></button>
<button id='reset' onClick={this.resetTime}>Reset</button>
</div>
</div>
</div>
)
}
}
const root=ReactDOM.createRoot(document.querySelector('#root'));
root.render(<Timer />);