Given integers 0-24, I have to convert them to a polar theta in radians as a string. Ex: 0 = '0', 1 = 'pi/12', 2 = 'pi/6' etc. The pi
should be an actual pi symbol. I think that my solution is inefficient. Here is what I do:
var fracStrings = ['0/1', '1/12', '1/6', '1/4', '1/3', '5/12', '1/2', '7/12', '2/3', '3/4', '5/6', '11/12', '1/1', '13/12', '7/6', '5/4', '4/3', '17/12', '3/2', '19/12', '5/3', '7/4', '11/6', '23/12', '2/1'];
function nToString(n){
//converts angle to string representation
var num = (fracStrings[n].split('/')[0] == 1) ? '' : fracStrings[n].split('/')[0];
num = (fracStrings[n].split('/')[0] == 0) ? '0' : num + '\u03C0';
var denom = (fracStrings[n].split('/')[1] == 1) ? '' : '/' + fracStrings[n].split('/')[1];
return '' + num + denom;
}
I find it inefficient to declare a fractions string. My problem is simplifying the fraction. I cannot have 12pi/12, I just need pi. Is there an efficient way to do this? This just hurts to look at.
FYI: \u03C0
is the pi symbol.