Last active
June 19, 2023 23:59
-
-
Save jonahsnider/7f0f8dd69f591b8e40b1ee38f0285403 to your computer and use it in GitHub Desktop.
Get the value 4 in Node.js
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
const four = () => process.toString().big().endsWith(String.fromCharCode(18 + ((process.toString().substring(1, 2).charCodeAt(Number.MIN_VALUE)) * 4) - Math.pow(20, 2))).toString().length; | |
console.log(four()); // 4 | |
const assert = require("assert"); | |
const fourButReadable = () => { | |
// String that contains an o as the second character | |
const objectInString = process.toString(); | |
assert.strictEqual(objectInString, "[object process]"); | |
// `objectInString` is randomly used, any string would work here | |
// This adds a `>` to the end of the string | |
const htmlString = objectInString.big(); | |
assert.ok(htmlString.endsWith(">")); | |
// Next we want to generate a value that is equal to `true`, so that when converted to a string it's 4 characters long | |
// This is done by comparing the last digit of `htmlString` and the character `>`, which is assembed with `String#fromCharCode` | |
// The letter o | |
const o = process.toString().substring(1, 2); | |
assert.strictEqual(o, "o"); | |
// The char code of o is 111 | |
// `Number.MIN_VALUE` is rounded to zero. | |
const aHundredAndEleven = o.charCodeAt(Number.MIN_VALUE); | |
assert.strictEqual(aHundredAndEleven, 111); | |
assert.strictEqual(o.charCodeAt(Number.MIN_VALUE), o.charCodeAt(0)); | |
// Multiply by 4 | |
const fourHundredFourtyFour = aHundredAndEleven * 4; | |
assert.strictEqual(fourHundredFourtyFour, 444); | |
// Subtract 400 | |
const fortyFour = fourHundredFourtyFour - Math.pow(20, 2); | |
assert.strictEqual(fortyFour, 44); | |
// Now we add 18 to 44 to get 62, the character code of `>` | |
const greaterThanCharCode = 18 + fortyFour; | |
assert.strictEqual(greaterThanCharCode, 62); | |
// Get `true` value | |
const stringEndsWithGreaterThanSymbol = htmlString.endsWith(String.fromCharCode(greaterThanCharCode)); | |
assert.strictEqual(stringEndsWithGreaterThanSymbol, true); | |
const fourCharacterString = stringEndsWithGreaterThanSymbol.toString(); | |
assert.strictEqual(fourCharacterString.length, 4); | |
return fourCharacterString.length; | |
} | |
console.log(fourButReadable()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
amazing, but how do I use this in a loop? I've been thinking of this but I don't think it's high performance: