Javascript Timestamp

Here again. My timestamp solution works perfectly. It returns everything as it exactly supposed to do. I asked this question previously and the person who was of most help also could not see why the code wasn’t passing the two tests. IT passes all tests except the last two. Even though it should.

Why isn’t my code passing the last two tests of the timestamp project in API’s and Microservices?
Here is the code:



// your first API endpoint... 
app.get("/api/", (req,res) =>{
  const date = new Date();
  date.setMinutes(date.getMinutes()-3);
  res.json({ "unix": date.valueOf(), "utc": date.toUTCString()})
});

app.get("/api/:date?", function(req, res) {
  
  let  date_params = new Date(req.params.date);

  if(date_params.toString() == "Invalid Date"){
    date_params = new Date(parseInt(req.params.date));
  }
  
  if(date_params.toString() !== "Invalid Date"){
    res.json({ "unix": date_params.getTime(), "utc": date_params.toUTCString()});
  } else{
    res.json({ "error": "Invalid Date"});
  }
});

Please give a link to the challenge.

@hbar1st means the link to your live project code I believe. I bootstrapped what you posted into another repl. The code fails the empty date parameter test, so start with that route and log the inputs and responses:

req.body: undefined
req.params: {}
req.query: {}
{ unix: 1663507911802, utc: 'Sun, 18 Sep 2022 13:31:51 GMT' }
req.body: undefined
req.params: {}
req.query: {}
{ unix: 1663507912001, utc: 'Sun, 18 Sep 2022 13:31:52 GMT' }

So, it’s hitting the route but failing. Progress. You manipulate the date in the route, so let’s debug that:

req.body: undefined
req.params: {}
req.query: {}
original date: Sun Sep 18 2022 13:36:42 GMT+0000 (Coordinated Universal Time)
modified date: Sun Sep 18 2022 13:33:42 GMT+0000 (Coordinated Universal Time)
{ unix: 1663508022765, utc: 'Sun, 18 Sep 2022 13:33:42 GMT' }
req.body: undefined
req.params: {}
req.query: {}
original date: Sun Sep 18 2022 13:36:42 GMT+0000 (Coordinated Universal Time)
modified date: Sun Sep 18 2022 13:33:42 GMT+0000 (Coordinated Universal Time)
{ unix: 1663508022955, utc: 'Sun, 18 Sep 2022 13:33:42 GMT' }

Dates are modified and they are 3 minutes apart now and the test fails. I can’t remember the tolerances on the tests, but it’s less than 3 minutes.

The question now is why are you altering the timestamps by three minutes?

Thank you so much! Removed that 3 second delay I originally put. I had it there because at first nothing worked in my earlier versions of code unless I put a certain time delay on my code (my logic had something to do with freecode camp processing speed). It worked for my code back then but after re-writing a bunch of times it seems I didn’t need it anymore. thanks alot! everything works!

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