Well, first off, whitespace:
var phi=function(n){var count=" " //an empty string
for(var i =1;i<=n;i++){
if(for(var j=1;j<=i;j++){
i%j===0;n%j===0;count += j.str;}) //appended the j as string into count
{return count.length;}}
};
has far to little. I reformatted and made this (put in a couple of semicolons, too):
var phi = function (n) {
var count = " "; //an empty string
for (var i = 1; i <= n; i++) {
if (for (var j = 1; j <= i; j++) {
i % j === 0;
n % j === 0;
count += j.str; //appended the j as string into count
}) {
return count.length;
}
}
};
" "
is not an empty string. ""
is.
Now, why the heck do you have a for
in an if
? Try rewriting it without one. Array#all
ought to help you with that. Once I get a chance I'll try my hand at it.
.str
isn't needed. Just write count += j
.
I can't comment on your algorithm because I can't tell what it is. This is why we write readable code. Don't use stupid hacks that make your program .03% faster that make you use stupid, unreadable tricks like, say, putting a for
in an if
. Prioritize readability until you absolutely have to do otherwise.
for
in anif
?? – Veedrac Jun 7 at 20:45