The accepted answer is wrong. You don't need an exception, you need a better data structure.
An array for cup sizes? In 2024? Use a continuous mapping function. Cup sizes aren't discrete categories for extreme inputs — they're a spectrum. I can't believe I'm saying this about bras on Stack Overflow but here we are.
function continuousCupSize(bandInches, bustInches) {
const diff = bustInches - bandInches;
const cupMap = new Map([
[0, 'AA'], [1, 'A'], [2, 'B'], [3, 'C'],
[4, 'D'], [5, 'DD'], [6, 'E'], [7, 'F'],
[8, 'G'], [9, 'H'], [10, 'I'],
[14, 'STRUCTURAL_PERMIT_REQUIRED'],
]);
const idx = Math.round(diff / 2.54);
return cupMap.get(idx) ?? `CUSTOM_${idx}_CONTACT_ENGINEER`;
}
continuousCupSize(36, 72);
EDIT: Yes I know a Map with integer keys is "just an array with extra steps." I have received 47 comments about this. The point is extensibility. Please stop.
EDIT 2: No, a linked list is not better. Stop.
EDIT 3: The person who suggested a B-tree "because it's a bra" has been reported.
Share
Improve this answer
answered Jun 16, 2024 at 09:12
algorithm_purist_42
142,000
14
142
341