Exercise Tracker Test 5 and 6

Hi everyone,

I have been stuck on exercise tracker for few days.
Its failing test 5 and 6.

I’ve had a look in date formatting, and setting duration as number.
Its not helping. Are there any other test cases out there?

solution: https://boilerplate-project-exercisetracker.supersyd.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36 .

Challenge: Exercise Tracker

Link to the challenge:

UPDATE: Test 4 was not supposed to be passing!

1: Only create new date in test 4 when undefined or empty, not in test 5 and 6
2: For test 5 and 6, if “to” and “from” query is empty or undefined, do not include them in response

return res.json({
      _id: mongoose.Types.ObjectId(data._id),
      username: data.username,
      count: myArray.length,
      log: myArray
      })

3: If “to” or “from” is not empty or not undefined in query, include one of them or both if they are both not empty or not undefined

return res.json({
      _id: mongoose.Types.ObjectId(data._id),
      username: data.username,
      from: myFromDate,
      count: myArray.length,
      log: myArray
      })
return res.json({
      _id: mongoose.Types.ObjectId(data._id),
      username: data.username,
      to: myToDate,
      count: myArray.length,
      log: myArray
      })
return res.json({
      _id: mongoose.Types.ObjectId(data._id),
      username: data.username,
      from: myFromDate,
      to: myToDate,
      count: myArray.length,
      log: myArray
      })

4: In the testing site, open inspect element in browser settings or press F12 then go to network, and click preview tab

There was an error in test 4 in the add exercise part and it passed in error.

1 Like

Hi,
I have made sure that the _id returned is of ObjectId type.
Still not passing. Let me know of other test cases.

Hello @superSyd ,

Unfornately, I am unable to see your code. If you can share your implementation, that’d be great.

Just in case you wanna look at my implementation, here it is.

router.get('/api/exercise/log', async (req, res) => {
  const { userId, limit, from, to } = req.query;

  try {
    const { _id, username } = await User.findOne({ _id: userId });
    const exercises = await Exercise.find({
      date: {
        $gte: from ? from : 0,
        $lte: to ? to : 'A',
      },
      userId,
    })
      .limit(parseInt(limit))
      .select('description duration date -_id');

    res.json({ _id, username, count: exercises.length, log: exercises });
  } catch (err) {
    res.status(500).send({ error: err.message });
  }
});

Here’s my code

app.get("/api/exercise/log",(req,res)=>{


var myUserID = req.query.userId;
var myFrom = req.query.from;
var myTo = req.query.to;
var myLimit = req.query.limit;

console.log(myUserID, myFrom, myTo, myLimit)

var testFromDate = new Date(myFrom);
var testToDate = new Date(myTo);

var dateFromString,dateToString;
var yearFrom,monthFrom,dateFrom,yearTo,monthTo,dateTo;

var count;

var myLimitOption;
var fromExists = false;
var toExists = false;

  if(myFrom != undefined || myTo != undefined){
    
    if(!isNaN(testFromDate)){
      fromExists = true;
       yearFrom = testFromDate.getFullYear();
      monthFrom = testFromDate.getMonth() + 1;
      dateFrom = testFromDate.getDate();

      dateFromString = yearFrom + "-" + ('0' + monthFrom).slice(-2) + "-" + ('0' + dateFrom).slice(-2);

    }
    
    if (!isNaN(testToDate)){
      // myDate = new Date(parseInt(myDate))
     toExists = true;
      yearTo = testToDate.getFullYear();
      monthTo = testToDate.getMonth() + 1;
      dateTo = testToDate.getDate();

      dateToString = yearTo + "-" + ('0' + monthTo).slice(-2) + "-" + ('0' + dateTo).slice(-2);
    }

  }

  if(myLimit != undefined){
    if(isNaN(parseInt(myLimit))){
     myLimitOption = null
    } else{
      myLimitOption = parseInt(myLimit)
    }
  }

    UserExercise.findById(myUserID, (err,data)=>{

    if(err){
      res.json({
        "error": err
        })
    }

    
    if(data){
      

      var myArray = data.log

      
         if(dateToString == undefined){
         // if(myArray.length > 0){

      dateToString = new Date()
        //     }
         }

    if(dateFromString == undefined){
                 if(myArray.length > 0){

      dateFromString = new Date(data.log[0]["date"])
    }
    } 

      if(!myLimitOption){
        myLimitOption = myArray.length
      }

    myArray = myArray.filter(item => {
      if(toExists){
       return (new Date(item["date"]) <= new Date(dateToString))
      } else {
        return true
      }

    })
    .filter(item => {
      if(fromExists){
        return (new Date(item["date"]) >= new Date(dateFromString))
      } else {
        return true
      }
     
    }
    )
    
    .slice(0, myLimitOption)    

    myArray = myArray.map((item)=>{
      return {
        description: item.description,
        duration: Number(item.duration),
        date: new Date(item.date).toString().substring(0, 15)
      }
    })

    console.log({
      _id: mongoose.Types.ObjectId(data._id),
      username: data.username,
      count: myArray.length,
      log: myArray
      })

    return res.json({
      _id: mongoose.Types.ObjectId(data._id),
      username: data.username,
      count: myArray.length,
      log: myArray
      }) 

    }
    

  })


})

Everything seems to be working okay but the test isn’t validated. Try changing the exercise date format in your db. Save it in the format YYYY-MM-DD. There is a library that helps with that dayjs().format('YYYY-MM-DD').

I think this is what worked for me.

I already applied this.

      year = testDate.getFullYear();
      month = testDate.getMonth() + 1;
      date = testDate.getDate();

dateString = year + "-" + ('0' + month).slice(-2) + "-" + ('0' + date).slice(-2);

Edit: I have applied dayjs library. Not much difference, still failing test 5 and 6. I have added the link to my code below.

Here’s my link to the code
https://replit.com/@superSyd/boilerplate-project-exercisetracker#server.js

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