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!