Guide: Timestamp Microservice - the solution looks wrong

I found that the solution in http://forum.freecodecamp.org/t/freecodecamp-challenge-guide-timestamp-microservice/301508 was wrong.

app.get("/api/timestamp/:date_string", (req, res) => {
  let dateString = req.params.date_string;

  //A 4 digit number is a valid ISO-8601 for the beginning of that year
  //5 digits or more must be a unix time, until we reach a year 10,000 problem
  if (/\d{5,}/.test(dateString)) {
    dateInt = parseInt(dateString);
    //Date regards numbers as unix timestamps, strings are processed differently
    res.json({ unix: dateString, utc: new Date(dateInt).toUTCString() });
  }

  let dateObject = new Date(dateString);

  if (dateObject.toString() === "Invalid Date") {
    res.json({ error: "Invaid Date" });
  } else {
    res.json({ unix: dateObject.valueOf(), utc: dateObject.toUTCString() });
  }
});

Above is the part of solution having problem. It couldn’t pass the test by some issues(variable declaration, lack of else, and typo).
I think it should be like following.

app.get("/api/timestamp/:date_string", (req, res) => {
  let dateString = req.params.date_string;

  //A 4 digit number is a valid ISO-8601 for the beginning of that year
  //5 digits or more must be a unix time, until we reach a year 10,000 problem
  if (/\d{5,}/.test(dateString)) {
    let dateInt = parseInt(dateString);
    //Date regards numbers as unix timestamps, strings are processed differently
    res.json({ unix: dateString, utc: new Date(dateInt).toUTCString() });
  } else {
    let dateObject = new Date(dateString);

    if (dateObject.toString() === "Invalid Date") {
      res.json({ error: "Invalid Date" });
    } else {
      res.json({ unix: dateObject.valueOf(), utc: dateObject.toUTCString() });
    }
  }
});

I didn’t know where is the proper reporting form(couldn’t reply to guide), so let me post here.
Thanks!

1 Like

I’ve edited your post so that those that do not want to see a working solution. If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

Hi, I am not sure if the challenge has changed this year but does that above updated solution still hold valid? If it has not then the Unix value returned for when the regex condition passes (5 digits or more) should be a number type rather than string? I had to change this and it passed all the tests.

Welcome, deanp.

All of the backend projects have changed in some way, within the last year. However, I am not sure in what way the Timestamp project has.

I am not really sure why there is a solution like this for the Timestamp Microservice project.