A framework that aims to only use functions and be short.
JavaScript function golf is a framework for JavaScript that aims to only use functions and be short. In fact, it's a golfing framework.
JavaScript function golf is included into the page HTML, so use it right from the console!
User | Flair | Battle Call |
---|---|---|
@schas002 | Founder | I made this for you! |
Add function golf from HTML:
<script src="http://schas002.github.com/js-function-golf/js-function-golf.js"></script>
On Programming Puzzles & Code Golf Stack Exchange, function golf is quite tricky to score - if in doubt, please ask on meta.
From JS, you can do this:
document.body.appendChild(document.createElement('script')).src = "http://schas002.github.com/js-function-golf/js-function-golf.js";
Theoretically, it is also possible to do this in function golf:
_("http://schas002.github.com/js-function-golf/js-function-golf.js");
But importing function golf when function golf is already imported is overkill. :kappa:
Or you can just download http://schas002.github.com/js-function-golf/js-function-golf.js and just import http://path.to/js-function-golf.js
replacing the site http://path.to/
with the directory where js-function-golf.js
is.
Currently the golden rule is, only functions allowed are JS function golf functions, imported (using _()
) functions and functions created in the current JavaScript, so i2s(123)
instead of 123.toString()
. The silver rule is, attributes are not allowed and replaced by existing function golf functions, so sl("ABC")
instead of "ABC".length
. Any other structures of JS are allowed.
Licensed under the ISC License. The full license follows.
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Important text ends here. Functions follow.
Don't forget - you are here forever.
$(id)
Returns an element in the document called by the ID you gave to $
.
_(address)
Appends to the body of the document JavaScript specified in the file the address of which you gave to _
.
p(data)
Prints to the console whatever you gave to p
.
p2a
instead alerts the user with the data. p2b
is deprecated in favor of p2a
.
yn(question)
Asks the user whatever you gave to yn
and returns the answer they gave, as a boolean. The answer is either "Yes" or "No".
yn2i
instead returns an integer ("Yes" is 1, "No" is 0).
i(question)
Asks the user whatever you gave to i
and returns the answer they gave. The answer is a string.
iai
instead parses the answer as an integer that is returned.
i2s(data)
Converts whatever you gave to i2s
to a string.
s2i(data)
Converts a string you gave to s2i
to an integer.
pr(number)
Finds if is whatever you gave to pr
prime. Returns 1 if the number is prime, else 0.
sl(data)
Measures the length of the string you gave to sl
.
sloc(data, query)
The sloc()
method returns the index of (the position of) the first occurrence of a specified text in a string.
siloc(data, query)
The siloc()
method returns the index of the last occurrence of a specified text in a string.
ss(data, query)
The ss()
method searches a string for a specified value and returns the position of the match.
ssl
, subs
, subsbl
There are 3 methods for extracting a part of a string:
ssl()
extracts a part of a string and returns the extracted part in a new string. The method takes 3 parameters: the initial string, the starting index (position), and the ending index (position).
subs()
is similar to ssl()
. The difference is that subs()
cannot accept negative indexes.
subsbl()
is similar to ssl()
. The difference is that the second parameter specifies the length of the extracted part.
sr(data, query, replacement)
The sr()
method replaces a specified value with another value in a string.
s2uc
, s2lc
A string is converted to upper case with s2uc()
.
A string is converted to lower case with s2lc()
.
scc(first, second)
scc()
joins two or more strings.
scha
, schca
There are 2 safe methods for extracting string characters:
The scha()
method returns the character at a specified index (position) in a string.
The schca()
method returns the unicode of the character at a specified index in a string.
sspl(data, separator)
A string can be converted to an array with the sspl()
method.
i2e(data, decimal)
i2e()
returns a string, with a number rounded and written using exponential notation.
i2fp(data, decimal)
i2fp()
returns a string, with the number written with a specified number of decimals.
i2p(data, length)
i2p()
returns a string, with a number written with a specified length.
s2f(data)
s2f()
parses a string and returns a number. Spaces are allowed. Only the first number is returned.
mmin
, mmax
mmin()
and mmax()
can be used to find the lowest or highest value in a list of arguments.
mrn()
mrn()
returns a random number between 0 (inclusive), and 1 (exclusive).
mro()
mro()
rounds a number to the nearest integer.
mrou()
rounds a number up to the nearest integer.
mrod()
rounds a number down to the nearest integer.
mrod()
and mrn()
can be used together to return a random number between 0 and, say, 10. It can be golfed with mrnr()
.
m
JavaScript provides 8 mathematical constants that can be accessed with the Math object, but in function golf they are accessed using variables starting with m
:
me // returns Euler's number
mpi // returns PI
m2r2 // returns the square root of 2
m2r_5 // returns the square root of 1/2
mln2 // returns the natural logarithm of 2
mln10 // returns the natural logarithm of 10
ml2e // returns base 2 logarithm of E
ml10e // returns base 10 logarithm of E