Hacker News new | past | comments | ask | show | jobs | submit login

I am not very familiar with JS but doesn't loading a framework externally kind of defeat the purpose?



I wouldn't say so, since it's a general purpose framework. In most languages you use a framework to build GUI applications. Working with the DOM in pure JS is pretty verbose.


Op here: you can’t do it with plain js Plus it’s ok to use frameworks according to the tweet


I don't mean to make this into a code golf thread, but saying it "can't" be done in plain JS was too enticing not to try it. Does this meet the requirements?

<body><script>let win=window;document.body.innerHTML=`<input id="a"/><b id="b"></b><ul id="c"></ul>`,win.b.onclick=a=>{let b=win.c.appendChild(document.createElement("p"));b.innerText=win.a.value,win.a.value="",b.onclick=()=>win.c.removeChild(b)};</script>

JS Fiddle: https://jsfiddle.net/2z0bus8d/

While I think there's always the standard caveat around code golf in JS (you're building on a vast set of APIs and an often resource hungry platform), reaching for a framework really doesn't feel in the spirit of this kind of puzzle.


> I don't mean to make this into a code golf thread

Too late. Golfing notes:

• Your `win` variable is unnecessary: window is approximately the global object (look into globalThis for the subtleties of how window isn’t quite the global object), so you can drop your “win.” every time, e.g. win.c.removeChild(b) can be just c.removeChild(b).

• parent.appendChild(child) → parent.append(child) and parent.removeChild(child) → child.remove(), using newer, shorter DOM methods.

• Skip quotes on attribute values, and trailing slashes are 100% superfluous in HTML (they’re explicitly ignored, as an XHTML compatibility mechanism; I reckon they’re actually a slightly bad thing rather than harmless).


And this is why code golf is fun regardless of language!

The first two are new to me, so thank you, I'll read up. The third succinctly explains a quirk I've never bothered to check the legitimacy of, so thank you again.

With your changes, that brings us way below the character limit, sitting at 208 characters with the <body> and <script> tags the challenge dictates.

I had to rejig things to use parent.append(child) as it doesn't return the element, but using the child.remove() method directly means there's no parameter and therefore one less character there, so it all balances out. I also had to rename the event variable as it caused a scoping conflict for the variable `a`.

With all that space left over you could certainly add in the clear all and strike-through behaviour properly. Maybe even some real state!

Updated fiddle: https://jsfiddle.net/2z0bus8d/1/


<script>x=document;v='innerHTML';o='onclick';x.body[v]="<input id=a><b id=b>X</b><p id=d>clear<ul id=c>",b[o]=e=>{let b=x.createElement("li");c.append(b);b[v]=a.value,a.value="",b[o]=()=>b.classList.toggle('y');d[o]=()=>c[v]=''};</script><style>.y{text-decoration:line-through}

Combining yours & the strike thru example, managed to get both strike thru & clear added in 277 chars. If i could save another char could close the p which would be nice :p

https://jsfiddle.net/hysjgkcm/

Down to 270 https://jsfiddle.net/vr7246jk/1/


<body><script>x=document;x.write`<input id=a><b id=b></b><p id=d>🆑<ul id=c>`,b[o='onclick']=b=>{b=x.createElement`li`;c.append(b);b.v=b[v='innerHTML']=a.value,a.value="",b[o]=e=>b[v]=(b[v]==b.v?'<strike>':'')+b.v;d[o]=e=>c[v]=''}</script>

https://jsfiddle.net/eqym2ac0/

Working with shrew. 240 char with script/html or 217 with as JS.


No JS-in-HTML golfing is complete until you cram your script into onload attribute:

    --<body><script>…</script>
    ++<body onload="…">
(But again, would not fulfill the requirements of "sole script in body" :] )


Ah yes, I’d completely forgotten that appendChild returns the child element and append doesn’t, because it’s so long since I actually had cause to use the appendChild return value!


This has to be the best solution I have seen, I feel it ticks all boxes and better yet it doesn’t use any external libraries like I did

I found it difficult manipulate DOM with just js without hitting the limit, well done sir, you deserve a cookie


Care to elaborate? There are some nifty tricks when it comes to golfing JS.


Well sorry I was in a hurry when I typed the above comment

Well the body can’t have any dom elements according to the challenger

That means I had to create the dom elements as well and I was unable to figure out a way within the given character limit


I wonder if document.write() would work for you. The old-fashioned APIs from before the DOM era (e.g innerHTML, innerText) generally seem to be a lot shorter although they're still a bit verbose.


Yes I guess that was the right way all along, I saw a comment on this thread using innerHtml and write


JS is ok, it’s the DOM APIs that are verbose. I mean this is just to handle the enter key:

    f.addEventListener('keypress',e=>{if(e.keyCode==000) etc()})


Just a fun note: You can replace this

    f.addEventListener('keypress',e=>{...})
With the older but essentially equivalent

    f.onkeypress=e=>{...}
and save 18 characters. You can even use `onkeyup` and save 3 more characters.


Exactly these were the challenges I faced when using plain js


Then that means it doesn't fit.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: