JavaScript bra size calculator

One of the more mesmerizing websites I've worked on recently was for a lingerie boutique in the UK. Aside from the unenviable task of having to look at pictures of women in lingerie all day, I was also forced (forced!) to write a bra size calculator.

The theory behind bra size calculation is arcane and somewhat magical. Understanding of it does not come easily to man nor beast, so it is lucky that I, falling cleanly into neither category, have passed through pain and torment to save you the trouble.

Check it out.

Pleasing, no? The code looks like this, and can be found here:


var BraCalculator = {

/**
* The string to be returned when the result could not be calculated.
*/
unknownString: "Unknown",

cupSizes: ["A", "B", "C", "D", "DD", "E", "EE", "F", "FF", "G", "GG", "H", "HH",
"J", "JJ", "K", "KK", "L", "LL", "M", "MM", "N", "NN"],

/**
* Returns the correct bra size for given under bust and over bust measurements
* @param {Number} underBust The measurement taken under the bust (in inches)
* @param {Number} overBust The measurement taken over the bust (in inches)
* @return {String} The correct bra size for the given measurements (e.g. 32C, 40DD, etc)
*/
calculateSize: function(underBust, overBust) {
var bandSize = this.calculateBandSize(underBust);
var cupSize = this.calculateCupSize(bandSize, overBust);

if (bandSize && cupSize) {
return bandSize + cupSize;
} else {
return this.unknownString;
};
},

/**
* Calculates the correct band size for a given under bust measurement
* @param {Number} underBust The measurement under the bust
* @return {Number} The correct band size
*/
calculateBandSize: function(underBust) {
var underBust = parseInt(underBust, 10);
return underBust + (underBust % 2) + 2;
},

/**
* Calculates the Cup size required given the band size and the over bust measurement
* @param {Number} bandSize The measured band size (should be an even number)
* @param {Number} overBust The measurement taken over the bust
* @return {String} The appropriate alphabetical cup size
*/
calculateCupSize: function(bandSize, overBust) {
var bandSize = parseInt(bandSize, 10);
var overBust = parseInt(overBust, 10);
var diff = overBust - bandSize;

var result = this.cupSizes[diff][/diff];

//return false if we couldn't lookup a cup size
return result ? result : false;
}
};

var BraCalculator = {

/**
* The string to be returned when the result could not be calculated.
*/
unknownString: "Unknown",

cupSizes: ["A", "B", "C", "D", "DD", "E", "EE", "F", "FF", "G", "GG", "H", "HH",
"J", "JJ", "K", "KK", "L", "LL", "M", "MM", "N", "NN"],

/**
* Returns the correct bra size for given under bust and over bust measurements
* @param {Number} underBust The measurement taken under the bust (in inches)
* @param {Number} overBust The measurement taken over the bust (in inches)
* @return {String} The correct bra size for the given measurements (e.g. 32C, 40DD, etc)
*/
calculateSize: function(underBust, overBust) {
var bandSize = this.calculateBandSize(underBust);
var cupSize = this.calculateCupSize(bandSize, overBust);

if (bandSize && cupSize) {
return bandSize + cupSize;
} else {
return this.unknownString;
};
},

/**
* Calculates the correct band size for a given under bust measurement
* @param {Number} underBust The measurement under the bust
* @return {Number} The correct band size
*/
calculateBandSize: function(underBust) {
var underBust = parseInt(underBust, 10);
return underBust + (underBust % 2) + 2;
},

/**
* Calculates the Cup size required given the band size and the over bust measurement
* @param {Number} bandSize The measured band size (should be an even number)
* @param {Number} overBust The measurement taken over the bust
* @return {String} The appropriate alphabetical cup size
*/
calculateCupSize: function(bandSize, overBust) {
var bandSize = parseInt(bandSize, 10);
var overBust = parseInt(overBust, 10);
var diff = overBust - bandSize;

var result = this.cupSizes[diff][/diff];

//return false if we couldn't lookup a cup size
return result ? result : false;
}
};

And to apply it to your own pages, use something a bit like this:

jQuery(document).ready(function(){
//add listeners to band and cup measurement text boxes
jQuery('#back').keyup(Honeys.updateBraSizeCalculation);
jQuery('#cup').keyup(Honeys.updateBraSizeCalculation);
});

var Honeys = {
updateBraSizeCalculation: function() {
var back = jQuery('#back')[0].value;
var cup = jQuery('#cup')[0].value;

if (back.length > 0 && cup.length > 0) {
jQuery('#fit')[0].value = BraCalculator.calculateSize(back, cup);
};
}
};
jQuery(document).ready(function(){
//add listeners to band and cup measurement text boxes
jQuery('#back').keyup(Honeys.updateBraSizeCalculation);
jQuery('#cup').keyup(Honeys.updateBraSizeCalculation);
});

var Honeys = {
updateBraSizeCalculation: function() {
var back = jQuery('#back')[0].value;
var cup = jQuery('#cup')[0].value;

if (back.length > 0 && cup.length > 0) {
jQuery('#fit')[0].value = BraCalculator.calculateSize(back, cup);
};
}
};

Now we're talking UK sizes here, so exercise extreme caution! It should be trivial to adapt to your country with our lovely conversion charts.

Don't pretend you're not going to play with it. You know you are. Like, right now.

Share Post:

What to Read Next

If you enjoyed this dive into creating a JavaScript bra size calculator, you might find the article Writing Compressible JavaScript interesting as it offers optimization strategies for writing efficient code. Additionally, explore Answering Nicholas Zakas' JavaScript quiz to challenge your understanding of JavaScript's intricacies.