Created
October 31, 2013 12:25
-
-
Save asika32764/7248825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
// 三角形的高 | |
var height = 4; | |
// 每一排加幾個星號 | |
var add = 2; | |
// 起始的星號數量 | |
var start = 1; | |
// 最大的那一行會有幾顆星 | |
var max = height * add + start; | |
for(i = 1; i <= height; i++){ | |
// 每一行有幾顆星 | |
// 高減一等於零,再乘上 add (每排增加的數量) | |
var number = start + (add * (i - 1)); | |
// 每一行有幾個空白 | |
var spaces = (max - number) / 2; | |
var line = ''; | |
// + 空白 | |
for(k=1; k <= spaces; k++){ | |
line = line + ' '; | |
} | |
// + 星號 | |
for(k=1; k <= number; k++){ | |
line = line + '*'; | |
} | |
// + 空白 | |
for(k=1; k <= spaces; k++){ | |
line = line + ' '; | |
} | |
console.log(line); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment