0
var calc = function(){
var nums = [];
for (i = 0; i < 25; i++){
    document.getElementById(i).value = nums[i%5][(i/5)-((i%5)/5)];
}

I can't figure out why the nums[i%5][(i/5)-((i%5)/5)] part evaluates to unidentified. There are 25 text boxes on the page in a 5x5 square formation. I am trying to get all elements into a 2-d array with the corresponding coordinates. Please help.

3 Answers3

2

If you want to fill the array, you must do the opposite assignment:

function calc(){
    var nums = [];
    for (i = 0; i < 5; i++) {
        nums[i] = [];
        for (j = 0; j < 5; j++)
            nums[i][j] = document.getElementById(5*j + i).value;
    }
}
Oriol
  • 225,583
  • 46
  • 371
  • 457
0

Firstly, I believe you've written your LHS and RHS of = the wrong way around.

side_to_set = side_to_get;

Next, you can't set a property of undefined, so say you have arr = [], you can't set the property arr[0].bar because arr[0] is undefined. You first have to set arr[0].

What does this mean for you? Well, you'll have to create new a new Array for each row of your 2D structure;

var calc = function(){
        var nums = [], i, j, k; // remember to `var`
        for (i = 0; i < 25; i++) { // keeping with your single for loop
            j = i % 5; // code clarity saves lives
            k = (i - j) / 5;
            if (!nums[j])     // if no array at this index
                nums[j] = []; // create one
            nums[j][k] = document.getElementById(i).value;
        }
        return nums; // let you access `nums` outside of `calc`
    };

This code accesses your elements in order of their id using one for, but you can simplify the calculations if you don't mind a nested for/accessing them in a differed order, as described in Oriol's answer

Community
  • 1
  • 1
Paul S.
  • 58,277
  • 8
  • 106
  • 120
0

It's hard to figure out from your example exactly what you're trying to do. If you're trying to create a 2d array containing the values of i%5 and (i%5)/5 and output those values into text boxes, this would do it:

var nums = [];
var calc = function(){
    for (i = 0; i < 25; i++){
        nums.push([i%5, (i/5)-((i%5)/5)]);
        document.getElementById('txtbox' + (i+1)).value = nums[i][0] + ', ' + nums[i][1];
    }
};
calc();

Also, you're using document.getElementById(i).value and i is a numeric variable, which means the id attribute of your elements would have to be a number. That's not valid. You need a letter as the first character, like t1 or txtbox1.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

See the fiddle: http://jsfiddle.net/s2Sqk/

EDIT: A single numeric value for the ID attribute is technically valid in HTML5, but not in HTML4. In either case, it's best practice to use an a-z character first.

Gavin
  • 5,796
  • 2
  • 44
  • 67
  • Using a value beginning with a non _a-z_ character for an _id_ isn't invalid in _HTML5_ [**spec**](http://www.w3.org/html/wg/drafts/html/master/dom.html#the-id-attribute) (can't remember earlier specs), but is [**bad pratice**](http://stackoverflow.com/a/79022/1615483), may have unexpected results in some browsers and means you can't use selector shorthand for them. – Paul S. Apr 06 '14 at 22:52
  • You're right, it's the [HTML4 spec](http://www.w3.org/TR/html4/types.html#h-6.2) that it's invalid in. – Gavin Apr 07 '14 at 07:03