-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPattern-22 The-Number-Pattern.js
37 lines (34 loc) · 1.08 KB
/
Pattern-22 The-Number-Pattern.js
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
function theNumberPattern(num) {
// Outer loop for no. of rows
for (let i = 0; i < 2 * num - 1; i++) {
// inner loop for no. of columns.
for (let j = 0; j < 2 * num - 1; j++) {
// Initialising the top, down, left and right indices of a cell.
let top = i;
let bottom = j;
let right = 2 * num - 2 - j;
let left = 2 * num - 2 - i;
// Min of 4 directions and then we subtract from num
// because previously we would get a pattern whose border
// has 0's, but we want with border N's and then decrease inside.
process.stdout.write(
num - Math.min(Math.min(top, bottom), Math.min(left, right)) + " "
);
}
// As soon as the numbers for each iteration are printed, we move to the
// next row and give a line break otherwise all numbers
// would get printed in 1 line.
console.log();
}
}
theNumberPattern(5);
// OUTPUT
// 5 5 5 5 5 5 5 5 5
// 5 4 4 4 4 4 4 4 5
// 5 4 3 3 3 3 3 4 5
// 5 4 3 2 2 2 3 4 5
// 5 4 3 2 1 2 3 4 5
// 5 4 3 2 2 2 3 4 5
// 5 4 3 3 3 3 3 4 5
// 5 4 4 4 4 4 4 4 5
// 5 5 5 5 5 5 5 5 5