简体   繁体   English

无法获取我的输入以显示javascript中的行和列

[英]Can't get my inputs to show rows and columns in javascript

So I'm supposed to create a nested 'for loop' that iterates through x (columns) and y(rows) up to the user-entered size to form a pattern of some sort and generate an interesting pattern (using the x and y values to make decisions) that places the three user-provided characters in a 2-dimensional grid." 因此,我应该创建一个嵌套的“ for循环”,该循环遍历x(列)和y(行)直至达到用户输入的大小,以形成某种模式并生成有趣的模式(使用x和y值进行决策),将三个用户提供的字符放置在二维网格中。”

I'm mostly having a problem with the rows and columns. 我主要遇到行和列的问题。 I keep getting one columns and too many rows. 我不断得到一列和太多行。 After it posts the three characters entered it starts to print undefined and NaN. 输入三个字符后,它将开始打印undefined和NaN。 I'm not sure why but here is the part of the code with other commented out code I was trying to get to work as well. 我不知道为什么,但是这是代码的一部分,以及我试图开始工作的其他注释掉的代码。

<html>
    <head>
        <title> Lab </title>
    </head>
    <style>
    </style>
    <body>
        <h1> </h1>
        <p id= 'myParagraph'> My Paragraph! </p>
    </body>
    <script>

    //prompt user to enter 3 characters
    var charOne = prompt('Enter a character.');
    var charTwo = prompt('Enter another character.');
    var charThree = prompt('Enter one last character.');
    console.log(charThree);

    //prompt user for pattern size
    var size = prompt('I will generate a pattern for you. Give me a size.');

    //check
    console.log('Still going.');

    //loops until number is givin
    while (isNaN(size) === true){
        if(isNaN(size) === true){
        alert('Not a number!');
        size = prompt('Give me a number.'); 
         } else {
       } 
    } 
    console.log('Outta the loop');

    //nested for loop for pattern
   /* for(var x = 0; x < size; x++) {
       for(var y = 1; y < size; y++){
       document.write(charOne,charTwo,charThree + '<br>');
       }
    }
    */
   // console.log('Outta nested loop');

    /*
    var cols = [];
    var rows = size;
    for (var i = 0; i < rows; i++){
         cols[i] = [];
   }*/

    /*
    var iMax = size;
    var jMax = size;
    var f = [charOne,charTwo,charThree];

    for(i = 0; i < iMax; i++) {
        f[i] = [charOne,charTwo,charThree];
        for(j = 0; j < jMax; j++) {
            f[i][j] = 0;
            document.write(charOne,charTwo,charThree + '<br>');
        }
        }
     */

    //an arry to make a pattern
    chars = [charOne, charTwo, charThree];

    for(var x = 0; x < size; x++){
        for(var y = 1; y < size; y++){
            document.write(chars[x] + chars[y] + '<br>');
        }

    }


    </script>

</html>

I don't really understand what you trying to achieve here, (you should give us an example of the result you hope to get), but I'm already seeing on problem in your loop: 我真的不明白您要在这里实现什么(您应该给我们举一个希望得到的结果的例子),但是我已经在循环中发现了问题:

chars = [charOne, charTwo, charThree];

for(var x = 0; x < size; x++){
    for(var y = 1; y < size; y++){
        // Here you take the xth and yth indexes
        // of the chars Array which only has 3 entries
        // document.write(chars[x] + chars[y] + '<br>');
        // You could try this:
        document.write(chars[0] + chars[1] + '<br>');
    }
}

Lets say I've entered ABC, therefore chars = ['A', 'B', 'C'] , assuming the size I've entered is 5, then the access to chars will go as follow: 假设我输入了ABC,因此chars = ['A', 'B', 'C'] ,假设我输入的size为5,那么对chars的访问将如下所示:

chars[0] = 'A'
chars[1] = 'B'
chars[2] = 'C'
chars[3] = undefined
chars[4] = undefined

As you can see, you have an index out of bounds error here. 如您所见,这里有一个索引超出范围错误。

If I understand you correctly, you want the print the letters on each cell in the grid? 如果我理解正确,您是否希望在网格中的每个单元格上打印字母? If not then please let me know, if it is then this should solve it: 如果不是,请让我知道,如果是,这应该解决:

for(var x = 0; x < size; x++) {
    for(var y = 1; y < size; y++){
        document.write(charOne + charTwo + charThree + '<br>');
    }
}

The problem is that your loop could be iterating through array elements that are undefined. 问题在于您的循环可能会遍历未定义的数组元素。 For example if numbers a, b, c are entered and someone enters 5 as the size then you could be trying to print values for an array that has only a size of 3 [0, 1, 2] but going out to and index of [0, 1, 2, 3, 4]. 例如,如果输入数字a,b,c,并且有人输入5作为大小,那么您可能要尝试打印大小仅为3 [0,1,2]但要索引到的数组的值[0,1,2,3,4]。 Char[3] and Char[4] don't exist. Char [3]和Char [4]不存在。 I suggest that size be the outer for loop and you use the size of the Char[] array (3) to limite the two inner loops. 我建议将大小作为外部for循环,并使用Char []数组(3)的大小来限制两个内部循环。 In your case set inner loops to go through char.length to avoid undefined. 在您的情况下,请将内部循环设置为通过char.length以避免未定义。

chars = [charOne, charTwo, charThree];
    for (var z = 0; z < size; z++) {
        for (var x = 0; x < chars.length; x++) {
            for (var y = 1; y < chars.length; y++) {
                document.write(chars[x] + chars[y] + '<br>');
            }
        }
    }

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

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