quote:
Originally posted by CookieRevised
quote:
Originally posted by phalanxii
Next, you define your own functions like this (these are a few of mine):code:
var Custom = {
"base" : function (Number, FromBase, ToBase) {
...much stuff...
}
objectname.toString([radix])
(convert to a base)
and
parseInt(numString, [radix])
(convert from a base)
?
Oh, cool. Never knew about that
. Thanks, Cookie!
PS. Using Shondoit's way to make functions is much better and easier now...
EDIT: Here are my new and improved functions.
code:
function base (x, fromBase, toBase) {
return parseInt(x, fromBase).toString(toBase).toUpperCase()
}
function choose (n, k) {
if (n != Math.floor(n) || k != Math.floor(k) || n < 0 || k < 0 || k > n) return 0
if (n == k || n == 0 || k == 0) return 1
if (n > 170) return Number.POSITIVE_INFINITY
return eval(Factorial(n) + "/(" + Factorial(n - k) + "*" + Factorial(k) + ")")
function Factorial (x) {
var Factorial = ""
for (var i = 1; i < x; i++) {
Factorial += i + "*"
}
Factorial += x
return Factorial
}
}
function gcd (a, b) {
if (a != Math.floor(a) || b != Math.floor(b)) return undefined
if (b == 0) return Math.abs(a)
else return Math.abs(gcd(b, a % b))
}
function isprime (n) {
if (n != Math.floor(n) || n < 0) return undefined
if (n == 0 || n == 1) return false
if (n == 2 || n == 3) return true
if (n % 2 == 0 || n % 3 == 0) return false
for (var k = 1; 6 * k <= Math.sqrt(n); k++) {
if (n % (6 * k - 1) == 0 || n % (6 * k + 1) == 0) return false
}
return true
}
function lcm (a, b) {
if (a != Math.floor(a) || b != Math.floor(b)) return undefined
return Math.abs(a * b / gcd(a, b))
}
function root (b, n) {
if (n == undefined) n = 2
return Math.pow(b, 1 / n)
}