Returning longest word in list of words

I’m currently making a block which will return the word which has the longest length from a list of words.

I’m currently at the point where the block will work in some instances for example if the first word is longer than 2nd and so on. But if the first word is shorther than the 2nd it will not go through the whole list and just stop at the first instance of the condition being met.
I have included a picture of my current block. I’m sure I’m just being stupid but all help is welcome and I’d like to thank you all in advance.

item(1) of (sort x by <[]>[]>)

The biggest misunderstanding here is that you're trying to REPORT after each item of the list, but a reporter can report only once; the first time you try to run that REPORT will end the procedure.

Once you have that under control, your algorithm can work.

That would be the advanced solution, just to show the power of Snap! (welcome, abuzars :slight_smile: )

untitled script pic (30)

(the SORT block is from the "List utilities" library, same where SENTENCE → LIST is from).

This could be an assignment so maybe don't give full solution :slight_smile:

9 times out of 10, a first time poster asking for something like this would be doing it as an assignment so I always err on the side of caution :slight_smile:


this is my updated solution which works but confuses me. Shouldn't it return a list?

Thanks for the response and warm welcome :smile:. I did get it to work with a different solution to this. But tbh I’m not quite sure why my working solution even returns the correct result.

I’ll add another post with a picture of my working solution if you’re interested (which I’m sure you aren’t lol)

You say SET UPDATED LIST TO ITEM. ITEM isn't a list, so neither is UPDATED LIST.

By the way, instead of taking a screenshot of the Block Editor, you should right-click on the hat block, the topmost block in the Block Editor, and choose "Script pic." Then when you post that picture, we can drag it into a Snap! window and get your actual code to play with.

Try applying your block to the sentence "the longest word".

oh yeah :joy: :joy: Back to the drawing board i goooo


think i got it this time. Thanks again for all the help everyone. Let me know if i've still got anything wrong :joy:

The only little thing is that if MAXLENGTH is supposed to be a number, and you want to initialize it, you should set it to 0 rather than to an empty string. It doesn't affect the result, because Snap! treats strings of digits as equivalent to numbers. But actually you don't have to initialize them at all, I think, since you get 0 as a default value for new variables.

You actually click the right arrow on the script vars block to add more script variables without using another block.

this is o(nlogn) to o(n2)(i think modern js engines all use quicksort?)
its just one logn more than the best solution
if it doesnt lag we can use it


why des this not return anything? am i using the contains predicate wrong?

Well - at a minimum, it's missing a report block

because it doesn't return() report anything

add [scratchblocks] report (output) :: control cap [/scratchblocks] to the very end of the definition. (report block can be found in control)

Loool oh yeah :joy::joy:. Note to self, This is why you shouldn’t try working on 4 hours sleep

you need a counter for the item number, as in where the current item is in the sent list, not what the contents of it are.

yeah I figured it out as soon as I posted lol. Thanks for the response tho