代码:
<html><head><title>function</title></head><body><script>function print_trangle(n){for (var i=0;i<n; i++){var count =0;document.write("<br/>");for(var j=2*i+1;;){count++;document.write("*");if (count==2*i+1){break;}}}}function print_trangle2(n){var last = 2*(n-1)+1;for (var i=0;i<n; i++){var full_left = parseInt(last/2)-i;document.write("<br/>");for(var j=0;j<last;j++){if (j<=full_left||j>full_left+2*i+1){document.writeln(" ");}else{document.writeln("*");}}//此处我也不知道什么原因会每次都在最后一行少打印最后一个*,如果有人知道麻烦回复告诉我什么问题,我就暂时先用这种强制方法解决了if (i==n-1){document.write("*");}}}print_trangle2(4);</script></body></html>
js实现打印正三角
原文地址:https://www.cnblogs.com/g177w/p/8365544.html