Answer the question
In order to leave comments, you need to log in
Why does the string length not match?
Good afternoon. I receive input from the console via stdin. I enter, for example, 12. I take the length of the string, it shows 4. Please tell me why 4 and not 2? And how to make it so that it was 2. Code:
process.stdin.on('readable', () => {
let input_data
input_data = process.stdin.read()
console.log(input_data.length)
});
Answer the question
In order to leave comments, you need to log in
The resulting buffer is as follows:
The last two values are service characters (not sure about the penultimate one)
Solution:
- specify the encoding process.stdin.setEncoding('utf-8');
- trim extra characters input_data .trim()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question