Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda