How do I move al my code under one array in Javascript?

I have two scripts in JavaScript that allows my to write data to a text file. I want to move all of my code into one function called myFunction. I want to do this with just code and not copy and paste because I don’t have access to the source code.

My code for both programs are:

`const fs = require(‘fs’)

      
    // Data which will write in a file.

    let data = “Learning how to write in a file.”

      
    // Write data in ‘Output.txt’ .

    fs.writeFile(‘Output.txt’, data, (err) => {

          
        // In case of a error throw err.

        if (err) throw err;`

Can someone help me? I want to put this in a function by code and not copy and paste or manually doing it.