function checkExamCode(){

        // send exam code to server
        axios.get('/api/exams/examByCode?exam_code='+exam_code)
        .then(function (response) {
            // if exam code is right
            console.log(response);
            let date_string = response.data.date_time_start;
            const exam_date_time_start = new Date(date_string);
            const exam_date_time_end = moment(exam_date_time_start).add(response.data.duration, 'm').toDate();
            const curr_date_time = new Date();

            // if exam has begun but not ended, then allow user to enter
            if(curr_date_time >= exam_date_time_start && curr_date_time < exam_date_time_end){
                
              // calculate time remaining
                var diff = Math.abs(exam_date_time_end - curr_date_time);
                var diff_mins = Math.floor((diff/1000)/60);
                var diff_secs = Math.floor(diff/1000)%60;
                console.log(diff, diff_mins, diff_secs);
                setError("Starting exam");
                // pass data to exam page and start the exam
                let data={
                    exam_code: exam_code,
                    student_name: props.name,
                    student_email: props.student_email,
                    exam_link: response.data.exam_link,
                    prof_email: response.data.prof_email,
                    mins_left: diff_mins,
                    secs_left: diff_secs,
                };
                history.push({ 
                    pathname: '/test',
                    state: data
                })
                 
            }

            // if current time is after exam end time, show error
            else if(curr_date_time >= exam_date_time_end){
                setError("Exam has already ended");
            }

            // if current time is before exam start, show error
            else {
                setError("Exam has not started now");
            }
          })

          .catch(function (error) {
            // if exam code is invalid show error
            console.log(error);
            setError("Exam code is invalid");
          });
    }
/**
     * This function is called when student enters exam code to start the exam
     * It checks the exam code, if invalid it displays error
     * If valid, it uses the start time and duration to find if exam is running or not
     * If running it allows student to enter, else displays error
     */
This function is called when student enters exam code to start the exam
It checks the exam code, if invalid it displays error
If valid, it uses the start time and duration to find if exam is running or not
If running it allows student to enter, else displays error
This function is called when student enters exam code to start the exam It checks the exam code if invalid it displays error If valid it uses the start time and duration to find if exam is running or not If running it allows student to enter else displays error
function checkExamCode(){
        axios.get('/api/exams/examByCode?exam_code='+exam_code)
        .then(function (response) {
            console.log(response);
            let date_string = response.data.date_time_start;
            const exam_date_time_start = new Date(date_string);
            const exam_date_time_end = moment(exam_date_time_start).add(response.data.duration, 'm').toDate();
            const curr_date_time = new Date();
            if(curr_date_time >= exam_date_time_start && curr_date_time < exam_date_time_end){
                var diff = Math.abs(exam_date_time_end - curr_date_time);
                var diff_mins = Math.floor((diff/1000)/60);
                var diff_secs = Math.floor(diff/1000)%60;
                console.log(diff, diff_mins, diff_secs);
                setError("Starting exam");
                let data={
                    exam_code: exam_code,
                    student_name: props.name,
                    student_email: props.student_email,
                    exam_link: response.data.exam_link,
                    prof_email: response.data.prof_email,
                    mins_left: diff_mins,
                    secs_left: diff_secs,
                };
                history.push({ 
                    pathname: '/test',
                    state: data
                })
            }
            else if(curr_date_time >= exam_date_time_end){
                setError("Exam has already ended");
            }
            else {
                setError("Exam has not started now");
            }
          })
          .catch(function (error) {
            console.log(error);
            setError("Exam code is invalid");
          });
    }
function checkExamCode ( ) { axios . get ( '/api/exams/examByCode?exam_code=' + exam_code ) . then ( function ( response ) { console . log ( response ) ; let date_string = response . data . date_time_start ; const exam_date_time_start = new Date ( date_string ) ; const exam_date_time_end = moment ( exam_date_time_start ) . add ( response . data . duration , 'm' ) . toDate ( ) ; const curr_date_time = new Date ( ) ; if ( curr_date_time >= exam_date_time_start && curr_date_time < exam_date_time_end ) { var diff = Math . abs ( exam_date_time_end - curr_date_time ) ; var diff_mins = Math . floor ( ( diff / 1000 ) / 60 ) ; var diff_secs = Math . floor ( diff / 1000 ) % 60 ; console . log ( diff , diff_mins , diff_secs ) ; setError ( "Starting exam" ) ; let data = { exam_code : exam_code , student_name : props . name , student_email : props . student_email , exam_link : response . data . exam_link , prof_email : response . data . prof_email , mins_left : diff_mins , secs_left : diff_secs , } ; history . push ( { pathname : '/test' , state : data } ) } else if ( curr_date_time >= exam_date_time_end ) { setError ( "Exam has already ended" ) ; } else { setError ( "Exam has not started now" ) ; } } ) . catch ( function ( error ) { console . log ( error ) ; setError ( "Exam code is invalid" ) ; } ) ; }
This function is called when student enters exam code to start the exam
 It checks the exam code, if invalid it displays error
 If valid, it uses the start time and duration to find if exam is running or not
 If running it allows student to enter, else displays error
This function is called when student enters exam code to start the exam It checks the exam code if invalid it displays error If valid it uses the start time and duration to find if exam is running or not If running it allows student to enter else displays error
// send exam code to server // if exam code is right // if exam has begun but not ended, then allow user to enter // calculate time remaining // pass data to exam page and start the exam // if current time is after exam end time, show error // if current time is before exam start, show error // if exam code is invalid show error