PDA

View Full Version : my program is skipping my function


Lazy8s
01-24-2003, 09:16 PM
I am SO frustrated, I am learning JavaScript for class and our teacher gave us this assignment:
The purpose of this program is to allow the user to input four numbers: test average, program average, final exam grade, and attendance grade. The program then calculates the final average of the student based on the weights each part of the grade counts for this class.

Your program must comply with the following specifications:

Prompt the user for a student name

Prompt the user for a test average

Prompt the user for a program average

Prompt the user for a final exam score

Prompt the user for an attendance grade

Give each of the four values an appropriate weight, as described in the class syllabus, and calculate the average for the student.

Print out the average for the student using a JavaScript alert.

Prompt the user for another student name.

Repeat until the user enters NONE as the student's name. When this happens the program should terminate.

The program must be named average.html. It must contain html code with embedded JavaScript code. It must be a structured program accessible through www.valdosta.edu/~yourUserName/CS1010/average.html. In order to get a 100 on this program, the function of calculating the average must be in a module which is called by the main program.

...okay, so I have it in the right directory and all, that's not the problem. The problem is that my program skips right over my function. (as you can see if you go to my site.) The program does EVERYTHING in the loop it's supposed to and correctly defines all the variables in the loop, but skips the function; like it doesn't exist. I have tried countless online tutorials and just don't understand JavaScript enough to understand the tutorials and therefore I cannot fix it myself. Here's the link: www.valdosta.edu/~wrconkli/CS1010/average.html
and here's my code:

var studentName //the student in question's name
var testAvg //their average for all their tests
var programAvg //their average for programs
var finalExam //their grade on the final exam
//all vars abive here are user defined
var attendanceGrade //their grade for attendance
var weightedTestAvg //the weighted test average
var weightedProgramAvg //the weighted program average
var weightedFinalExam //the weighted final exam
var weightedAttendanceGrade //their weighted attendance grade
var finalGrade //the grade the student will recieve for the course
//all vars above this are program defined
studentName = prompt("Please enter the name of a student or type NONE to exit.","")
//priming read; prompts user to input the student's grade

while (studentName != "NONE")
//loop that prompts user for all of the user defined variables
{
testAvg = prompt("Enter " + studentName + "'s test average.","")
programAvg = prompt("Enter " + studentName + "'s program average.","")
finalExam = prompt("Enter " + studentName + "'s final exam grade.","")
attendanceGrade = prompt("Enter " + studentName + "'s attendance grade.","")
function calculateGrade()
{
weightedTestAvg = testAvg * (1/2)
weightedProgramAvg = programAvg * .25
weightedFinalExam = finalExam * .2
weightedAttendanceGrade = attendanceGrade * .05
finalGrade = weightedTestAvg + weightedProgramAvg + weightedFinalExam + weightedAttendanceGrade
}
alert(studentName + " made a " + finalGrade + " in CS1010.")
alert("weightedTestAvg = " + weightedTestAvg)
studentName = prompt("Please enter the name of a student or type NONE to exit.","")
}


...any help would be GREATLY appreciated, but since I am trying to learn some sort of explination as to why it doesn't work now along with how to fix it and why the fix will make it work would be the best. Thank you in advance.

Lazy8s
01-24-2003, 09:18 PM
just as a side note, I added the alert showing the user the value of weightedTestAvg to see if it was performing any of the calculations inside the function, which is how I figured out that my problem is with the function

Spookster
01-24-2003, 09:51 PM
You have your function defined inside your while loop. You should define it outside your loop. And the reason your function is being skipped over is because at no point did you ever call the function. To call a function you would do so as follows:

//here is where I define the function
function myFunction(){
alert("FooBar");
}

//here is where I call the function
myFunction();

Spookster
01-24-2003, 09:54 PM
And keep in mind that javascript is interpreted so web browsers read the script from top to bottom, left to right so if you use variables inside your function but declare them globally then they must be defined before you define the function that uses them.

Also just a suggestion but a more graceful exit of that program could be to use a confirm box at the end asking the user if he/she wishes to enter another record, if not then utilize the "break" tag to end the loop or use a flag variable.