4 ms·
Javascript example in the article: function triangle(a,b){ if(a > 0 && b > 0 ){ function sqroot(x){ if( x > 0 ){ return Math.pow(
by nir 17y ago
Javascript example in the article:
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.
- TNO 17y agoWith JavaScript 1.8.1: function triangle(a,b) a <= 0 || b <= 0 ? 0 : (a = a*a + b*b) > 0 ? Math.pow(a,.5) : 0;
- jrockway 17y agoIs ?: really not right-associative in JavaScript? I thought PHP was the only language that messed that one up...
- WalkingDead 17y agoDoesn't work on V8, at least not on the version I am using. Which JS engine is 1.8.1 complaint?
- sid0 17y agoSpiderMonkey in Firefox 3.5 and above.
- deleted 17y ago[deleted]