by nir · 16y ago · 0 comments
function triangle(a,b){
if(a > 0 && b > 0 ){
function sqroot(x){
if( x > 0 ){
return Math.pow(x,.5);
}
else {
return 0;
}
}
return sqroot( a*a + b*b );
}
else {
return 0;
}
}
Can be made much cleaner: function triangle(a,b){
if (a <= 0 || b <= 0 ) return 0
function sqroot(x){ return ( x > 0 ? Math.pow(x,.5) : 0 ) }
return sqroot( a*a + b*b )
}
I don't know about a whitespace-sensitive language in the browser. Much JS is written by non-coders, later quickly edited on live servers (yeah, I know. But that's reality) etc.. It all comes down to taste, of course, but personally I find Ruby at least as readable as Python, so I'm not sure whitespaces are where the elegance is found. function triangle(a,b)
a <= 0 || b <= 0 ? 0 : (a = a*a + b*b) > 0 ? Math.pow(a,.5) : 0;