Excercise tracker fourth test

I cannot get my exercise tracker app to pass test 4:

My response looks exactly like the example response and seems to fit the criteria of the test, but it will not pass.

Code:

app.post("/api/exercise/add", (req, res) => {
  let newDate = 0;

  if (req.body.date == "") {
    newDate = new Date().toLocaleDateString(undefined, {
      weekday: "short",
      month: "short",
      day: "numeric",
      year: "numeric"
    });
  } else {
    newDate = new Date(req.body.date).toLocaleDateString(undefined, {
      weekday: "short",
      month: "short",
      day: "numeric",
      year: "numeric"
    });
  }

  User.findOneAndUpdate(
    { _id: req.body.userId },
    {
      $push: {
        log: {
          date: newDate.replace(/,/g, ''),
          duration: parseInt(req.body.duration),
          description: req.body.description
        }
      }
    },
    { new: true, upsert: true },
    (err, user) => {
      res.send({_id: user._id, username: user.username, ...user.log[user.log.length - 1]});
    }
  );
});

Output Json:

{"_id":“606219cf581fbe3de1a0626e”,“username”:“snag”,“date”:“Sat Jun 11 1977”,“duration”:20,“description”:“Running”}

Is there a different format for the object keys that it is looking for? Is there somewhere that I can find more detailed descriptions of the test criteria than what is provided?

Project link: link

My code: GitHub repo

I had this same problem. In short, it looks like your if statement only checks for an empty string, but doesn’t check for undefined. It should generate a new current date even when undefined.

Also maybe for next post but it would be useful to include a link to your code next time for people to try and help you.

Let us us know if you manage!

I have updated the If statement to include undefined and “Invalid Date”, but it stilll doesn’t pass. I have also added a link to the GitHub repo of my code.

Any way you could link your code for me to look at it?

I added it above:

My code: GitHub repo

Hello there,

You might be complicating the logic a bit much. From what I remember, it was enough to say:

const dateToReturn = new Date(req.body.date).toDateString();

WIth error checking, obviously.

Hope this helps

Ha! I swear that that is the first way that I tried it, but found the Date format I got was very different. This is indeed the fix and now I pass the fourth test!

1 Like

Ohh Looks like you have your fix then! I was trying to figure it out and got caught up in a mistake I’ve made trying to host it… That’s great! :slight_smile:

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