Skip to content

Instantly share code, notes, and snippets.

@jkraszewski
Last active May 29, 2018 19:28
Show Gist options
  • Save jkraszewski/6a7d838bb9e12080f26d5ba101f8cd2b to your computer and use it in GitHub Desktop.
Save jkraszewski/6a7d838bb9e12080f26d5ba101f8cd2b to your computer and use it in GitHub Desktop.
test_data = [
['IP Source', 'Source Mask', 'IP Destination', 'Destination Mask'],
['123.456.789.000', '/30', '111.111.111.111', '/30'],
['123.23.45.233', '/31', '127.0.0.1', '/8'],
['1.1.1.1', '/31', '1.2.3.4', '/30'],
['n/a', '/31', '123.432.234.567', '/12'],
];
// List<List<String>> -> String
// Assumes a non-empty list of non-empty lists of Strings
function create_table(lst){
retVal = "";
size = lst[0].length;
spaces = [lst[0].length];
for(let x = 0; x < lst[0].length; x++){
spaces[x] = 0;
}
//get spaces info
for(let i = 0; i < lst.length; i++){
for(let y = 0; y < size; y++){
tmp = Math.floor(lst[i][y].length / 4) + 1;
if(tmp > spaces[y]) {
spaces[y] = tmp;
}
tmp = 1;
}
}
// start adding values to return String
for(let i = 0; i < lst.length; i++){
for(let y = 0; y < lst[i].length; y++) {
retVal += lst[i][y];
this_size = Math.floor(lst[i][y].length / 4);
for(k = this_size; k < spaces[y]; k++) {
retVal += '\t';
}
}
retVal += '\n';
if(i === 0) {
sum = 0;
for(let c = 0; c < lst[0].length; c++){
sum += lst[0][c].length + 1;
}
sum += 6;
for(let c = 0; c < sum; c++){
retVal += '-';
}
retVal += '\n';
}
}
return retVal;
}
result = create_table(test_data);
console.log(result);
@jkraszewski
Copy link
Author

Fixed for loop size problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment