5
\$\begingroup\$

I have some JavaScript code in a <script> HTML tag I'd like to golf, while still remaining in the tag. How can I golf that code, if at all? Here's the code:

<script>document.write`<font color=#40e0d0 face=arial>YOU ARE IN THE BEDROOM.<br>YOU SEE CRAB NICHOLSON.<br>HE HAS NO MITTENS.<br>HIS CLAWS ARE MENACING.<br>OBVIOUS EXITS ARE:<br>BATHROOM, KITCHEN, LIVING ROOM<br>AND CRAB NICHOLSON'S<br>ULTIMATE LAIR OF AWESOMENESS</font>`;document.body.style.background="#000"</script>
\$\endgroup\$
  • \$\begingroup\$ Is these the complete HTML or just a part of it? \$\endgroup\$ – tsh Nov 5 '19 at 3:43
8
\$\begingroup\$

Save 3 bytes by assigning document to a variable:

(d=document).write`<font color=#40e0d0 face=arial>YOU ARE IN THE BEDROOM.<br>YOU SEE CRAB NICHOLSON.<br>HE HAS NO MITTENS.<br>HIS CLAWS ARE MENACING.<br>OBVIOUS EXITS ARE:<br>BATHROOM, KITCHEN, LIVING ROOM<br>AND CRAB NICHOLSON'S<br>ULTIMATE LAIR OF AWESOMENESS</font>`;d.body.style.background="#000"

Save 2 bytes by reusing ' CRAB NICHOLSON':

(d=document).write(`<font color=#40e0d0 face=arial>YOU ARE IN THE BEDROOM.<br>YOU SEE${c=' CRAB NICHOLSON'}.<br>HE HAS NO MITTENS.<br>HIS CLAWS ARE MENACING.<br>OBVIOUS EXITS ARE:<br>BATHROOM, KITCHEN, LIVING ROOM<br>AND${c}'S<br>ULTIMATE LAIR OF AWESOMENESS</font>`);d.body.style.background="#000"

Save 1 byte by using .join`<br>`:

(d=document).write(`<font color=#40e0d0 face=arial>YOU ARE IN THE BEDROOM._YOU SEE${c=' CRAB NICHOLSON'}._HE HAS NO MITTENS._HIS CLAWS ARE MENACING._OBVIOUS EXITS ARE:_BATHROOM, KITCHEN, LIVING ROOM_AND${c}'S_ULTIMATE LAIR OF AWESOMENESS</font>`.split`_`.join`<br>`);d.body.style.background="#000"

Further golfs suggested by @Night2:

Save 14 bytes by using .bgColor=0:

(d=document).write(`<font color=#40e0d0 face=arial>YOU ARE IN THE BEDROOM._YOU SEE${c=' CRAB NICHOLSON'}._HE HAS NO MITTENS._HIS CLAWS ARE MENACING._OBVIOUS EXITS ARE:_BATHROOM, KITCHEN, LIVING ROOM_AND${c}'S_ULTIMATE LAIR OF AWESOMENESS</font>`.split`_`.join`<br>`);d.body.bgColor=0

Save 7 bytes by 'forgetting' to close the <font> tag (YMMV):

(d=document).write(`<font color=#40e0d0 face=arial>YOU ARE IN THE BEDROOM._YOU SEE${c=' CRAB NICHOLSON'}._HE HAS NO MITTENS._HIS CLAWS ARE MENACING._OBVIOUS EXITS ARE:_BATHROOM, KITCHEN, LIVING ROOM_AND${c}'S_ULTIMATE LAIR OF AWESOMENESS`.split`_`.join`<br>`);d.body.bgColor=0
\$\endgroup\$
  • \$\begingroup\$ You can save 1 byte by removing the # in the color. Google Chrome parses as the color rgb(64, 224, 208), with or without the #. \$\endgroup\$ – Ismael Miguel Nov 8 '19 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.