1 Replies - 754 Views - Last Post: 11 December 2009 - 01:48 PM

#1 jennii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-November 09

Array value returns undefined

Posted 11 December 2009 - 12:44 PM

I have a homework assignment which is pretty simple. It is a grade calculator that uses arrays to gather the grades from the user. This is what I have for the problematic section:

var homework = new Array(2);	/* The grades entered by the user for Homework 1, 2, and 3 */
var project = new Array (3);	/* The grades entered by the user for Project 1, 2, 3, and 4 */
var exam = new Array (2);		/* The grades entered by the user for Exam 1, 2, and 3 */				
				
/* Read in and convert the grades for the homeworks */
for (i = 1; i < 4; i++)
{
   homework[i] = parseInt (prompt ("Enter your grade for Homework # " + i + ": "));
}

/* Read in and convert the grades for the projects */
for (i = 1; i < 5; i++)
{
   project[i] = parseInt (prompt ("Enter your grade for Project # " + i + ": "));
}

/* Read in and convert the grades for the exams */
for (i = 1; i <4; i++)
{
   exam[i] = parseInt (prompt ("Enter your grade for Exam # " + i + ": "));
}

/* Display what the user entered for visual verification */
document.write ("<h2>Grade Values Entered</h2>");
document.write ("<h3>Homeworks</h3>");
document.write ("<p>Homework 1: " + homework[0] + "%<br />Homework 2: " + homework[1] + "%<br />Homework 3: " + homework[2] + "%</p>");
document.write ("<h3>Projects</h3>");
document.write ("<p>Project 1: " + project[0] + "%<br />Project 2: " + project[1] + "%<br />Project 3: " + project[2] + "%<br />Project 4: " + project[3] + "%</p>");
document.write ("<h3>Exams</h3>");
document.write ("<p>Exam 1: " + exam[0] + "%<br />Exam 2: " + exam[1] + "%<br />Exam 3: " + exam[2] + "%</p><br />");



My problem is the document.write portion of the code, where it is supposed to return either homework[0], project [0], or exam[0], it instead returns undefined. There is no problem with any other subscript. And yes, it does successfully prompt me for each of the [0] subscripts in the first part of the code. Am I just missing something that is right in front of my face? Does all arrays not start with the [0] subscript?

Is This A Good Question/Topic? 0
  • +

Replies To: Array value returns undefined

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 828
  • Joined: 25-October 08

Re: Array value returns undefined

Posted 11 December 2009 - 01:48 PM

You had a number of problems, but it is easier to show you than it is to explain.
Compare the following line-by-line to your original post.
Post back if you need specific answers.

Main problem is that array do start at element [0], but you were counting from [1] in your for loop. Note changes to allow a different number of entries just by changing the array initialization value (3) or (4).
<html>
<head>
<title>Gradebook</title>
</head>
<body>
<script type="text/javascript">
// From: http://www.dreamincode.net/forums/showtopic17947.htm

var homework = new Array(3);	/* The grades entered by the user for Homework 1, 2, and 3 */
var project = new Array (4);	/* The grades entered by the user for Project 1, 2, 3, and 4 */
var exam = new Array (3);		/* The grades entered by the user for Exam 1, 2, and 3 */				
			   
/* Read in and convert the grades for the homeworks */
for (i = 0; i < homework.length; i++) {
   homework[i] = parseInt (prompt ("Enter your grade for Homework # " + (i+1) + ": "));
}

/* Read in and convert the grades for the projects */
for (i = 0; i < project.length; i++) {
   project[i] = parseInt (prompt ("Enter your grade for Project # " + (i+1) + ": "));
}

/* Read in and convert the grades for the exams */
for (i = 0; i <exam.length; i++) {
   exam[i] = parseInt (prompt ("Enter your grade for Exam # " + (i+1) + ": "));
}

/* Display what the user entered for visual verification */
document.write ("<h2>Grade Values Entered</h2>");
document.write ("<h3>Homeworks</h3>");
document.write ("<p>Homework 1: " + homework[0] + "%<br />Homework 2: " + homework[1] + "%<br />Homework 3: " + homework[2] + "%</p>");
document.write ("<h3>Projects</h3>");
document.write ("<p>Project 1: " + project[0] + "%<br />Project 2: " + project[1] + "%<br />Project 3: " + project[2] + "%<br />Project 4: " + project[3] + "%</p>");
document.write ("<h3>Exams</h3>");
document.write ("<p>Exam 1: " + exam[0] + "%<br />Exam 2: " + exam[1] + "%<br />Exam 3: " + exam[2] + "%</p><br />");
</script>

</body>
</html>


Next step would be to use the arrays to calculate an average for the homework, projexts and exams...
Good Luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1