print是打印在网页上,代码里只写了控制
<script>
function print(str) {
document.write("<span style='white-space:pre;'>")
document.write(str)
document.write("</span>")
}
function half(a) {
// console.log('ok', parseInt(a.toFixed(1).slice(0,-2)) );
return parseInt(a.toFixed(1).slice(0,-2))
}
a(9,9)
function same(a,b) {
if(a%2==0 && b%2==0) { return true }
else if(a%2 !=0 && b%2 != 0) { return true }
else { false }
}
function a(x,y) {
let str = ""
for(let i=0;i<y;i++) {
if(i<=half(y/2)) {
for(let j=0;j<x;j++) {
if(j<=half(y/2)+i && j>=half(y/2)-i && same(i,j)) {
str += "*"
} else {
str+=" "
// str+="/"
// str+=" "
}
}
} else {
for(let j=0;j<x;j++) {
if(j>=i-half(y/2) && j<y-i+half(y/2) && same(i,j)) {
str += "*"
} else {
str+=" "
// str+="/"
// str+=" "
}
}
}
str+="\n"
}
console.log(str);
// print(str)
}
</script>
最终效果
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
```