Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
collecting data from a text file and parsing the collected data to create an array of objects

I need some help So basically I have to collect some data from a text file placed on GitHub (some data about some students that looks like this

john
New York
123456456
Math
Ben
California
3265455554
Philosophy
..... ..... .....

there are in total 5 students), and I have to parse the file in to the web page like this using either a class or object

Name: John
Address: New York
Phone: 123456456
Course: Math

Name: Ben
Address: California
Phone: 3265455554
Course: Philosophy

Name: ....
Address: ....
Phone:.....
Course: ....

and so on.

The idea is to use async /await functions in my assignment this is what my teacher told me.

var button = document.getElementById("btn");
var loader = document.getElementById("loading");
var displayArea = document.getElementById("data");


button.addEventListener("click", function () {


  getData();



})

class Student {

  constructor(fullName, address, phone, course) {

    this.fullName = fullName;
    this.address = address;
    this.phone = phone;
    this.course = course;

  }

  getInfo() {
    return "Full Name: " + this.fullName + "\n" +
      "Address: " + this.address + "\n" +
      "Telephone: " + this.phone + "\n" +
      "Course: " + this.course;
  }
}

var studentName = new Student().fullName;
var studentAddress = new Student().address;
var studentPhone = new Student().phone;
var studentCourse = new Student().course;
var studentInfo = new Student().getInfo();

var studentData = [studentName, studentAddress, studentPhone, studentCourse]


async function getData() {

  loader.style.display = "inline-block";


  try {

    let response = await fetch(`https://v-dresevic.github.io/Advanced-JavaScript-Programming/data/students.txt`);


    if (response.status !== 200) {

      throw new Error("Error while reading file.");

    }

    let text = await response.text();



    var res = text.split([3], "\n");

    for (let i = 0; i <= res.length; i++) {


    }

    displayArea.innerHTML = res


  } catch (err) {

    displayArea.innerHTML = "Unknown Error " + err;

  } finally {

    loader.style.display = "none";
  }


}
 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

async function getData(){
  
  let response = await fetch(`https://v-dresevic.github.io/Advanced-JavaScript-Programming/data/students.txt`);

    if (response.status !== 200) {
      throw new Error("Error while reading file.");
    }

    let text = await response.text();
    let arr = text.split("\n");
    let result = arr.reduce((resultArray, item, index) => { 
      const chunkIndex = Math.floor(index/4);
      if(!resultArray[chunkIndex]) {
        resultArray[chunkIndex] = []
      }
      resultArray[chunkIndex].push(item.replace(/(.+)(\r)$/, '$1'));
      return resultArray
    }, []).map(d=>{
      return {Name: d[0], Address: d[1], Phone: d[2], Course: d[3]};
    });  

    return result;
}

async function func(){

let result = await getData();
console.log(result);

}

func();

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda