So, i split text from api and then i need to go loop array. I need to make the first 4 elements are properties of the first object, the second 4 elements are properties of the second object, and so on..
It is necessary to place all created objects within one array. In the end, it is necessary to go through such a series of student objects and call the getInfo() method for each one to print the data. There is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student</title>
</head>
<body>
<textarea name="text-area" id="text-area" cols="70" rows="40"></textarea>
<button id="btn_dugme">Klikni</button>
<script>
let text_area = document.getElementById("text-area")
let btn_dugme = document.getElementById("btn_dugme")
class Student {
constructor(name, adress, phone, course) {
this.name = name
this.adress = adress
this.phone = phone
this.course = course
}
getInfo() {
return `Name: ${this.name} \nAdress: ${this.adress}\nPhone: ${this.phone}\nCourse: ${this.course}`
}
}
btn_dugme.addEventListener("click", () => {
getData()
})
async function getData() {
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 array = []
var obj = {}
let lines = text.split('\n')
for(let i = 0; i < lines.length; i++){
var a = lines[i];
var b = lines[i+1];
var c = lines[i+2];
var d = lines[i+3];
</script>
</body>
</html>
Need to be like this: https://www.link-elearning.com/linkdl/coursefiles/1473/AJS0-Assignment.png