简体   繁体   中英

Multiple values on same index

So I am doing a small code test at a place called Experis. And while the assigment itself was fine, the problem lies when I try to fetch the input from their servers. There are multiple inputs that needs to be tested, however instead of being sent in an array, they are all sent at once. Which itself is fine, I just use " line.split(' ') " to split every sentence into its own word. However after I try to call the first part with "line[0] " I get a very weird result, instead of just sending the first word, it sends the first letter of every word. The inputs are:

at ordeals abcdefghijklmnopqrstuvwxyz abcdefghijklmabcdefghijklm abcdABCDabcd

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on('line', (line) => {
    var nums = line.split(' ');
    
    console.log(nums);
});

Expected result: at

Actual result: aoaaa

I have tried to parse the data, turn it into an array, using different kinds of spliting, but nothing I try seem to work properly. I have contacted their support team, but since it is weekend they don't seem to answer. And at this point I am desperate for help.

Edit:

logging line gave me at ordeals abcdefghijklmnopqrstuvwxyz abcdefghijklmabcdefghijklm abcdABCDabcd in type string,string,string,string,string.

While logging nums gives me: [ 'at' ] [ 'ordeals' ] [ 'abcdefghijklmnopqrstuvwxyz' ] [ 'abcdefghijklmabcdefghijklm' ] [ 'abcdABCDabcd' ] in type object,object,object,object,object. Which makes it so that if I log nums[0] then I get the entirety of input again in string,string,string,string,string.

try somthing like that

var nums = line.split(/\s/);

  1. Try logging what is JSON.stringify({line}) and JSON.stringify({nums})
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on('line', (line) => {
    var nums = line.split(' ');
    
    // expected: '{line: "at ordeals ... " }'
    console.log(JSON.stringify({line}));
    // expected: '{nums: ["at", "ordeals", ...] }'
    console.log(JSON.stringify({nums}));
});

JSON and object is to check if you understand its correctly and escape \n, \t, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM