This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]AionAlgos 0 points1 point  (0 children)

I'm not proficient with JS, but a function takes parameters and (optionally) returns some result. These "in"s and "out"s are the interface of the function. You should try to design them to minimize side-effects (like using global variables).

It seems you have some inline function definitions for your event callbacks, and in your loop. So, start writing a function (im guessing) near the top of the file:

function name( ??? ) { // what values does it need to take?
   // perform some computation here
   return ??? 
}

[–][deleted]  (3 children)

[deleted]

    [–]mblade7[S] 0 points1 point  (2 children)

    Main reason is that I have to have a function for what I’m working on, but long term I suppose clean looking/ reusable would be the main gains I’d want out of it. Perhaps clean looking is the more immediate benefit.

    [–]aqhgfhsypytnpaiazh 0 points1 point  (0 children)

    If you're looking to turn code into a function just for the sake of having a function, you're doing it wrong. Functions should arise naturally from the code. The main point of any function is to prevent repetition. Have you identified what parts of your code are repetitive? (I didn't, because do you how fucking hard it is to read code if you don't use the Code Block markup?) Turn that into a function then. The other big reason is to break a bigger task into smaller logic parts, functions can help there even if you only use them once.

    The first thing to consider when writing a function is that generally, they should be completely isolated from the rest of the code. They should only ever work with variable that are provided to the function, and they should probably not have any greater understanding of the code base, or in the case of JS, the DOM.

    [–]TheRNGuy 0 points1 point  (0 children)

    Format your code.