Modbus crc16 calculation function error syntax

The issue is monaco (trys) to help too much some times.

It is essentially pointing out the code is not great - the function returns a number, then converts to string then it is split to an array of characters. Technically, it works, but it is ugly.

Here is a very quick fix up...

image

function crc16(str) {
    const buf = Buffer.from(str, 'hex')
    let crc = 0xFFFF;
    for (let i = 0; i < buf.length; i++) {
        crc = crc ^ buf[i];
        for (let j = 0; j < 8; j++) {
            const temp = crc & 0x01;
            crc >>= 0x01;
            if (temp == 0x01) {
                crc ^= 0xA001;
            }
        }
    }
    return crc;
}

const crc = crc16(msg.payload)
const arr = crc.toString(16).toUpperCase().substring(-4).split("")
msg.payload = msg.payload + arr.splice(-2).concat(arr).join("");
return msg;