Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
How can I create .txt file from parallel JavaScript arrays?

I currently have the following code, although poor, as I am unfamiliar with how to write to .txt files using JS.

Here are my parallel arrays:

let myQuestions = ["Where is New York?", "Where is Paris?"]; 
let myAnswers = ["USA", "France"]; 

I would like the .txt file to look something like:

txt file

I believe that in order to achieve what I desire, I must loop through both arrays using a single for loop, as such:

function addToTxtFile()
{
    for(let i = 0; i<myQuestions.length; i++){
        addToFile(myQuestions[i], myAnswers[i]); 
    }
}

Please note that the addToFile method does not exist, I am simply trying to map out the process.

Edit following answer:

HTML:

<button type = 'button' onclick = 'addToTxtFile()'></button>
const fs = require('fs');
const padString = (str) => str.padEnd(padding);
let content = `${padString('Questions:')} ${padString('Answers:')}\n\n`;


let myQuestions = ["Where is New York?", "Where is Paris?"]; 
let myAnswers = ["USA", "France"]; 

function addToTxtFile()
{
    for (let i = 0; i < myAnswers.length; i++) {
        let line = `${padString(myQuestions[i])} ${padString(myAnswers[i])}\n`;
        content += line;
    }
    
    fs.writeFileSync('out.txt', content, {encoding: 'utf-8'});

}

I am getting the following messages: error message

And:

error message

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try something like this:

let myQuestions = ["Where is New York?", "Where is Paris?"];
let myAnswers = ["USA", "France"];

const fileContent = getFileContent(myQuestions, myAnswers);
writeToFile(fileContent);

function getFileContent(questions, answers) {
    const padding = 30;
    const padString = (str) => str.padEnd(padding);

    let content = `${padString('Questions:')} ${padString('Answers:')}\n\n`;

    // assuming questions.length === answers.length
    for (let i = 0; i < answers.length; i++) {
        let line = `${padString(questions[i])} ${padString(answers[i])}\n`;
        content += line;
    }
    return content;
}

function writeToFile(content) {
    // assuming NodeJS
    const fs = require('fs');
    fs.writeFileSync('out.txt', content, {encoding: 'utf-8'});
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda