I have a simple question although i cannot manage to resolve this problem. Hope you can help. I need to make triangle using for loop and from this 4 exercises I don’t know what to do with the third one. I haven’t used Javascript before, so any help would be appreciated.
JavaScript
x
6
1
# # # # #
2
# # # #
3
# # # <----- here is triangle i need to make. Just in case
4
# #
5
#
6
JavaScript
1
15
15
1
var i;
2
var j;
3
for (i = 0; i <= 5; i++ )
4
{
5
document.write("</br>");
6
for ( j = 0; j < 6-i; j++ )
7
{
8
document.write( "  " );
9
}
10
for ( j = 6-i; j <= 5; j++ )
11
{
12
13
document.write( "*" );
14
}
15
}
This is code I wrote for D in photo. And I’m sorry i did not add it at first.
Advertisement
Answer
I’m sure there are better solutions (simply left-padding with spaces comes to mind), but here’s the quick and dirty one I created from your own solution.
JavaScript
1
11
11
1
for (var i = 0; i < 5; i++) {
2
for (var j = 0; j < i; j++) {
3
document.write(" ");
4
}
5
for (var j = 5; j > i; j--) {
6
document.write("#");
7
if (j > i + 1) document.write(" ");
8
}
9
document.write('<br/>')
10
}
11