如何为形成框的星号创建程序
How to create program for asterisks forming a box?
*******
* *
* * *
* *
*******
它应该看起来像上面的星号排列。我是编程新手。到目前为止,我已经写过:
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<body>
<script>
var maxCount = 7;
var iterationCount = 0;
while (iterationCount < maxCount) {
iterationCount = iterationCount + 1;
document.write('*');
}
到目前为止,我有一种强烈的感觉,这是不正确的,即使它是正确的,我也不知道如何从那里继续下去。我知道我必须使用 for 和嵌套循环,但我非常困惑。
尝试
var width = 7,
height = 7, // for example
boxElem = document.getElementById("box"); // this is the element which will hold your box
for(var i = 0; i < height; i++) {
for(var j = 0; j < width; j++) {
if(i === 0 || i === height - 1 || j === 0 || j === width - 1 || (i === Math.floor(height / 2) && j === Math.floor(width / 2))) {
// check if it's the first or last row or column, or if it's the centre of the box
boxElem.innerHTML += "*";
} else {
boxElem.innerHTML += " "; // otherwise, don't add an asterisk but an empty space
}
}
boxElem.innerHTML += "'n"; // new line
}
相关文章:
- 如何创建带有插槽的vue js组件预加载程序
- 我想在AngularJS应用程序中创建一个输入数字框,用户不应该在该框上键入十进制数字.(一个整数输入框)
- 有没有一种方法可以让内联事件处理程序在元素创建后立即执行
- Javascript库创建类似heroku仪表板的应用程序
- 我需要学习Java才能使用phoneGap创建android应用程序吗
- 要求创建空模块作为依赖项加载程序
- 创建nw.js+流星桌面应用程序
- Chrome应用程序在全屏中创建新窗口-html元素不是全高的
- 如何创建可从多个应用程序使用的PHP登录web服务
- Ember.js+传单+rails(使用Javascript MVC创建rails应用程序并打开源代码映射)
- RequireJS正在加载angular应用程序,但没有't创建app.controller
- 在动态创建的元素中包含参数的事件处理程序
- 在 Chrome 扩展程序中创建自定义事件的最惯用方式
- 如何使用引导程序创建自定义滚动条
- 如何使用引导程序创建模式窗口
- 在Chrome扩展程序图标中创建一个切换开关,以打开/关闭JavaScript
- 我想在混合移动应用程序中使用Sqlite插件(Cordova)创建一个示例项目
- 创建程序以显示当前日期和时间
- 如何为形成框的星号创建程序
- 在用javascript和html创建程序时遇到问题