I am learning loop in Javascript.
I need output like this:
*
**
***
****
*****
But, When I use this code :
for (i = 1; i <= 5; i = i + 1) {
for (j = 0; j < i; j = j + 1) {
console.log('*');
}
console.log("");
}
I got an output like this :
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
How do I solve this?
In JS, every console.log() is printed on a new line. So you will have to keep a string which you keep adding to. And then print it:
for (i = 1; i <= 5; i = i + 1) {
let str = "";
for (j = 0; j < i; j = j + 1) {
str += "*";
}
console.log(str);
}
In JavaScript, console.log() prints output on a new line, so you need to concatenate all the stars in a string and print it.
const printPattern = (count, symbol) => {
for (let i = 1; i <= count; i++) {
console.log(symbol.repeat(i));
}
};
printPattern(5, '*');