Necesito ayuda Básicamente, tengo que recopilar algunos datos de un archivo de texto colocado en GitHub (algunos datos sobre algunos estudiantes que se ven así
Juan
Nueva York
123456456
Matemáticas
ben
California
3265455554
Filosofía
..... ..... .....
hay un total de 5 estudiantes), y tengo que analizar el archivo en la página web de esta manera usando una clase u objeto
Nombre: Juan
Dirección: Nueva York
Teléfono: 123456456
Curso: Matemáticas
Nombre: Ben
Dirección: California
Teléfono: 3265455554
Curso: Filosofía
Nombre: ....
Dirección: ....
Teléfono:.....
Curso: ....
y así.
La idea es usar funciones async/await en mi tarea, esto es lo que me dijo mi maestro.
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"; } } 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();