以下面的格式在javascript中显示金字塔
我试图在控制台部分中仅使用javascript(document.write除外)显示金字塔 我不想要上面的格式。 我想要下面的格式以下面的格式在javascript中显示金字塔,javascript,Javascript,我试图在控制台部分中仅使用javascript(document.write除外)显示金字塔 我不想要上面的格式。 我想要下面的格式 * * * * * *
*
* *
* * *
* * * *
* * * * *
我的代码是
函数getPyramid(param){
对于(var i=1;i已解决):我得到了这个问题的逻辑
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<button onclick="getPyramid('10')">
Get Pyramid</button>
<script>
function getPyramid(param)
{
for(var i=0;i<param;i++) {
var output="";
for(var j=0;j<param-i;j++) {
output+=" ";
// console.log(" ");
}
for(var k=0;k<=i;k++) {
// console.log("* ");
output += "* ";
}
// output += "";
console.log(output);
}
}
</script>
</body>
</html>
页面标题
得到金字塔
函数getPyramid(param)
{
对于(变量i=0;i
varⅠ,j;
对于(i=1;i=1;i--)
{
对于(j=1;j=0;i--)
{
对于(j=0;j函数renderPyramid(n){
对于(变量i=0;i
function pyramid(n) {
// generate base of pyramid, aka longest possible string
let limit = n+n-1;
let hashesToPrint = 1; // number of hashes to print
for (let i=0; i<n; i++) {
// get length of spaces we need on each side
let difference = (limit - hashesToPrint) / 2;
// generate spaces string
let spaces = ' '.repeat(difference);
// create pounds string
let pounds = '#'.repeat(hashesToPrint);
// append spaces on either side of our pound string
let newString = spaces + pounds + spaces
console.log(newString)
// increment our counter by two
hashesToPrint += 2
}
}
pyramid(3)
函数金字塔(n){
//生成金字塔的底部,也称为最长的字符串
设极限=n+n-1;
让hashesToPrint=1;//要打印的哈希数
对于(让i=0;这很好。你的问题是什么?我想用javascript精确显示金字塔中心。这仍然不是问题。这是你的待办事项列表。@Marc我已经编辑了我的问题。你能检查一下这不是问题,那是要求我们为你做家庭作业。最好添加一些解释,或者添加适当的注释w我在密码里。
<script type="text/javascript">
var i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
document.write("\t*");
}
document.write("<br>");
}
for (i = 5; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
document.write("\t*");
}
//document.write("\t");
document.write("<br>");
}
for (i = 5; i >= 0; i--)
{
for (j = 0; j < i; j++)
{
document.write("*");
}
for (k = i; k <= 5; k++)
{
document.write("*");
}
document.write("*");
}
</script>
function pyramid(n) {
// generate base of pyramid, aka longest possible string
let limit = n+n-1;
let hashesToPrint = 1; // number of hashes to print
for (let i=0; i<n; i++) {
// get length of spaces we need on each side
let difference = (limit - hashesToPrint) / 2;
// generate spaces string
let spaces = ' '.repeat(difference);
// create pounds string
let pounds = '#'.repeat(hashesToPrint);
// append spaces on either side of our pound string
let newString = spaces + pounds + spaces
console.log(newString)
// increment our counter by two
hashesToPrint += 2
}
}
pyramid(3)