nodeschool / discussions

:school::speech_balloon: need help with nodeschool? or just wanna ask a question? open an issue on this repo!
489 stars 107 forks source link

datetime error #2911

Open lws-xzx opened 1 year ago

lws-xzx commented 1 year ago

Notes

Please delete this section after reading it

If you have a problem with an error message:

Error output

 你提交的结果与预期结果的比较如下:

                实际结果                                预期结果
────────────────────────────────────────────────────────────────────────────────

   "2023-09-10 17:26"                  ==    "2023-09-10 17:26"
                                       !=    ""

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

 ✗

 提交的结果不符合预期!

 # 失败 你对 授时服务器 所作的答案未通过验证, 请再试一次!

My Code

 const net = require("net");

const server = net.createServer(function (socket) {
    let data = ""

    const date = new Date()
    const year = date.getFullYear().toString().padStart(4, '0')
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    const hour = date.getHours().toString().padStart(2, '0')
    const minute = date.getMinutes().toString().padStart(2, '0')

    data = year + '-' + month + '-' + day + ' ' + hour + ":" + minute

    socket.end(data)
})
server.listen(process.argv[2], () => {

})
lws-xzx commented 1 year ago

add '\n'

`const net = require("net");

const server = net.createServer(function (socket) { let data = ""

const date = new Date()
const year = date.getFullYear().toString().padStart(4, '0')
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const hour = date.getHours().toString().padStart(2, '0')
const minute = date.getMinutes().toString().padStart(2, '0')

data = year + '-' + month + '-' + day + ' ' + hour + ":" + minute

socket.end(data + '\n')

}) server.listen(process.argv[2], () => {

})`

mahdi-eth commented 11 months ago

To rectify the discrepancy, it's recommended to utilize the .trim() method to remove any potential leading or trailing whitespace from the output. This ensures an exact match between the produced output and the expected result.

const net = require("net");

const server = net.createServer(function (socket) {
    let data = "";

    const date = new Date();
    const year = date.getFullYear().toString().padStart(4, '0');
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');

    data = year + '-' + month + '-' + day + ' ' + hour + ":" + minute;

    socket.end(data.trim()); // Implementing trim() to remove possible whitespace
});

server.listen(process.argv[2], () => {});

This adjustment should resolve the discrepancy observed during the comparison.