I have tried printing 3*5 box * (star) pattern program, but not able to get it to format properly. Can any one guide me mistakes if and help to rectify?
Below is the javascript code:
// Getting input via STDIN
const readline = require("readline");
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
//start-here
var i,j,a=3,b=5;
for(i=1;i<=a;i++)
{
for(j=1;j<=b;j++)
{
console.log("*");
}
console.log("\n");
}
//end-here
});
output:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
need to get like this output:
*****
*****
*****
console.log automatically logs to a new line. You should instead append to a variable and log it outside of the loop:
var i, j, a = 3,
b = 5;
for (i = 1; i <= a; i++) {
var str = "";
for (j = 1; j <= b; j++) {
str += "*";
}
console.log(str + "\n");
}
Or even better, don't use a loop at all and use String.repeat:
var i, j, a = 3,
b = 5;
for (i = 1; i <= a; i++) {
console.log("*".repeat(b) + "\n");
}