workshopper / learnyounode

Learn You The Node.js For Much Win! An intro to Node.js via a set of self-guided workshops.
Other
7.28k stars 1.84k forks source link

Time Server sometimes passes, sometimes fails the same code. #341

Open bradleyrichey54 opened 9 years ago

bradleyrichey54 commented 9 years ago

I'm running the exact same solution multiple time immediately one after another. Sometimes the solution passes, sometimes it does not. I'm running OS X Yosemite 10.10.4. When it doesn't pass, it gives this message:

 Your solution to TIME SERVER didn't pass. Try again!

Your submission results compared to the expected:

                 ACTUAL                                 EXPECTED                
    ──────────────────────────────────────────────    ──────────────────────────────────

   "2015-08-12 11:47"                  ==    "2015-08-12 11:47"                 
                                       !=    ""                                 

When the same code does pass, it does not have the second line in that table:

Your submission results compared to the expected:

                ACTUAL                                 EXPECTED                
  ────────────────────────────────────────────────────────────────────────────────

  "2015-08-12 11:47"                  ==    "2015-08-12 11:47"                 

  ────────────────────────────────────────────────────────────────────────────────

   ✓ Submission results match expected

Here's the code:

var net = require('net');
var server = net.createServer(function(socket){
    var date = new Date();
    socket.write(dateFormat(date));
    socket.end();
});
server.listen(process.argv[2]);

function dateFormat(date){
    var actualMonth = date.getMonth() + 1;
    var output = "";
    output += date.getFullYear();
    output += '-';
    output += actualMonth > 9 ? actualMonth : '0' + actualMonth;
    output += '-';
    output += date.getDate() > 9 ? date.getDate() : '0' + date.getDate();
    output += ' ';
    output += date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
    output += ':';
    output += date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
    return output;
   }
anjanjanj commented 9 years ago

I'm wondering the same thing. My code is similar to yours and passes sometimes when there is no second line. In the official solution they appended a newline to the end of the write, but it's not mentioned as a requirement in the exercise description.

jpdamon commented 8 years ago

The test does specify a newline in the first paragraph:

Your server should listen to TCP connections on the port provided by the first argument to your program. For each connection you must write the current date & 24 hour time in the format: "YYYY-MM-DD hh:mm" followed by a newline character. Month, day, hour and minute must be zero-filled to 2 integers.

However there is definitely something going on here. If I use your code and append a newline, the test will pass everytime. But omitting the newline will cause random failures.

BASEhead2528 commented 6 years ago

I had the same issue, the new line was missing: My code:

const port = process.argv[2] const net = require('net'); var moment = require('moment');

var server = net.createServer((socket) => { socket.end(moment().format('YYYY-MM-DD HH:mm') + "\n"); });

server.listen(port);

ddoyediran commented 1 year ago

This works for me:

const net = require("net");

const port = process.argv[2];

const server = net.createServer(function (socket) {
  let date = new Date(); // create date instance
  // create the current date
  let data = `${date.getFullYear()}-${
    date.getMonth() + 1
  }-${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;

  // write the data to the socket
  socket.write(data + "\n");

  // close the socket
  socket.end();
});

server.listen(parseInt(port));