Last active
October 28, 2018 09:42
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}) | |
}; |
Author
polRk
commented
Oct 28, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment