简体   繁体   English

用户定义的最后一个数字在JAVASCRIPT中的序号三角形

[英]sequential number triangle with user-defined last number in JAVASCRIPT

I am a complete novice at programming who is following a course in JavaScript as my first language. 我是编程的新手,他将JavaScript作为我的第一门语言。

Our current task is to use the any of the following: 我们当前的任务是使用以下任一方法:

  • for loop for循环
  • while loop while循环
  • Nesting 嵌套
  • document.write

to create the following result (without the additional enter): 创建以下结果(不带附加回车):

1
2 3
4 5 6
7 8 9 10
11 12 13 14

The last number in the sequence must be user-defined. 序列中的最后一个数字必须是用户定义的。

I am able to produce the following: 我能够产生以下结果:

1
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 

using edit: please note that the var "rows" is misleading (see bold text above): 使用edit:请注意,var“行”具有误导性(请参见上面的粗体):

var rows = parseInt(prompt("choose end number"))
var i 
var j 

for (i = 1; i <= rows; i++)
{
    for(j=1; j<=i; j++)
    {
        document.write(j + " "); 
    }
    document.write("<br>");
}

But I am not able to create something that breaks and then continues the sequence on the next line. 但是我无法创建中断的内容,然后继续下一行的序列。

If you have the time, could you also give me an explanation of the code you post in terms of how the computer processes it and why it works? 如果您有时间,还可以根据计算机的处理方式和工作原理,为我解释一下所发布的代码吗?

Not sure if i'd do it differently in practice, but this works: 不知道我在实践中是否会有所不同,但这可行:

var endNumber = parseInt(prompt("choose end number"))
var rowLength = 1; 
var rowCounter = 0;

for (var i = 1; i <= endNumber; i++) {
    document.write(i + " ");

    rowCounter++;

    if (rowCounter === rowLength) {
        rowLength++;
        rowCounter = 0;
        document.write("<br>");
    }
}

Explanation 说明

My thought process was essentially that there are two parts to the problem. 我的思考过程实质上是问题的两个部分。 (1) printing numbers from 1 to end value, and (2) inserting line breaks at certain points. (1)打印从1到最终值的数字,以及(2)在某些点插入换行符。

Printing the numbers is the easy part, you just need to do this: 打印数字很容易,您只需要执行以下操作:

var endNumber = parseInt(prompt("choose end number"));

for (var i = 1; i <= endNumber; i++) {
    document.write(i + " ");
}

The remaining part of the problem is to insert line break between numbers, initially with an interval of 1, and increasing by 1 each time. 问题的其余部分是在数字之间插入换行符,最初以1为间隔,每次增加1。 So we initialise two variables, one represents the current row length (starting at 1), and the other represents a counter within the current row (starting at 0). 因此,我们初始化了两个变量,一个代表当前行的长度(从1开始),另一个代表当前行内的计数器(从0开始)。

var rowLength = 1; 
var rowCounter = 0;

Then inside the forloop, we increment the counter by 1 each time, and check whether it matches the row length. 然后在forloop中,我们每次将计数器加1,然后检查它是否与行长匹配。 If it matches, we reset the counter, increase the line length by 1 and reset the counter. 如果匹配,我们将重置计数器,将行长增加1并重置计数器。

    rowCounter++;

    if (rowCounter === rowLength) {
        rowLength++;
        rowCounter = 0;
        document.write("<br>");
    }

One way to do this would be: 一种方法是:

var rows = parseInt(prompt("choose end number"))
var i 
var j 
var count = 1

for (i = 1; i <= rows; i++)
{
             for(j=1; j<=i; j++)
             {
             document.write(count + " ");
             count  = count +1
             }
document.write("<br>");
}

I have simply added a count variable and incrementing it every time you write something. 我只是添加了一个count变量,并在每次写东西时将其递增。 The above solution still doesnot calculate i from end number. 上面的解决方案仍然无法根据结束号计算i。 See the next solution for that. 请参阅下一个解决方案。

Another way: Can we do it with only i and j? 另一种方式:我们可以只用i和j来做吗?

var N = parseInt(prompt("choose end number")) 
rows=1
while(rows*(rows+1)/2 < N){
    rows = rows+1
}
var i
var j 
for (i = 1; i <= rows; i++)
{
             for(j=1; j<=i; j++)
             {
             if (i*(i-1)/2 + j  <= N){
                 document.write(i*(i-1)/2 + j + " ");
             }
             }
document.write("<br>");
}

Till row i-1 we would have printed sum of first i-1 natural numbers. 直到第i-1行,我们都将打印第一个i-1自然数的和。 and so instead of printing j you could add sum of first i-1 natural numbers to j 因此,除了打印j之外,您还可以将第一个i-1自然数的和加到j

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM