I've been recently dealing with the problem of integers literals being too readable and efficient and I mean look at this literal: 6
, it's too simple!
For this task we'll unoptimize a function called itoi
wich stands for "integer to integer", it'l take as input an integer and will output the same value in strage and bizarre ways in order to largely decrease efficiency, one liners are encouraged.
In code's playful realm, we weave a disguise,
With cryptic constructs that confound the wise.
Ternaries and tricks, a maze to explore,
A challenge to fathom, and then some more.
JavaScript joins, a quirky domain,
Its strengths and its quirks, a unique refrain.
Yet coding's the art where all minds unite,
In clever endeavors, we find our delight.
WARNING!
test cases will be small to allow for far worse efficiency!
const integer_dark_magic = (integer, index) => !(index == false)? integer_dark_magic(integer + (((index > 0)? 1 : 0)* 2 - 1), index - (((index > 0)? 1 : 0)* 2 - 1) * (false ? [].length : integer_dark_magic(1, false))) : integer;
//termination condition chose wether subtract or increment the integer, the opposite of that but with the index
const itoi = (integer) => integer_dark_magic(0, integer);
const chai = require("chai");
const assert = chai.assert;
describe("Integer to integer conversion", function() {
it("should convert integers to the same value", function() {
assert.strictEqual(itoi(-7), -7);
assert.strictEqual(itoi(-1), -1);
assert.strictEqual(itoi(0), 0); //handle edge cases, preferably with ternary
assert.strictEqual(itoi(1), 1);
assert.strictEqual(itoi(6), 6);
});
});