Skip to content

Instantly share code, notes, and snippets.

@ranafarhanakram
Created February 13, 2018 10:02
Show Gist options
  • Save ranafarhanakram/e5f31e56b9383cc3ede28b5fae08806d to your computer and use it in GitHub Desktop.
Save ranafarhanakram/e5f31e56b9383cc3ede28b5fae08806d to your computer and use it in GitHub Desktop.
var input = [[1, 3], [9, 10], [13, 15], [4,14], [21,17], [2,7]];
//output = [1,9,13], [1,9,14], [1,9,15]
//console.log(input[0][0])
var allElements = [];//new Array(input.length);
var y = 0;
var z = 0;
for (var x = 0; x < input.length; x++){
allElements.push(input[x][0]);
allElements.push(input[x][1]);
}
var result = [];
var result2 = [];
for (var x = 0; x < allElements.length; x++){
result[x] = [];
result[x].push(allElements[x]);
}
var z = 0;
for(var x = 0; x < result.length; x++){
for(var y = 0; y < allElements.length; y++,z++){
if(x == y && (x%2 === 0)){
y +=2;
if (!result2[z]) result2[z] = [];
if(allElements[y]&&result[x][0]){
//console.log(y);
result2[z].push(result[x][0]);
result2[z].push(allElements[y]);
}else{
z--;
}
}else if(x == y){
--z;
}else{
if (!result2[z]) result2[z] = [];
//console.log(y);
if(allElements[y]&&result[x][0]){
result2[z].push(result[x][0]);
if(allElements[y])
result2[z].push(allElements[y]);
}else{
z--;
}
}
}
}
var result3 = [];
var z = 0;
for(var x = 0; x < result2.length; x++){
for(var y = 0; y < allElements.length; y++,z++){
if(x == y && (x%2 === 0)){
y +=2;
if(result2[x][0] && result2[x][1] && allElements[y]){
if (!result3[z]) result3[z] = [];
result3[z].push(result2[x][0]);
result3[z].push(result2[x][1]);
if(allElements[y])
result3[z].push(allElements[y]);
}else{
z--;
}
}else if(x==y ){
--z;
}else{
if(result2[x][0] && result2[x][1] && allElements[y]){
if (!result3[z]) result3[z] = [];
result3[z].push(result2[x][0]);
result3[z].push(result2[x][1]);
if(allElements[y])
result3[z].push(allElements[y]);
}else{
z--;
}
}
}
}
console.log(result3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment