-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathpunched_cards.js
More file actions
47 lines (46 loc) · 1.53 KB
/
punched_cards.js
File metadata and controls
47 lines (46 loc) · 1.53 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
//Requirements to read the input file
const fs = require('fs');
const inputFile = fs.readFileSync(0, 'utf8').trim().split('\n');
// Function definition
const punchedCards = (input) => {
// Declaring variables
let testCases = input[0],
cols = 0,
rows = 0,
caseNum = 1;
// Loop for each case
while (caseNum <= testCases) {
// Saving rows and columns numbers and create the matrix
rows = input[caseNum].split(' ')[0] * 2;
cols = input[caseNum].split(' ')[1] * 2;
let card = [];
// Loop to circle each row
for (let i = 0; i <= rows; i++) {
card.push(['']);
// Second loop to circle each column
if (i % 2 == 0) {
for (let j = 0; j <= cols; j++) {
card[i][j] = ((j % 2 == 0)? '+' : '-');
}
}
else {
for (let j = 0; j <= cols; j++) {
card[i][j] = ((j % 2 == 0)? '|' : '.');
}
}
}
// Replacing the top-left cell
card[0][0] = '.';
card[0][1] = '.';
card[1][0] = '.';
card[1][1] = '.';
// Printing the result
console.log(`Case #${caseNum}:`);
for (let i = 0; i <= rows; i++) {
console.log(`${card[i].join('')}\n`);
}
caseNum++;
}
};
// Function call
punchedCards(inputFile);