Skip to content

Instantly share code, notes, and snippets.

@polRk
Last active October 28, 2018 09:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polRk/ebbf50ec729eaf3cd66a9f693973215f to your computer and use it in GitHub Desktop.
Save polRk/ebbf50ec729eaf3cd66a9f693973215f to your computer and use it in GitHub Desktop.
Find the best placement of buttons in columns. Viber richMessage (see example.ts)
const findBestPlacement = (count) => {
const result = [4, 5, 6, 7].map(rows => ({
'Rows': rows,
'Columns': ((count / rows) ^ 0) === (count / rows) ? count / rows : (count / rows >> 0) + 1,
'Excess': count % rows,
'Free': count % rows === 0 ? 0 : rows - count % rows
}));
return [...result]
.filter((item) => {
return item.Columns <= 6;
})
.sort((a, b) => {
if (a.Columns === b.Columns) {
return (a.Excess < b.Excess || b.Excess === 0) ? 1 : (a.Excess > b.Excess || a.Excess === 0) ? -1 : 0;
}
else {
return (a.Columns < b.Columns) ? -1 : 1;
}
});
};
const findBestPlacement = (count: number): { Rows: number; Columns: number; Excess: number; Free: number }[] => {
const result = [4, 5, 6, 7].map(rows => ({
Rows: rows,
Columns: ((count / rows) ^ 0) === (count / rows) ? count / rows : (count / rows >> 0) + 1,
Excess: count % rows,
Free: count % rows === 0 ? 0 : rows - count % rows
}));
return [...result]
.filter((item) => {
return item.Columns <= 6;
})
.sort((a, b) => {
if (a.Columns === b.Columns) {
return (a.Excess < b.Excess || b.Excess === 0) ? 1 : (a.Excess > b.Excess || a.Excess === 0) ? -1 : 0;
}
else {
return (a.Columns < b.Columns) ? -1 : 1;
}
})
};
@polRk
Copy link
Author

polRk commented Oct 28, 2018

Array.from(Array(36), (d, i) => i + 7).map((i) => console.table(findBestPlacement(i)))

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