Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得数字金字塔的正确Javascript输出_Javascript_Loops_For Loop - Fatal编程技术网

如何获得数字金字塔的正确Javascript输出

如何获得数字金字塔的正确Javascript输出,javascript,loops,for-loop,Javascript,Loops,For Loop,我目前正在研究一个1/2的数字金字塔。我可以把输出加起来,得到除了*号以外的所有数字。希望有人能伸出援助之手。这是我已经完成的代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Assignment 1</title> <script> var num = "";

我目前正在研究一个1/2的数字金字塔。我可以把输出加起来,得到除了*号以外的所有数字。希望有人能伸出援助之手。这是我已经完成的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">  
    <title>Assignment 1</title>
    <script>
        var num = "";
        var match ="";


var size = prompt("Enter the size of the pyramid");
if (size >=1) {
var total="1";

for(var i=1; i<=size; i++)
{
    if (i < size){
  num = num  +  i + " " 

  } if (i==size) {
  num =num + i }


   total= total * i;
  document.write(num   + " = "+ total + "<br>"); 
}
 }else {
 alert("Please enter a number equal to or greater than 1");
 }
var total="1";
    </script>
</head>
<body>
    <main>
        <!-- Will show after the script has run -->
        <h1>Assignment 1</h1>
    </main>
</body>
</html>
等等。
再次感谢

您可以使用这样的循环,并将总时间作为新的迭代值:

var总计=1; var newList=[];
forlet i=1;iprompt返回字符串或null。你应该先把结果转换成一个数字。那么你得到的结果有什么问题呢?为什么有var total=1;在循环之后?我看不到您在构建字符串时添加*。请使用正确且一致的缩进。这将使你和我们的生活更轻松;
1=1
1*2=2
1*2*3=6
1*2*3*4=24
1*2*3*4*5=120