+ 2

# Pyramid Triangle Logic

Hello I'm a beginner in JavaScript programming. I really like the logical aspect of programming so I like to come up with my own logics for my codes. I wrote this code to print a pyramid triangle after a lot of thinking with trial and error but I don't know if it is the correct way to do it. If you notice I use 3 arguments in the initialization of the first for loop and 2 final expressions. I need to know if this is a good or bad programming practice and if there is a better way to do it please share. This is the code: for(let a = 10, b = a, c = b; a >= 0; a--, b -= 2) { for(let d = 0; d <= c; d++) { if(b < 0) { break; } else if(d < b) { document.write("&nbsp"); } else if(d <= c) { document.write("*"); } } document.write("<br>"); }

22nd Sep 2020, 10:52 PM
Logos
2 Respuestas
+ 4
No it's not a bad practice. The bad practice I can see is the use of document.write(). Please avoid it. Rather use console.log or Element.innerText. There are easier ways to print this pattern but they are not necessarily better. The best option is the one that you can understand with your current skills.
22nd Sep 2020, 11:57 PM
Kevin ★
+ 3
I don't think what you've done is bad practice per se, but there is some redundancy there. You can do away with 'a' for a start. 'b' becomes negative before the first for loop finishes, and nothing is outputted after that happens. 'a' isn't used anywhere else either. The 'break' statement could be moved outside of the inner for loop too, and the second else if is also redundant - d <= c is the only criterion for that for loop continuing to iterate. Aside from all that, the logic is very good 😉
22nd Sep 2020, 11:33 PM
Russ
En tendencia hoy