doing things that make me feel gross

(it's

let i = 0;
for (; i<matches[0].length; i++) {
const idx = i + currentSignal.length;
if (matches[0][idx] !== matches[matches.length-1][idx]) break;
}

)

@monorail I think a while loop would be better but I'm not sure I would give enough of a shit in a code review because either way would look Weird

@wallhackio ??? Iterating i over a range of numbers is a very typical use for a for loop, why are you saying that a while loop would be better?!

@alas @wallhackio Oh. Something like this?

[removed because it's full of mistakes and I'm ashamed]

This does make sense from a control flow perspective, sorry for not seeing it immediately

Follow

@vaporeon_ @alas oh I wouldn't have thought to do it with a do while, this is neat

my evil code golfing brain wants the i++ snuck into the previous line but that kind of thing tends to get you in trouble

by the way you also need i<matches[0].length in the conditional

@wallhackio @alas Augh. My brain really is not working today. :blobcat_notlikethis: And see, this is why I would prefer a for-loop here. Iterate over all i in a particular range and break early if some condition happens. Common pattern and less likely for me to make stupid mistakes about ranges or increments due to being sleepy.

Really want to know how you would've written it with a while loop that's less hideous and ugly and wrong than what I've done.

@wallhackio @vaporeon_ @alas what it actually needs for my use case that i only realized later is to break on first === undefined || last === undefined || first !=== last

it's a "longest common prefix" algorithm

@monorail @wallhackio @alas So that it breaks when either of the arrays end, even if last is shorter than first? Is that the reason for checking the undefined? Can you also do that with

for (; i<matches[0].length&&i<matches[matches.length-1].length; i++)

Or am I making a stupid mistake again?

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.