Open bradleyrichey54 opened 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.
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.
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);
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));
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:
When the same code does pass, it does not have the second line in that table:
Here's the code: