-
Thought I'd just add a factorial in :):
Code:
<script type="text/javascript">
Number.prototype.Fact = function(){
var i = 1;
for(x=1;x<this;x++){
i += x * i;
}
return i;
};
var num = 5;
alert(num.Fact());
</script>
In a few seconds, mburt is gonna post saying "I've got a shorter version Nile.", and then twey is gonna be like "Mine is only 2 lines though."
-
Code:
Number.prototype.Fact = function() {
for (i = 1, x = 1;i < this;i++) {
x += i * x;
}
return x;
};
Haha... Only one line (I think) in difference.
-
Twey's turn. Lol, that's basically the same thing as mine. :)
-
Quick 'n' dirty:
Code:
Number.prototype.fact = function() {
for (var i = 1, r = 1; i <= this; r *= i++);
return r;
};
One-liner:
Code:
Number.prototype.fact = function() { for (var i = 1, r = 1; i <= this; r *= i++); return r; };
Safe:
Code:
Number.prototype.fact = function() {
if (this < 0) return NaN;
for (var i = 1, r = 1; i <= this; r *= i++);
return r;
};
Using my toolkit:
Code:
var fact = (function(F, O) {
return F.compose(F.curry(F.reduce)(O.multiply), F.curry(F.range)(1));
})(Functional. Functional.Operator);
Without shortcuts:
Code:
var fact = Functional.compose(Functional.curry(Functional.reduce)(Functional.Operator.multiply), Functional.curry(Functional.range)(1));
In Python:
Code:
import operator
def fact(x):
return reduce(operator.mul, range(1, x + 1))
In Haskell:
Code:
fact = product . (enumFromTo 1)
Any more questions? :p
-
Wow... I like that first function. I never think of using loops like that. But I guess you just look at it like this:
Code:
for (variables;condition(s);assigment)
Is your toolkit on the web anywhere Twey?
-
Not yet. You can do most of it with Functional Javascript.
You can do anything you like within a for loop. If I'd wanted to, I could have written it like this:
Code:
Number.prototype.fact = function() {
for (var i = 0, r = 1; r *= ++i, i < this; );
return r;
};
A for loop is divided into three sections. They're commonly labelled 'initialisation', 'condition', and 'incrementation' because that's what they generally do, but those labels are stylistic: you can put anything you like in there. Pragmatically speaking, they could be given the better (but admittedly less stylish) titles of 'code that runs once when the loop is first encountered', 'code that runs before each iteration of the loop', and 'code that runs after each iteration of the loop'. The first two have some special properties: the 'initialisation' section allows 'var' statements, and the 'condition section' can terminate the loop if its return value is falsy. The 'incrementation' section is free-form.
This said, the 'initialisation', 'condition', 'incrementation' style is usually the least messy. There are some handy variants, though, like:
Code:
for (var i = 0, e; e = a[i++]; ) // leaves 'e' bound to the current value; terminates if it hits a falsy element.
-
Thanks for the lesson on for-loops haha :D. I think I understand it a bit better now.
-
@Twey, I almost came up with your first solution before you posted, infact I had the same thing except I had one thing wrong, don't remember what it was.