Problem 38. diamond.js
/*Question
Print diamond shape with n=5
*/
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 n = parseInt(readLine()); // 5
for (let i = 1; i <= n; i++) // Responsible for the 5 rows
{
for (let j = 1; j <= n - i; j++) // Responsible for printing spaces
{
process.stdout.write(" ");
}
for (let k = 1; k <= 2 * i - 1; k++)// Responsible for printing stars *
{
process.stdout.write('*');
}
console.log();
}
for (let i = n - 1; i >= 1; i--) // here we have taken n-1 because we only need 4 rows
{
for (let j = 1; j <= n - i; j++) // Responsible for printing spaces
{
process.stdout.write(" ");
}
for (let k = 1; k <= 2 * i - 1; k++)// Responsible for printing stars *
{
process.stdout.write('*');
}
console.log();
}
TERMINAL:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node diamond.js <input.txt
*
***
*****
*******
*********
*******
*****
***
*
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$
Comments
Post a Comment