admin管理员组文章数量:1322032
I am practicing to code in various languages and hence, am a newbie to node.js. The site I am using to practice the code mostly gives me multi-line inputs as an argument to my function, which I don't know how to process (I tried using split on \n, but, that, doesn't work).
Following is a code that, gets multi-line input and then, this input is passed to a function. Can you please tell me how can I read/process the input in-order to store each line of an input in an array as a data item ?
function main(input) {
//Enter your code here
// var arr = input.split("")
process.stdout.write(input[6]);
}
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input;
});
process.stdin.on("end", function () {
main(stdin_input);
});
Thanks'
I am practicing to code in various languages and hence, am a newbie to node.js. The site I am using to practice the code mostly gives me multi-line inputs as an argument to my function, which I don't know how to process (I tried using split on \n, but, that, doesn't work).
Following is a code that, gets multi-line input and then, this input is passed to a function. Can you please tell me how can I read/process the input in-order to store each line of an input in an array as a data item ?
function main(input) {
//Enter your code here
// var arr = input.split("")
process.stdout.write(input[6]);
}
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input;
});
process.stdin.on("end", function () {
main(stdin_input);
});
Thanks'
Share Improve this question asked Oct 16, 2017 at 4:20 sulabh chaturvedisulabh chaturvedi 3,9943 gold badges15 silver badges26 bronze badges2 Answers
Reset to default 6Splitting on a new line works for me.
function main(input) {
//Enter your code here
var arr = input.split("\n")
process.stdout.write(JSON.stringify(arr));
}
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input;
});
process.stdin.on("end", function () {
main(stdin_input);
});
It's important to note that process.stdout.write
can only write a string. Trying to pass an array as an argument will cause an error.
just a idea my code is just for many string or number have space between for example if you want sum two number we write in terminal : 23 56
notice i use here string_decoder for any one want to raplace number with string
const {StringDecoder} = require('string_decoder');
const decode = new StringDecoder('utf8');
const sum = (a, b) => {
let operation = a + b;
return console.log('result is : ', operation);
}
process.stdin.on('readable', () => {
const aa = process.stdin.read(); // read string from REPL
const buffer = Buffer.from(aa);
const j = decode.write(buffer).split(' ');
const a = +j[0];
const b = +j[1];
// console.log(a + b)
if((a & b) != null) // check if the value not empty
{
sum(a, b);
}
});
版权声明:本文标题:Javascript (Node.js) - How to read and process multiple line provided as an input to a function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742108762a2421145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论