如何获得数字金字塔的正确Javascript输出
我目前正在研究一个1/2的数字金字塔。我可以把输出加起来,得到除了*号以外的所有数字。希望有人能伸出援助之手。这是我已经完成的代码如何获得数字金字塔的正确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 = "";
<!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