3

I am trying to make a function that takes an array and creates a pair of arrays for example an array [1,2,3,4] the pair will be:

pair = [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] ;

And the pairs would be:

pairs = [[[1,2],[1,3]], [[1,2],[1,4]], [[1,2],[2,3]] .... [[2,4],[3,4] ] ;

So far my function looks like this:

function makePairs(arr) {

    var l = arr.length -1 ,  
    pair =  [];
        for(var i=0; i < l ; i++ ) {
          for(var j=i+1; j <= l ; j++ ) {
            pair.push( [arr[i],arr[j]]) ;
          }
        }  

  // i get the desired pair by the above nested for loop... 
  // console.log(pair) ; 

  // i try to do the same nested for loop with the pair array.. 
  // but i get [circular object Array]; 
  var k =  pair.length -1,
  pairs = [] ;
          for(var m=0; m < k ; m++ ) {
          for(var n=m+1; n <= k ; n++ ) {
            pairs.push( [pair[m],pair[n]]) ;
          }
        }
    return pairs; 
}

console.log(  makePairs([1,2,3,4]) );

So the pair gives me the desired pair but when I do the same type of nested for loop with the pair array, I get [circular object Array]. I thought the nested for loop will work on the pairs too but it does not. I read that circular reference is formed between a javascript object and a native object causing memory leak but I don't know if that's happening here. please help.

error..

Psych Half
  • 1,393
  • 1
  • 11
  • 22

3 Answers3

1

I wonder if the issue is the debugger itself. It's outputting [circular object Array] any time it's referring to an item already referred to.

Try making a lot more console messages. Replace your last line with:

var answer = makePairs([1,2,3,4]);
for (var i = 0; i < answer.length; ++i) {
    console.log("[[" + answer[i][0][0] + ", " + answer[i][0][1] + "], [" + 
         answer[i][1][0] + ", " + answer[i][1][1] + "]]");
}

I bet it will print out ok.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • @PsychHalf Don't feel stupid - your code was fine, and everyone always trusts their debuggers. What debugger are you using? – Scott Mermelstein Oct 17 '13 at 20:40
  • thanks... umm.. well.. whatever it was i won't be using it again.. i'm throwing it out of my window right away.. – Psych Half Oct 17 '13 at 20:47
  • @PsychHalf Not to be too pushy, but was it developer tools with a specific web browser like IE's or Chrome's F12 utilities, or a plugin like FireBug Lite, or something else? It would be good to know "Hey, don't be surprised if you see this error when using this specific tool." – Scott Mermelstein Oct 17 '13 at 20:50
  • 1
    try any web based console.. like jsconsole.. or others.. they'all give you circular.. – Psych Half Oct 17 '13 at 21:03
0

maybe you can make use of

console.dir(  makePairs([1,2,3,4])  )

on chrome-console and latesst ff-firebug

john Smith
  • 17,409
  • 11
  • 76
  • 117
0

Try with JSON.stringify() and JSON.parse() like below:

console.log(
  makePairs(
    JSON.parse(
      JSON.stringify([1,2,3,4])
    )
  )
);
itsazzad
  • 6,868
  • 7
  • 69
  • 89