-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d0eb3f
commit 38e6803
Showing
1 changed file
with
15 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,17 @@ | ||
<h1>This doc shows how to input output for javascript in Node environment</h1> | ||
|
||
Case #1. Single line input | ||
```js | ||
const readline = require('readline'); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
rl.on('line', function (line) { | ||
console.log(line) | ||
}); | ||
``` | ||
|
||
Case #2: Multiple lines input, number of line is certain: | ||
```js | ||
const readline = require('readline'); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
// number of lines to be input: | ||
let num = 2; | ||
let inputs =[]; // Array to store all the lines | ||
rl.on('line', function (line) { | ||
inputs.push(line); | ||
if(inputs.length === num){ | ||
// all lines are input done, start processing them | ||
console.log(inputs[0].substr(0,parseInt(inputs[1]))); | ||
// Clean the array so that it can store next group lines | ||
inputs = []; | ||
} | ||
}); | ||
``` | ||
|
||
case #3: Multiple lines input, first line is the line number, subsequent lines are real data: | ||
```js | ||
const readline = require('readline'); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
let num = 0; | ||
let inputs = []; | ||
rl.on('line', function (line) { | ||
if(num === 0){ | ||
num = parseInt(line) | ||
}else{ | ||
inputs.push(line.toLocaleLowerCase()); | ||
} | ||
if(inputs.length === num){ | ||
for(let item of inputs){ | ||
//... | ||
//Process all the lines in input array | ||
// clean for next group | ||
num = 0; | ||
inputs = []; | ||
} | ||
} | ||
}); | ||
``` | ||
Case #4, multiple lines input, line number is uncertain: | ||
```js | ||
const readline = require('readline'); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
|
||
let inputs = []; | ||
|
||
rl.on('line', function (line) { | ||
inputs.push(line); | ||
} | ||
); | ||
// 输入结束时,在 close 中对数据进行处理 | ||
rl.on('close', function () { | ||
|
||
<h1>How to reverse a long sentence and then each world</h1> | ||
|
||
```ts | ||
function reverseChunks(input: string): Promise<string>[] { | ||
let arrs = input.split('').reverse().join('').split(' '); | ||
return arrs.map(chunk => new Promise<string>((resolve) => { | ||
resolve(chunk.split('').reverse().join('')); | ||
})); | ||
} | ||
|
||
let input : string = 'this is a long string that will be split into chunks and each chunk will be reversed'; | ||
const promises = reverseChunks(input); | ||
Promise.all(promises).then(results => { | ||
let ret: string = results.join(' '); | ||
console.log(ret); | ||
}); | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|