-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (43 loc) · 1.29 KB
/
index.js
File metadata and controls
54 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var input = require('fs').readFileSync('./stdin.txt', 'utf8');
var lines = input.split('\r\n');
let numbers = [];
let words = []
let output = [];
for (const line of lines) {
if(line && +line >= 0){
numbers.push(+line)
}
else {
words.push(line);
}
}
numbers.pop();
// console.log('>>> numbers', numbers);
let indexOld = 0;
for (const number of numbers) {
// console.log('>>> number', number);
// console.log('>>> indexOld', indexOld);
let maxSpaces = 0;
for (let i = indexOld; i < (number + indexOld); i++) {
// console.log('>>> words[i]', words[i]);
maxSpaces = maxSpaces < words[i].length ? words[i].length : maxSpaces;
}
for (let i = indexOld; i < (number + indexOld); i++) {
if(words[i].length < maxSpaces){
let diff = maxSpaces - words[i].length;
let newWord = ''
for (let index = 0; index < diff; index++) {
newWord += ' ';
}
newWord += words[i];
words[i] = newWord;
}
output.push(words[i]+'\n')
}
output.push('\n')
indexOld += number;
}
output = output.join('').substring(0, output.join('').length - 2)
// console.log('>>> numbers', numbers);
// console.log('>>> words', words);
console.log(output);