How can I golf this JavaScript code, if at all?

5

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>

Niall Ward

Posted 2019-11-02T09:13:16.307

Reputation: 59

Is these the complete HTML or just a part of it? – tsh – 2019-11-05T03:43:06.780

Answers

8

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

primo

Posted 2019-11-02T09:13:16.307

Reputation: 30 891

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 #. – Ismael Miguel – 2019-11-08T10:20:16.577