Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Print an array results in undefined?
function getGrades()
{
//21 names
var students = new Array("Anderson", "Blackburne", "Brown", "Emrich", "Frost", "Garrison", "Harman", "Hill", "Horky",
"House", "Jones", "Malone", "Menzel", "Price", "Riddle", "Rieck", "Smerling", "Tharp", "Thornton", "Ward", "Wright");
var grades = new Array();
for(var i=0; i <= students.length; i++)
{
grades[i] = prompt("Enter grade for " + students[i]);
}
for(var k=0; k <= students.length; k++)
{
document.write(students[i] + ": " + grades[i] + "</br>");
}
}
1 Answer
- 5 years ago
Arrays in JavaScript have a 0 based index. So in your For/Next loop you need to loop from 0 to the array's length -1. You're also trying to loop in your second For/Next loop using the variable i from your first For/Next loop. To fix your existing code either replace <= to < or change .length to .length - 1 and also change students[i] and grades[i] to students[k] and grades[k].