Aspect ratio string

Caclulates the aspect ratio string notation (e.g. "16:9") for a given width or height.

by JibstaMan

JavaScript

var ratios = [
	{
  	ratio: 4 / 3,
    aspect: "4:3"
  },
  {
  	ratio: 16 / 9,
    aspect: "16:9"
  },
  {
  	ratio: 21 / 9,
    aspect: "16:9"
  }
];

function getAspectRatio(width, height)
{
  var a = (width > height) ? width : height,
		  b = (width > height) ? height : width;

  for (var i = 0; i < ratios.length; i++)
  {
    var ratio = ratios[i];
    var w = b * ratio.ratio;
    var diff = Math.abs(a - w);
    console.log(diff);
    if (diff < 3)
    {
      return ratio.aspect;
    }
  }
  return false;
}

console.log(getAspectRatio(175, 98));
console.log(getAspectRatio(98, 175));
console.log(getAspectRatio(750, 1334));