Algorithm to replace this duplicated code I've got a simple thing that I inherited from another developer and think it could be replaced with something a bit more elegant. What this is doing is comparing the length of a string to a set value, and setting the font size accordingly:

if(maxLen&lt;50){$('.fc-cards').css('font-size',40);}
if(maxLen&lt;100){$('.fc-cards').css('font-size',25);}
if(maxLen&lt;150){$('.fc-cards').css('font-size',22);}
if(maxLen&lt;200){$('.fc-cards').css('font-size',20);}


If the min font size is 18, and the max 40, what a nice way to dynamically figure a font size with a range of &lt;50 chars being 40px to >200 being 18px... it doesn't have to match the exact values mentioned above, a sliding scale would be perfect.
So, a linear mapping from 50-200 onto 40-20 is (40-20)/(200-50) = 0.1333 font size points per length. So we can can compute this as

$('.fc-cards').css('font-size', 20 + 0.1333 * (200 - maxLen));


for a sliding linear falloff in font size. If you want to clamp the sizes to change in exactly the same way, you could index them into an array of sizes, computed by the modulus of the length, i.e. something like...

 $('.fc-cards').css('font-size', [40, 25, 22, 20][Math.floor(maxLen / 50)]);


But you'll need suitable checks on the length to ensure you remain in bounds.

These solutions are debatably more elegant though. They take less code, but they're not as easy to read as definitely solving the same problem your original if tests did. I'd probably prefer a loop over a dictionary of range/size possibilities as the best solution:

sizes = [50, 100, 150, 200];
fonts = [40, 25, 22, 20];

for (var i = 0; i &lt; sizes.length; i++)
{
  if (maxLen &lt; sizes[i])
  {
    $('.fc-cards').css('font-size', fonts[i]);
    break;
  }
}


Edit: Changed based on comment that for/in doesn't ensure enumeration order.

I wouldn't rage against the original solution in a code review, either. Elegance is found in the clearest expression of the goal, in tension with the solution efficiency and maintainability (where, in my view, clear expression wins 99/100 times). Efficiency is not a worry here, and adding/removing sizes from the range in the original code is trivial. Hence, pick whatever your team finds easiest to read a solving the "Change the font size based on length in these windows" problem is.