I'm mostly concerned about edge cases, although I think I dealt with them all. Improvements on code quality would also be much appreciated.
/*
printRadian(90) -> π/2
printRadian(-90) -> -π/2
printRadian(180) -> π
printRadian(-180) -> -π
printRadian(270) -> 3π/2
*/
function printRadian(degrees) {
if(degrees % 180 == 0) {
if(degrees == 180) {
return "π";
}
else if(degrees == -180){
return "-π";
}else{
return degrees / 180 + "π";
}
}
var frac = degrees / 180;
// 10 ** number of decimal places
var multiplier = Math.pow(10, frac.toString().split(".")[1].length);
var numerator = frac * multiplier;
var denominator = multiplier;
//http://stackoverflow.com/a/17445304/896112 - Euclid's Algorithm
function gcd(a, b) {
return (!b)?a:gcd(b, a%b);
}
var g = gcd(numerator, denominator);
numerator = numerator/g;
denominator = denominator/g;
if(numerator == 1){
numerator = "π";
}else if(numerator == -1){
numerator = "-π";
}else{
numerator += "π"
}
if(denominator < 0){
denominator = Math.abs(denominator);
numerator = "-"+numerator;
}
return numerator +"/"+ denominator;
}
printRadian(270) -> 2π/2 -> π
is wrong, it should be3π/2
\$\endgroup\$