为什么双镜帕斯卡星图案没有打印出来?

huangapple go评论68阅读模式
英文:

Why the Double mirror pascal star pattern is not printed?

问题

以下是您的JavaScript代码,用于打印下面图像中显示的模式。

var n = prompt("输入要打印的n的数量");
let str = "";
var i, j, k;

for(i = 1; i <= n; i++){
    for(j = 1; j <= i; j++){
        str += "*";
    }
    for(k = n + 1; k >= i; k--){
        str += " ";
    }
    for(k = n + 1; k >= i; k--){
        str += " ";
    }
    for(j = 1; j <= i; j++){
        str += "*";
    }
    str += "\n";
}

for(i = 1; i <= n + 2; i++){
    for(j = n + 2; j > i; j--){
        str += "*";
    }
    for(k = 1; k <= i; k++){
        str += " ";
    }
    for(k = 1; k <= i; k++){
        str += " ";
    }
    for(j = n + 2; j > i; j--){
        str += "*";
    } 
    str += "\n";
}
console.log(str);

代码中的 prompt 函数用于接受用户输入的数字 n。如果您的输出与图像不匹配,您可以检查代码是否正确,并查看是否有任何错误。

英文:

Here is the my javascript code for printing the pattern shown below in the image.
please check the code and solve the error.

&lt;script&gt;
var n = prompt(&quot;Enter the number of n you want to print&quot;);
        //rows = Math.floor(n / 2)
        
        let str = &quot;&quot;
        var i, j, k
        for(i = 1; i &lt;= n; i++){
            for(j = 1; j &lt;= i; j++){
                str += &quot;*&quot;
            }
            for(k = n + 1; k &gt;= i; k--){
                str += &quot; &quot;
            }
            for(k = n + 1; k &gt;= i; k--){
                str += &quot; &quot;
            }
            for(j = 1; j &lt;= i; j++){
                str += &quot;*&quot;
            }       
            str += &quot;\n&quot;
        }       
        
        for(i = 1; i &lt;=n + 2; i++){
            for(j = n + 2; j &gt; i; j--){
                str += &quot;*&quot;
            }
            for(k = 1; k &lt;= i; k++){
                str = &quot; &quot;
            }
            for(k = 1; k &lt;= i; k++){
                str = &quot; &quot;
            }
            for(j = n + 2; j &gt; i; j--){
                str += &quot;*&quot;
            } 
            str += &quot;\n&quot;
        }
        console.log(str)
&lt;/script&gt;

I want Output like this :
为什么双镜帕斯卡星图案没有打印出来?

but I got just 2 spaces in output

答案1

得分: 1

你在第二个“i for循环”中都将str设置为&quot; &quot;

例如:

for(k = 1; k &lt;= i; k++){
    str = &quot; &quot;
}
for(k = 1; k &lt;= i; k++){
    str = &quot; &quot;
}

如果你将它们更新为+=,它就能正常工作。

英文:

You are setting str equal to &quot; &quot; in both "k loops" in the second "i for loop".

e.g:

for(k = 1; k &lt;= i; k++){
    str = &quot; &quot;
}
for(k = 1; k &lt;= i; k++){
     str = &quot; &quot;
 }

If you update those to += it works.

答案2

得分: 1

将您的输入转换为 Number

n = Number(n);

然后按照 @TomLV 的建议修改您的代码:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
var n = prompt("Enter the number of n you want to print");
n = Number(n);
let str = "";
var i, j, k;
for (i = 1; i <= n; i++) {
    for (j = 1; j <= i; j++) {
        str += "*";
    }
    for (k = n + 1; k >= i; k--) {
        str += " ";
    }
    for (k = n + 1; k >= i; k--) {
        str += " ";
    }
    for (j = 1; j <= i; j++) {
        str += "*";
    }
    str += "\n";
}

for (i = 1; i <= n + 2; i++) {
    for (j = n + 2; j > i; j--) {
        str += "*";
    }
    for (k = 1; k <= i; k++) {
        str += " ";
    }
    for (k = 1; k <= i; k++) {
        str += " ";
    }
    for (j = n + 2; j > i; j--) {
        str += "*";
    }
    str += "\n";
}
console.log(str);

<!-- end snippet -->
英文:

Convert your input to Number:

n = Number(n);

and then change your code as @TomLV mentioned:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var n = prompt(&quot;Enter the number of n you want to print&quot;);
//rows = Math.floor(n / 2)
n = Number(n);
let str = &quot;&quot;
var i, j, k
for(i = 1; i &lt;= n; i++){
for(j = 1; j &lt;= i; j++){
str += &quot;*&quot;
}
for(k = n + 1; k &gt;= i; k--){
str += &quot; &quot;
}
for(k = n + 1; k &gt;= i; k--){
str += &quot; &quot;
}
for(j = 1; j &lt;= i; j++){
str += &quot;*&quot;
}       
str += &quot;\n&quot;
}       
for(i = 1; i &lt;=n + 2; i++){
for(j = n + 2; j &gt; i; j--){
str += &quot;*&quot;
}
for(k = 1; k &lt;= i; k++){
str += &quot; &quot;
}
for(k = 1; k &lt;= i; k++){
str += &quot; &quot;
}
for(j = n + 2; j &gt; i; j--){
str += &quot;*&quot;
} 
str += &quot;\n&quot;
}
console.log(str)

