How to print this pattern In JavaScript without using document.write() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to print this pattern In JavaScript without using document.write()

* ** *** **** *****

12th Mar 2020, 1:30 PM
Bilal Yoosuf
Bilal Yoosuf - avatar
9 Answers
+ 9
Alright, then I'll give my solution too: for(i of [1, 2, 3, 4, 5]) console.log('*'.repeat(i)); (We should prefer to just give hints so that the user learns something by trying themselves!)
12th Mar 2020, 2:00 PM
HonFu
HonFu - avatar
+ 5
Bilal Yoosuf We can do using console.log Check this for (var i = 0; i < 5; i++) { var a = ""; for (var j = 0; j <= i; j++) { a = a + "*"; } console.log(a); } HonFu is right.
12th Mar 2020, 1:58 PM
A͢J
A͢J - avatar
+ 4
Use console.log instead.
12th Mar 2020, 1:31 PM
HonFu
HonFu - avatar
+ 3
Not (hint) if you create each line before you log it. 😉
12th Mar 2020, 1:37 PM
HonFu
HonFu - avatar
+ 3
Create <pre> </pre> area, and change innerHTML.
12th Mar 2020, 1:51 PM
Igor Kostrikin
Igor Kostrikin - avatar
+ 3
Thanks everyone for the answers. I understood the mistake I made while using console.log().
12th Mar 2020, 3:59 PM
Bilal Yoosuf
Bilal Yoosuf - avatar
+ 2
HonFu If we use console.log we won't get the pattern like this. Console.log automatically choose the next line always.
12th Mar 2020, 1:33 PM
Bilal Yoosuf
Bilal Yoosuf - avatar
12th Mar 2020, 2:38 PM
Igor Kostrikin
Igor Kostrikin - avatar
13th Mar 2020, 9:20 PM
\•/
\•/ - avatar