Skip to main content

Quick Ratios with JavaScript



I always find myself needing to know how numbers compare. But I hate taking out a calculator or grabbing pencil and paper to work it out. So I wrote a small script to do it for me.

Method

Math.Ratio = function (rt1, rt2) {
 var rt1n = rt1.split(':');
 var rt2n = rt2.split(':');

 var rntd1i = false, rntd2i = false;

 for (var i = 0; i < rt1n.length; i++) {
  if (rt1n[i] == '?') {
   rntd1i = i;
  }
 }
 for (var ii = 0; ii < rt2n.length; ii++) {
  if (rt2n[ii] == '?') {
   rntd2i = ii;
  }
 }

 function rtDetermine(r1, r2, ri) {
  var isum = r1[0] / r1[1];
  return ri == 0 ? r2[1] * isum : r2[0] / isum;
 }
 
 if (rntd1i !== false && rntd2i !== false) {
  console.warn('Whoops, can only work with one unknown number.');
  return false;
 } else if (rntd1i !== false) {
  return rtDetermine(rt2n, rt1n, rntd1i);
 } else if (rntd2i !== false) {
  return rtDetermine(rt1n, rt2n, rntd2i);
 } else {
  console.warn('Whoops, need at least one unknown number.');
  return false;
 }
};

And then you can call it like so:

Math.Ratio('1920:1080','?:720');//1280

Demo
?


Comments

  1. Harrah's Reno casino and racetrack, NV - Dr.MCD
    Reno, NV (March 24, 2019) – Harrah's Reno Casino and Racetrack 여수 출장마사지 has officially opened its doors to 아산 출장샵 all 익산 출장안마 patrons with a $1,000 M 군산 출장안마 Resort 의왕 출장샵 Fee,

    ReplyDelete
  2. How do I make money from playing games and earning
    These are kadangpintar the three most popular forms of gambling, and are https://sol.edu.kg/ explained in a very concise https://deccasino.com/review/merit-casino/ and งานออนไลน์ concise manner. The most common forms filmfileeurope.com of gambling are:

    ReplyDelete
  3. Play casino - No.1 for the Casino Guru
    No longer have the opportunity filmfileeurope.com to go to the casinos or read sol.edu.kg the reviews of casinosites.one the slots you love. febcasino But they're not always the same. Sometimes you have aprcasino a new online

    ReplyDelete

Post a Comment

Popular posts from this blog

US Bank Routing Number Validation

You are probably here because you want to validate routing and bank account numbers. There is good and bad news. Good news US bank routing numbers follow a very strict format. That makes it so you can easily validate it and be assured that before you send that data to the back-end it passes the format requirements. Bad News Bank account numbers are not so fortunate. There is no strict format unlike IBAN . Dang it! I discuss is more here . Routing Number You can get really fancy and do some really deep validation for the routing number. As you can see here  there is a lot behind the format including what each set of digits represents (what kind of institution is the bank, what Federal Reserve district it is in, which state its in, etc). But we are going to keep it simple. There is a checksum. Thanks to BrainJar for presenting a simple solution. First off the number is always 9 digits. Next is the checksum algorithm. Here is our example rou