<!-- end snippet -->

答案3

得分: 1

以下是修正后的代码:

// 将字符串转换为数字:
const n = +prompt("输入要打印的n的数量");

let str = "";
for (let i = 1; i <= n; i++) {
    for (let j = 1; j <= i; j++) {
        str += "*";
    }
    // 减少了这里的迭代次数:
    for (let k = n; k >= i; k--) {
        str += " ";
    }
    // 为中心列添加一个空格,该列是唯一没有星号的列
    str += " ";
    // 减少了这里的迭代次数:
    for (let k = n; k >= i; k--) {
        str += " ";
    }
    for (let j = 1; j <= i; j++) {
        str += "*";
    }       
    str += "\n";
}

// 减少了迭代次数。i 不应该等于 n + 2
for (let i = 1; i <= n + 1; i++) {
    for (let j = n + 2; j > i; j--) {
        str += "*";
    }
    // 减少了这里的迭代次数:
    for (let k = 1; k < i; k++) {
        str += " "; // 修复了赋值
    }
    // 为中心列添加一个空格,该列是唯一没有星号的列
    str += " ";
    // 减少了这里的迭代次数:
    for (let k = 1; k < i; k++) {
        str += " "; // 修复了赋值
    }
    for (let j = n + 2; j > i; j--) {
        str += "*";
    } 
    str += "\n";
}
console.log(str);

希望对你有所帮助!

英文:

Some issues:

  • prompt returns a string, you need to convert it to a number. You can use the unary plus for that.
  • str = &quot; &quot; occurs at two places where you should have done str += &quot; &quot;
  • The generated pattern has two spaces in the center line, while you are asked to only have one space there. To make that happen have the k loops make one iteration less, and add str += &quot; &quot; as a separate statement outside of those loops.
  • The output has an empty line at the very end. This is because the second i loop is making one iteration too many.

Not a problem, but:

  • Use semi-colons to separate statements. Although JavaScript provides automatic semi-colon insertion, you wouldn't be the first to fall into one of the pitfalls. It is better to take control of this yourself and have the habit of adding the semi-colons.
  • There really is no need here to declare loop variables at the top. Just declare them at the moment you need them with only the scope they need to have.

I'll assume that the number of lines in the output is supposed to be n*2+1 and that there was no error concerning that aspect.

Corrected code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Convert string to number using unary plus:
const n = +prompt(&quot;Enter the number of n you want to print&quot;);
let str = &quot;&quot;;
for (let i = 1; i &lt;= n; i++) {
for (let j = 1; j &lt;= i; j++) {
str += &quot;*&quot;;
}
// Reduced the number of iterations here:
for (let k = n; k &gt;= i; k--) {
str += &quot; &quot;;
}
// Add one space for the center column 
//    that is the only column without asterisks
str += &quot; &quot;;
// Reduced the number of iterations here:
for (let k = n; k &gt;= i; k--) {
str += &quot; &quot;;
}
for (let j = 1; j &lt;= i; j++) {
str += &quot;*&quot;;
}       
str += &quot;\n&quot;;
}
// Reduced number of iterations. i should not become equal to n + 2
for (let i = 1; i &lt;= n + 1; i++) {
for (let j = n + 2; j &gt; i; j--) {
str += &quot;*&quot;;
}
// Reduced the number of iterations here:
for (let k = 1; k &lt; i; k++) {
str += &quot; &quot;; // Fixed assignment
}
// Add one space for the center column 
//    that is the only column without asterisks
str += &quot; &quot;;
// Reduced the number of iterations here:
for (let k = 1; k &lt; i; k++) {
str += &quot; &quot;; // Fixed assignment
}
for (let j = n + 2; j &gt; i; j--) {
str += &quot;*&quot;;
} 
str += &quot;\n&quot;;
}
console.log(str);

<!-- end snippet -->

Note that JavaScript has functions that can facilitate this proces, like &quot;*&quot;.repeat(i) can be used instead of a loop to produce the same string.

So then it becomes:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const n = +prompt(&quot;Enter the number of n you want to print&quot;);
let str = &quot;&quot;;
for (let i = 1; i &lt;= n + 1; i++) {
str += &quot;*&quot;.repeat(i) + &quot; &quot;.repeat(2*n + 3 - 2*i) + &quot;*&quot;.repeat(i) + &quot;\n&quot;;
}       
for (let i = n; i &gt;= 1; i--) {
str += &quot;*&quot;.repeat(i) + &quot; &quot;.repeat(2*n + 3 - 2*i) + &quot;*&quot;.repeat(i) + &quot;\n&quot;;
}
console.log(str);

<!-- end snippet -->

And you could also reuse the results of the first loop to derive the second half of the output by storing the lines in an array. You can then reverse that array to get the second half (without the middle line):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const n = +prompt(&quot;Enter the number of n you want to print&quot;);
const arr = Array.from({length: n + 1}, (_, i) =&gt; 
&quot;*&quot;.repeat(i+1) + &quot; &quot;.repeat(2*(n-i)+1) + &quot;*&quot;.repeat(i+1)
);
console.log([...arr, ...arr.reverse().slice(1)].join(&quot;\n&quot;));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 14:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定