can the two functions be combined into an optimized function? give an example code block in javascript showing such a function with comments describing each step