我得到一个未定义的 [i]
I get an undefined [i]
在这段代码中,你应该先说出有多少人(数字),然后是他们的名字(在本例中是 Manolo 和 Juan),然后你必须输入“P”或“A”,如果你按 P,这个值得到 +1,所以数组就像这样 => [name, p, a]。尝试使用“for”进行此操作会得到一个未定义 [i] 的错误,我不知道这是怎么发生的
let alumnosNumero = prompt("¿Cuantos hay?");
let alumnosTotal = [];
for (i = 0; i < alumnosNumero; i++) {
alumnosTotal[i] = [prompt("¿Nombre del alumno " + (i + 1) + "?"), 0, 0];
}
// alumnosTotal = [nombre1, nombre2]
// nombre1 = [juan, 0, 0]
// nombre2 = [Manolo, 0, 0]
// almunosTotales = [[Manolo, 0], [Juan, 0]]
const asistencia = (nombre) => {
let asistencia = prompt(nombre + " ha estado --> P = presente | A = ausente");
if (asistencia == "P" || asistencia == "p") {
alumnosTotal[i][1]++;
} else if (asistencia == "A" || asistencia == "a") {
alumnosTotal[i][2];
}
};
for (i = 0; i < 5; i++) {
for (alumnos in alumnosTotal) {
asistencia(alumnosTotal[alumnos][0]);
}
}
document.write(alumnosTotal);
<!DOCTYPE html>
<html lang="es">
<style>
* {
font-size: 70px;
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="js/pruebas.js"></script>
</body>
</html>
我建议你看看JS中的数组表示法,错误是在诸如asistencia(alumnosTotal[alumnos][0].
如果我正确理解了你的问题,应该这样做
let alumnosNumero;
let days;
let alumnosTotal = [];
readStudentAmmount();
readStudentNames();
readTotalDays();
checkIfAttended();
function readStudentAmmount() {
alumnosNumero = prompt("¿How Many students?");
}
function readStudentNames() {
for (i = 0; i < alumnosNumero; i++) {
alumnosTotal[i] = [prompt("Enter Student Name " + (i + 1) + "?"), 0, 0];
}
}
function readTotalDays() {
days = prompt("¿How many days?");
}
function checkIfAttended() {
alumnosTotal.forEach((alumno)=> {
for (let i = 1; i <= days; i++) {
const asistencia = prompt("asistio a clases el dia " + i + "? P = Presente | A = Ausente")
if (asistencia == "p"){
alumno[1]++
} else {
alumno[2]++
}
}
});
console.log(alumnosTotal)
}
<!DOCTYPE html>
<html lang="es">
<style>
* {
font-size: 70px;
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="js/pruebas.js"></script>
</body>
</html>
在这段代码中,你应该先说出有多少人(数字),然后是他们的名字(在本例中是 Manolo 和 Juan),然后你必须输入“P”或“A”,如果你按 P,这个值得到 +1,所以数组就像这样 => [name, p, a]。尝试使用“for”进行此操作会得到一个未定义 [i] 的错误,我不知道这是怎么发生的
let alumnosNumero = prompt("¿Cuantos hay?");
let alumnosTotal = [];
for (i = 0; i < alumnosNumero; i++) {
alumnosTotal[i] = [prompt("¿Nombre del alumno " + (i + 1) + "?"), 0, 0];
}
// alumnosTotal = [nombre1, nombre2]
// nombre1 = [juan, 0, 0]
// nombre2 = [Manolo, 0, 0]
// almunosTotales = [[Manolo, 0], [Juan, 0]]
const asistencia = (nombre) => {
let asistencia = prompt(nombre + " ha estado --> P = presente | A = ausente");
if (asistencia == "P" || asistencia == "p") {
alumnosTotal[i][1]++;
} else if (asistencia == "A" || asistencia == "a") {
alumnosTotal[i][2];
}
};
for (i = 0; i < 5; i++) {
for (alumnos in alumnosTotal) {
asistencia(alumnosTotal[alumnos][0]);
}
}
document.write(alumnosTotal);
<!DOCTYPE html>
<html lang="es">
<style>
* {
font-size: 70px;
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="js/pruebas.js"></script>
</body>
</html>
我建议你看看JS中的数组表示法,错误是在诸如asistencia(alumnosTotal[alumnos][0].
如果我正确理解了你的问题,应该这样做
let alumnosNumero;
let days;
let alumnosTotal = [];
readStudentAmmount();
readStudentNames();
readTotalDays();
checkIfAttended();
function readStudentAmmount() {
alumnosNumero = prompt("¿How Many students?");
}
function readStudentNames() {
for (i = 0; i < alumnosNumero; i++) {
alumnosTotal[i] = [prompt("Enter Student Name " + (i + 1) + "?"), 0, 0];
}
}
function readTotalDays() {
days = prompt("¿How many days?");
}
function checkIfAttended() {
alumnosTotal.forEach((alumno)=> {
for (let i = 1; i <= days; i++) {
const asistencia = prompt("asistio a clases el dia " + i + "? P = Presente | A = Ausente")
if (asistencia == "p"){
alumno[1]++
} else {
alumno[2]++
}
}
});
console.log(alumnosTotal)
}
<!DOCTYPE html>
<html lang="es">
<style>
* {
font-size: 70px;
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="js/pruebas.js"></script>
</body>
</html>