Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
How can I correct this Javascript for-Loop and array program?

I've got some kind of mental block while developing the code for this particular program.

Instructions to create a flexible marks storing database: Ask the user how many students there are. Using a for loop and prompt statements, ask the user for the student names. Store them in an array. Using a for loop and prompt statements, ask the user for the marks (include the name of the class) for each student. Store them in an array. Calculate the average mark for the class, and print it out on the screen.

It looks like got somewhere with the code but I've having difficulty combing the second for-loop to calculate the average of the marks of the number of students. I believe the error results because I didn't declare all the necessary variables and also my array structure is wrong; like the way I declared and stored the elements.I think a few steps are missing from the code.

var howManyStudents = 0;
var studentGradesAverage = 0;
var marksTotal = 0;
howManyStudents = parseInt(prompt("How any students are in this class?", "enter a number"));

    document.writeln("<p> There are " + howManyStudents + " students in this class </p>");

    var studentNames = Array(howManyStudents);
    var classGrades = Array(howManyStudents);
    var allStudents = Array(studentNames, classGrades);
    for (var i = 0; i < howManyStudents; i++) { 
        allStudents[0][i] = prompt("Please enter the name of the student.", "Student Name").toString();

         // reference a student Math grade
         allStudents[1][i] = parseFloat(prompt("Enter the student's Grade. The Grade are between 0 to 100. "));
     
         marksTotal += allStudents[1][i];
    }
    
    studentGradesAverage = marksTotal/howManyStudents; 
document.writeln("<p> Average per class is: " + studentGradesAverage + "</p>");

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda