Skip to content

Instantly share code, notes, and snippets.

@dk5ax
Last active October 26, 2023 09:32
Show Gist options
  • Save dk5ax/ca1d80b011a029f361cba3667682d9e8 to your computer and use it in GitHub Desktop.
Save dk5ax/ca1d80b011a029f361cba3667682d9e8 to your computer and use it in GitHub Desktop.
const input = {
"columns": 80,
"padChar": "·",
"events": [
{ "name": "Code 100", "location": "Zagreb, Croatia", "date": "29.11.2023" },
{ "name": "LIVE", "date": "ongoing" },
{ "name": "Coffee With Developers", "location": "various", "date": "ongoing"} ,
{ "name": "World Congress","location": "Berlin, Germany", "date":"17-19.07.2024" }
]
};
console.log(`${input.padChar.repeat(input.columns)}`);
input.events.forEach(event => {
const name = ((event.name?.length) ?? 0) ? (' ' + event.name + ' ') : '';
const location = ((event.location?.length) ?? 0) ? (' ' + event.location + ' ') : '';
const date = ((event.date?.length) ?? 0) ? (' ' + event.date + ' ') : '';
const toPadCount = input.columns - 2 - (name.length) - (location.length) - (date.length)
const lspaces = Math.floor(input.columns/2-(name.length)) - Math.floor(location.length / 2);
const rspaces = toPadCount - lspaces;
console.log(`${input.padChar}${name}${input.padChar.repeat(lspaces)}${location}${input.padChar.repeat(rspaces)}${date}${input.padChar}`);
});
console.log(`${input.padChar.repeat(input.columns)}`);
@dk5ax
Copy link
Author

dk5ax commented Oct 26, 2023

Getting there. Misses the spaces around the words and the upper and downer line though.

Done.

@codepo8
Copy link

codepo8 commented Oct 26, 2023

No spaces yet :)

@dk5ax
Copy link
Author

dk5ax commented Oct 26, 2023

Thanks, I should have read more carefully.....not just flying over the sample, not even realizing there were spaces....

@codepo8
Copy link

codepo8 commented Oct 26, 2023 via email

@dk5ax
Copy link
Author

dk5ax commented Oct 26, 2023

Ok, should be fine now....is there some broader data set available to test with?

@codepo8
Copy link

codepo8 commented Oct 26, 2023

Not yet :)

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