Paste: Making pairs in javascript

Author: Joey
Mode: javascript
Date: Fri, 7 Oct 2011 16:42:21
Plain Text |
var pairs = function (data) {
  var make_pairs = function (first, rest) {
    var pairs = [];
    for (var i=0; i<rest.length; i++) {
      pairs.push([first, rest[i]]);
    }
    return first? pairs.concat(make_pairs(rest[0], rest.slice(1))): pairs;
  };
  return make_pairs(data[0], data.slice(1));
};


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

New Annotation

Summary:
Author:
Mode:
Body: