Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!