Feedback on Exercise Tracker Filter part

I’ve just finished my exercise tracker project and everything works fine but the problem is that most of the time I get “test timed out” on filtering the exercise log

So my main questions are:

  • Is it because my way of filtering is too slow?
  • If so, what is a better way to do the filtering?

Source code: https://glitch.com/edit/#!/exercise-tracker-moon
Demo: https://exercise-tracker-moon.glitch.me/

This is my code on logging and filtering:

router.get("/log", async (req, res) => {
  try {
    const {userId, from, to, limit} = req.query;
    const user = await User.findById(userId);
    
    if(user) {
      var resObj = {
        _id: user._id,
        username: user.username
      };
      var log = await Exercise.find({userId})
                              .select('-_id description duration date')
                              .exec();
      
      //Filter...
      if(from) {
        let fromDate = new Date(from);
        log = log.filter(each => each.date.getTime() >= fromDate.getTime())
        resObj.from = fromDate.toDateString();
      }
      
      if(to) {
        let toDate = new Date(to);
        log = log.filter(each => each.date.getTime() <= toDate.getTime());
        resObj.to = toDate.toDateString();
      }
      
      if(limit) {
        log = log.slice(0, limit)
      }
      
      //Clean log data
      log = log.map(eachObj => {
        return {
          description: eachObj.description,
          duration: eachObj.duration,
          date: eachObj.date.toDateString()
        }
      })
      
      resObj.count = log.length;
      resObj.log = log;
      console.log("\n\n", resObj);
      
      return res.json(resObj);
    }
    throw Error("Unknown UserId!")
  }
  catch(err) {
    console.log("error...", err.message)
    return res.json({'error': err.message});
  }
})

Link to the Challenge: FCC Exercise Tracker Project

I tried to submit your link multiple times and some times it failed, but always for a different test case. And judging by request times I’d say it’s something with the hosting platform, because I took a quick look at your code and couldn’t see any obvious reasons for it to be slow :man_shrugging:

1 Like

Thanks for having a look @jenovs. Just to clarify, my code is okay, right? and when you submit my link, the test that failed was “test timed out”, is that right?

At quick glance I couldn’t see anything obviously wrong.

I didn’t see any “timed out” message, but in the network tab I could see that while all the test requests were quite slow some of them were particularly slow.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.