Created
February 25, 2020 01:27
-
-
Save alacret/bd518dae45cc3b66e7803f9798cc31d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 S = { | |
TAB : "\t", | |
SPACE : " ", | |
BREAK : "\n", | |
} | |
// S.TAB = "[tab]"; | |
// S.SPACE = "[space]"; | |
// S.BREAK = "[LF]"; | |
function whitespaceNumber(n) { | |
if (n === 0) | |
return " \n"; | |
const result = []; | |
if (n > 0) | |
result.push(S.SPACE); | |
else | |
result.push(S.TAB); | |
const absNumber = Math.abs(n); | |
const binaryNumber = absNumber.toString(2); | |
binaryNumber.split('').forEach(n => { | |
if (n === '0') result.push(S.SPACE) | |
else result.push(S.TAB) | |
}) | |
result.push(S.BREAK); | |
return result.join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment