0

I'm trying to return 1/2 for all the input values that are <0.05. (Don't bother with the math. I just want to return 1/2 for all the values that are <0.05).

Here is the snippet that I'm working on:

function simpler(whole, x, y) {
  if (x == 0) {
    return whole;
  } else if (x == 1 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 2 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 3 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 4 && y == 10) {
    return whole + '<sup>' + 2 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 5 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 6 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 7 && y == 10) {
    return whole + '<sup>' + 7 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 8 && y == 10) {
    return whole + '<sup>' + 4 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 9 && y == 10) {
    return whole + '<sup>' + 9 + '</sup>/<sub>' + 10 + '</sub>'
  } else {
    return whole + '<sup>' + x + '</sup>/<sub>' + y + '</sub>';
  }
}

function Fraction() {}
Fraction.prototype.convert = function(x, improper) {
  improper = improper || false;
  var abs = Math.abs(x);
  this.sign = x / abs;
  x = abs;
  var stack = 0;
  this.whole = !improper ? Math.floor(x) : 0;
  var fractional = !improper ? x - this.whole : abs;

  function recurs(x) {
    stack++;
    var intgr = Math.floor(x);
    var dec = (x - intgr);
    if (dec < 0.0019 || stack > 20) return [intgr, 1];
    var num = recurs(1 / dec);
    return [intgr * num[0] + num[1], num[0]]
  }
  var t = recurs(fractional);
  this.numerator = t[0];
  this.denominator = t[1];
}

Fraction.prototype.toString = function() {
  var l = this.sign.toString().length;
  var sign = l === 2 ? '-' : '';
  var whole = this.whole !== 0 ? this.sign * this.whole + ' ' : sign;
  return simpler(whole, this.numerator, this.denominator);

}

function f() {
  var text = $('#text').val();
  var roundUp = 0.4;
  var digit = (text * 1).toFixed(1);
  var frac = new Fraction()
  frac.convert(digit, false)
  $('#result').html(frac.toString());
}

$('#text').on('change', function() {
  f();
});

f();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" value="0.04" />
<div id="result"></div>

Here in the example, I've used toFixed(1) to keep only one decimal of the input. As a result, if the input value is <0.05 it returns nothing. But I want to return 1/2 instead.

Ashonko
  • 533
  • 8
  • 34
  • I don't completely understand what you're trying to do (1/2 !== 0.05 and your example converts 0.25 into 2/5 for example) but you may benefit from looking here: https://stackoverflow.com/questions/22368529/jquery-convert-results-of-decimal-into-fractions – Daniel May 11 '18 at 15:38
  • Thanks for the suggestion. I want the output value to be `1/2` for inputs `<0.05` no matter what the actual math is. @Daniel – Ashonko May 11 '18 at 16:09

3 Answers3

1

How about something simple like: (I used in the snippet)

if ( '' === whole && 0 == x & 1 == y ) {
  return '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>';
}

or maybe just:

if ( '' === whole ) {
  return '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>';
}

function simpler(whole, x, y) {
  if ( '' === whole && 0 == x & 1 == y ) {
    // If the input value is < 0.05, `whole` is an empty string with `x` = 0 and `y` = 1
    return '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 0) {
    return whole;
  } else if (x == 1 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 2 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 3 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 4 && y == 10) {
    return whole + '<sup>' + 2 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 5 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 6 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 7 && y == 10) {
    return whole + '<sup>' + 7 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 8 && y == 10) {
    return whole + '<sup>' + 4 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 9 && y == 10) {
    return whole + '<sup>' + 9 + '</sup>/<sub>' + 10 + '</sub>'
  } else {
    return whole + '<sup>' + x + '</sup>/<sub>' + y + '</sub>';
  }
}

function Fraction() {}
Fraction.prototype.convert = function(x, improper) {
  improper = improper || false;
  var abs = Math.abs(x);
  this.sign = x / abs;
  x = abs;
  var stack = 0;
  this.whole = !improper ? Math.floor(x) : 0;
  var fractional = !improper ? x - this.whole : abs;

  function recurs(x) {
    stack++;
    var intgr = Math.floor(x);
    var dec = (x - intgr);
    if (dec < 0.0019 || stack > 20) return [intgr, 1];
    var num = recurs(1 / dec);
    return [intgr * num[0] + num[1], num[0]]
  }
  var t = recurs(fractional);
  this.numerator = t[0];
  this.denominator = t[1];
}

Fraction.prototype.toString = function() {
  var l = this.sign.toString().length;
  var sign = l === 2 ? '-' : '';
  var whole = this.whole !== 0 ? this.sign * this.whole + ' ' : sign;
  return simpler(whole, this.numerator, this.denominator);

}

function f() {
  var text = $('#text').val();
  var roundUp = 0.4;
  var digit = (text * 1).toFixed(1);
  var frac = new Fraction()
  frac.convert(digit, false)
  $('#result').html(frac.toString());
}

$('#text').on('change', function() {
  f();
});

f();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" value="0.04" />
<div id="result"></div>
0 or 0.00 would also return &frac12;
Sally CJ
  • 15,362
  • 2
  • 16
  • 34
0

I don't know if I got everything right that you want to achieve. But what about this:

Basically I just define a fix template (var low_num_template) and assign this to your #result box when the value is below 0.05 (assuming that you only have numbers in this input. If there are alphabetical characters you would have to check for this first before using parseFloat() to prevent an error).

Did I get you right with this?

function simpler(whole, x, y) {
  if (x == 0) {
    return whole;
  } else if (x == 1 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 2 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 3 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 4 && y == 10) {
    return whole + '<sup>' + 2 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 5 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 6 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 7 && y == 10) {
    return whole + '<sup>' + 7 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 8 && y == 10) {
    return whole + '<sup>' + 4 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 9 && y == 10) {
    return whole + '<sup>' + 9 + '</sup>/<sub>' + 10 + '</sub>'
  } else {
    return whole + '<sup>' + x + '</sup>/<sub>' + y + '</sub>';
  }
}

function Fraction() {}
Fraction.prototype.convert = function(x, improper) {
  improper = improper || false;
  var abs = Math.abs(x);
  this.sign = x / abs;
  x = abs;
  var stack = 0;
  this.whole = !improper ? Math.floor(x) : 0;
  var fractional = !improper ? x - this.whole : abs;

  function recurs(x) {
    stack++;
    var intgr = Math.floor(x);
    var dec = (x - intgr);
    if (dec < 0.0019 || stack > 20) return [intgr, 1];
    var num = recurs(1 / dec);
    return [intgr * num[0] + num[1], num[0]]
  }
  var t = recurs(fractional);
  this.numerator = t[0];
  this.denominator = t[1];
}

Fraction.prototype.toString = function() {
  var l = this.sign.toString().length;
  var sign = l === 2 ? '-' : '';
  var whole = this.whole !== 0 ? this.sign * this.whole + ' ' : sign;
  return simpler(whole, this.numerator, this.denominator);

}

function f() {
  var text = $('#text').val();
  text = parseFloat(text);
  var roundUp = 0.4;
  var digit = (text * 1).toFixed(1);
  var frac = new Fraction()
  frac.convert(digit, false)
  
  var low_num_template = '<sup>1</sup>/<sub>2</sub>';
  $('#result').html((text < 0.05) ? low_num_template : frac.toString());
  
}

$('#text').on('change', function() {
  f();
});

f();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" value="0.04" />
<div id="result">asdasd</div>
hallleron
  • 1,972
  • 3
  • 13
  • 18
  • Thanks for the suggestion, but wouldn't it be cleaner if we could use some logic in the `function simpler`, maybe something like, `if (x == 0 && y == 0 && whole == 0)` ... `return whole + '' + 1 + '/' + 2 + ''` ... or something like this that actually works? – Ashonko May 14 '18 at 09:33
-1

function simpler(whole, x, y) {
  if (x == 0) {
    return whole;
  } else if (x == 1 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 2 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 3 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 4 && y == 10) {
    return whole + '<sup>' + 2 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 5 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 6 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 7 && y == 10) {
    return whole + '<sup>' + 7 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 8 && y == 10) {
    return whole + '<sup>' + 4 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 9 && y == 10) {
    return whole + '<sup>' + 9 + '</sup>/<sub>' + 10 + '</sub>'
  } else {
    return whole + '<sup>' + x + '</sup>/<sub>' + y + '</sub>';
  }
}

function Fraction() {}
Fraction.prototype.convert = function(x, improper) {
  improper = improper || false;
  var abs = Math.abs(x);
  this.sign = x / abs;
  x = abs;
  var stack = 0;
  this.whole = !improper ? Math.floor(x) : 0;
  var fractional = !improper ? x - this.whole : abs;

  function recurs(x) {
    stack++;
    var intgr = Math.floor(x);
    var dec = (x - intgr);
    if (dec < 0.0019 || stack > 20) return [intgr, 1];
    var num = recurs(1 / dec);
    return [intgr * num[0] + num[1], num[0]]
  }
  var t = recurs(fractional);
  this.numerator = t[0];
  this.denominator = t[1];
}

Fraction.prototype.toString = function() {
  var l = this.sign.toString().length;
  var sign = l === 2 ? '-' : '';
  var whole = this.whole !== 0 ? this.sign * this.whole + ' ' : sign;
  return simpler(whole, this.numerator, this.denominator);

}

function f() {
  var text = $('#text').val();
  var roundUp = 0.4;
  var digit = (text * 1).toFixed(1);
  var frac = new Fraction()
  frac.convert(digit, false)
  if(parseInt(text)<0.05){
      $('#result').html("1/2");
   }
 else{
  $('#result').html(frac.toString());}
  }

$('#text').on('change', function() {
  f();
});

f();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" value="0.04" />
<div id="result"></div>
Nomair Ghanem
  • 524
  • 4
  • 12