Tell me what the problem is. If the lines are the same, then "1", and if not, then undefined. And I need to true or false.

function slowStringComparison(givenSignature, computedSignature, cb) { if (!givenSignature || !computedSignature || givenSignature.length !== computedSignature.length) { return cb(false); } var n = computedSignature.length; var signaturesMatch = true; for (var i = 0; i < n; i++) { signaturesMatch &= (computedSignature.charAt(i) == givenSignature.charAt(i)); } return cb(null, signaturesMatch);} 
  • 2
    The essence of bit expressions is that they return a number. If boolean value is needed, use logical && instead of bitwise - Grundy
  • 2
    At the expense of slowStringComparison - you got excited. in your cycle, all the same thing happens, the only thing is that there is no check for null. Well, javascript has long allowed to access the string symbol by index computedSignature[i] instead of calling charAt - Grundy
  • Thanks, did so signaturesMatch = computedSignature [i] == givenSignature [i]; - Pantera
  • one
    bad - so each time the value will be overwritten and as a result there will be only the result of the last comparison. That is, if everyone except the last character is different - signaturesMatch will still show true after the cycle - Grundy
  • one
    check the value of signaturesMatch - if false - do a break to the loop, since there is no sense to check further. Or use the logical &&: signaturesMatch = signaturesMatch && computedSignature[i] == givenSignature[i]; - Grundy

1 answer 1

There is a much simpler way to compare two strings in node.js. To do this, use the Buffer class, and its Buffer.equals method:

 function slowStringComparison(givenSignature, computedSignature, cb) { var givenBuff = Buffer.from(givenSignature), computedBuff = Buffer.from(computedSignature); cb(givenBuff.equals(computedBuff)); }