www.webdeveloper.com
Results 1 to 6 of 6

Thread: Problem with array iteration

  1. #1
    Join Date
    Oct 2013
    Posts
    7

    Problem with array iteration

    Hi,
    can anyone tell me what is wrong in the code bellow?
    what i want is to push a new index in var array with the values from var sample but only if they don't already exist in var array!
    Ty!


    <button id="button">Add to array</button>

    var sample = [1, 1];
    var array = [
    [0, 1],
    [1, 0],
    [1, 1],
    [0, 2] ];

    $('#button').click(function () {
    for (i in array) {
    if (sample[0] !== array[i][0] && sample[1] !== array[i][1]) {
    array.push([sample[0], sample[1]]);
    }
    }
    console.log(array);

    });

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    I would think that you should set a flag so that the sample only gets pushed once:
    Code:
    var found=false;
    for (i in array) {
    if (sample[0] !== array[i][0] && sample[1] !== array[i][1] && !found) {
    console.log("pushing")
    array.push([sample[0], sample[1]]);
    found=true;
    }
    }
    as a side note, the for in loop is generally used for objects, and can't really be trusted for arrays. General consensus is that you should use a standard for loop to iterate an array:
    Code:
    for (var i = 0; i < array.length; i++) {
    // do stuff
    		}
    or else one of the newer array methods

  3. #3
    Join Date
    Oct 2013
    Posts
    7
    Ty for the answer!
    Thats exctly what i came up with while i was waiting for help , my brain is very slow today!

    function addUsedCell(a1, a2){

    var c = a1;
    var d = a2;
    console.log(c);

    for (i = 0; i <d.length; i++) {
    if(c[0] == d[i][0] && c[1] == d[i][1]){
    status = true;
    //usedCells.push( [ c[0], c[1] ] );
    }else{
    status = false;
    }
    }
    console.log(status);

  4. #4
    Join Date
    Oct 2013
    Posts
    7
    BTW, i'm in a learning process right now..
    I try to make a TicTacToe (canvas/javascript game)
    Are arrays the best way to check and store the cell positions or are there better ways?
    Ty!

  5. #5
    Join Date
    Nov 2010
    Posts
    1,084
    maybe - it depends on how the rest of your code works. I would suspect that you don't need for your array to be so complicated if that's all you are using it for. You can just "number" the squares from 1 to 9 (or 0 to 8 in javascript-speak) and as long as you stick to that system consistently you won't need the [1,0] notation

  6. #6
    Join Date
    May 2014
    Posts
    909
    In your original code, the biggest problem is it should never exit. You were pushing the result onto the same array, constantly increasing the size of the array with more matches to process.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles