I am doing an exercise where I must show in a table the names of the students, subjects and their grades, I don't know if what I have done is correct. What I am trying to do is an array where the names of the students are and another associative two-dimensional array, where the grades and subjects are, wanting to make a map so that the key is student and the value is subjects, and print it in a table
but I get absolutely nothing when I run the program, no console errors or anything. I know I'm doing the formal, but I don't know how to do it so that it collects everything.
var map = new Map();
var alumnos = ["Juan", "Pepe", "Alba", "Maria", "Alvaro", "Roberto", "Laura", "Daniel", "Cristina", "David"];
var asignaturas = new Array();
asignaturas[0] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[1] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[2] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[3] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[4] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[5] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[6] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[7] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[8] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
asignaturas[9] = new Array({ "Lenguaje": "4", "Matematicas": "7", "Musica": "8", "Ingles": "9", "Fisica": "5" });
map.set(alumnos, asignaturas);
document.write("<table bordes=1>");
for (i = 0; i < map; i++) {
document.write("<tr>");
document.write("<td>i</td>");
document.write("</tr>");
}
document.write("</table>");
Your code has many problems, I will show you each of them.
First,
map.set(alumnos, asignaturas)
you are putting in the map object a single array with all the objects inside. You have to put all the objects in the map object. In this wayNext, you want to print the information with
document.write()
, which I don't recommend. I would say create a function to create DOM elements and put the information in there. You pass the type of label and the parent element as a parameter so that it is placed.Another is that the for loop is poorly put together. You have to get the length with the size method. Also, with
map.get(i)
we get each record of the map. And we pass the record in the DOM element with$box.textContent = map.get(i)
You wanted to print the records in a table. Remove the table tag and replace it with a div, as it was missing a lot of structure. Each record is a span tag.
In general, this code would remain
Maybe this is what you are looking for:
You can then implement the array as needed.
I hope it helps you, greetings.