Problem 29. pattern.js (***Revise***)
/*Question
Given a positive integer n, generate the following format.
suppose n is 3, then the format is
1
1
2
1
2
3
Input: One line containing a positive integer, denoting n .
Output: Above described format.
Example:
Input:
5
output:
1
1
2
1
2
3
1
2
3
4
1
2
3
4
5
Algorithm:
1) Read the input number
2) Print the pattern
*/
let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');
function readLine() {
idx++;
return data[idx - 1].trim();
}
//.......................................................................................
let num = parseInt(readLine()); // 3
for (let i = 1; i <= num; i++) // 1) i=1
{
for (let j = 1; j <= i; j++) //1) j=1 , 2<=1 false , so come out of the inner forloop
print the space and do i++ = 2
{
console.log(j); // 1) 1 now j++ is 2
}
console.log(); // this is to print space
}
TERMINAL:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node pattern.js <input.txt
1
1
2
1
2
3
*** Without the empty console.log() , the output would look like below:-
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node pattern.js <input.txt
1
1
2
1
2
3
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$
Comments
Post a Comment