question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timestamp project | "An empty date parameter should return the current time in a JSON object with a utc/unix key" test doesn't work correctly

See original GitHub issue

Hello. In the timestamp project, the results of all tests are successful, except for the data value tests: “An empty date parameter should return the current time in a JSON object with a unix key An empty date parameter should return the current time in a JSON object with a utc key”

I did some research and the logic in your test is as follows:

(getUserInput) =>
  $.get(getUserInput('url') + '/api').then(
    (data) => {
      var now = Date.now();
      assert.approximately(data.unix, now, 20000);
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

When the difference between the current time specified in the test and the time in the response is large, the test fails, ie if the difference is greater than 20,000, the result fails (as it usually does). To check, I added +20000 to the current time I set in the code and the test was successful.

Affected page

Challenge page

Test page

Your code


//  empty date parameter
app.get("/api", (req, res)=>{
  let date = new Date();
  let UTC = date.toUTCString();
  let UNIX = date.getTime();
  res.json({ unix: UNIX, utc: UTC });
})


Expected behavior

the test must be successful

Screenshots

my result: image

System

  • Device: Laptop
  • OS: Windows 10
  • Browser: edge
  • Version: 99.0.1150.30

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ojeytonwilliamscommented, Mar 8, 2022

Hi @ebdurrehm I forked your repl (https://replit.com/@ojeytonwilliams/boilerplate-project-timestamp-1#server.js) and converted it to use

app.get("/api", (req, res)=>{
  let date = new Date();
  let UTC = date.getTime();
  UTC = new Date(UTC);
  UTS = UTC.toUTCString();
  let UNIX = date.getTime();
  res.json({ unix: UNIX, utc: UTS });
})

and it passes all the tests. If I instead increased it from + 20000 to + 25000 it fails fairly reliably. This is to be expected, since assert.approximately(actual, expected, delta) should fail when actual - expected > 20000.

If you haven’t already, it might be worthwhile to discuss this on the forum.

0reactions
xylvnkingcommented, Aug 27, 2022

In case anybody else ends up here, I couldn’t get this to pass either, ended up here too, and used @ojeytonwilliams answer but removed the +20000 and it passed after submitting it a few times. I’ve had trouble with lots of the freeCodeCamp + replit tests not passing unless submitted multiple times, so always try a few times (in my experience). Thanks for the help, and the free education.

app.get("/api", (req, res)=>{
  let date = new Date();
  let UTC = date.getTime();
  UTC = new Date(UTC);
  UTS = UTC.toUTCString();
  let UNIX = date.getTime();
  res.json({ unix: UNIX, utc: UTS });
})

(code for reference)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Please help - APIs and Microservices Projects - JavaScript
An empty date parameter should return the current time in a JSON object with a 'utc' key. Your code so far app.get("/api/timestamp", (req,...
Read more >
gmdate - Manual - PHP
gmdate(string $format , ?int $timestamp = null ): string. Identical to the date() function except that the time returned is Greenwich Mean Time...
Read more >
Gemini REST API Reference
a JSON array. Each element contains a payload that will be described. Timestamps. The timestamp data type describes a date and time as...
Read more >
Pulse One API Client Specification - Product Documentation
The information in this document is current as of the date on the title ... correctly, a JSON body containing a User Entity...
Read more >
Handling Time Zone in JavaScript - TOAST UI - Medium
Implementing time zone features beyond simple formatting of time and calculating time data with complex operations (e.g. calendar) was a truly daunting task....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found