News for hackers. Stuff that matters.
Re:Show HN: A TO-DO app that fits inside a single tweet by rukshn on Monday December 21, 2020 @02:38AM (#25492888)
Op here: you can’t do it with plain js Plus it’s ok to use frameworks according to the tweet
Re:Show HN: A TO-DO app that fits inside a single tweet by shrew on Monday December 21, 2020 @04:20AM (#25493341)
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.

Re:Show HN: A TO-DO app that fits inside a single tweet by chrismorgan on Monday December 21, 2020 @05:29AM (#25493640)
> 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).

Re:Show HN: A TO-DO app that fits inside a single tweet by shrew on Monday December 21, 2020 @06:47AM (#25494024)
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/

Re:Show HN: A TO-DO app that fits inside a single tweet by thybag on Monday December 21, 2020 @07:52AM (#25494450)
<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/

Re:Show HN: A TO-DO app that fits inside a single tweet by thybag on Monday December 21, 2020 @10:21AM (#25495704)
<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.

Re:Show HN: A TO-DO app that fits inside a single tweet by myfonj on Monday December 21, 2020 @01:42PM (#25497954)
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" :] )
Re:Show HN: A TO-DO app that fits inside a single tweet by chrismorgan on Monday December 21, 2020 @12:24PM (#25497059)
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!
Re:Show HN: A TO-DO app that fits inside a single tweet by rukshn on Monday December 21, 2020 @05:04AM (#25493530)
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

Re:Show HN: A TO-DO app that fits inside a single tweet by numlock86 on Monday December 21, 2020 @02:51AM (#25492945)
Care to elaborate? There are some nifty tricks when it comes to golfing JS.
Re:Show HN: A TO-DO app that fits inside a single tweet by rukshn on Monday December 21, 2020 @03:24AM (#25493090)
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

Re:Show HN: A TO-DO app that fits inside a single tweet by tdeck on Monday December 21, 2020 @04:14AM (#25493309)
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.
Re:Show HN: A TO-DO app that fits inside a single tweet by rukshn on Monday December 21, 2020 @05:06AM (#25493539)
Yes I guess that was the right way all along, I saw a comment on this thread using innerHtml and write
Re:Show HN: A TO-DO app that fits inside a single tweet by bamboleo on Monday December 21, 2020 @03:32AM (#25493133)
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()})
Re:Show HN: A TO-DO app that fits inside a single tweet by tdeck on Monday December 21, 2020 @04:03AM (#25493272)
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.
Re:Show HN: A TO-DO app that fits inside a single tweet by rukshn on Monday December 21, 2020 @03:38AM (#25493163)
Exactly these were the challenges I faced when using plain js
Re:Show HN: A TO-DO app that fits inside a single tweet by jacquesm on Monday December 21, 2020 @03:34AM (#25493139)
Then that means it doesn't fit.