Last active
September 7, 2017 07:07
-
-
Save moshest/2f9741e9b00eb0833d44f915ab5bd1d5 to your computer and use it in GitHub Desktop.
A JavaScript function that reproduce itself as a Quine program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A JavaScript function that reproduce itself as a Quine program. | |
* Copyright (c) 2017 Moshe Simantov | |
*/ | |
const assert = require('assert'); | |
const quine = (function () { | |
var f = '(function () {%0A var f = %27@%27;%0A%0A return eval(unescape(f).replace(/@/, f));%0A})'; | |
return eval(unescape(f).replace(/@/, f)); | |
}); | |
const quineCode = quine.toString(); | |
assert.equal(quineCode, quine().toString()); | |
assert.equal(quineCode, quine()().toString()); | |
assert.equal(quineCode, quine()()().toString()); | |
console.log(`const quine = (${quineCode});`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment