Click to See Complete Forum and Search --> : Learning to use functions


Acrappa
03-02-2003, 01:50 AM
I am fairly new to JavaScript and have enrolled in an online class. For my fourth project, I created a JavaScript that calculates the user's grade point average.

- http://members.cox.net/acrappa/javascript/project4.html

For this week's project, my instructor has challenged the class to accomplish the same task but with using a function. The function is used to convert the letter grade to a number (A=4, B=3, etc.). I haven't finished this yet, but here's my work in progress:

- http://members.cox.net/acrappa/javascript/project5prog.html

Something is causing an error in my function. I know that I've properly called it, but something is not right. For every class that is entered, the default in the 'switch' reports the 'default' error. That is, instead of converting three grades to three numbers, the default message displays three times telling the user to use proper formatting.

I'm not sure if my problem is the way I'm using a 'switch' in the function or if something is setup wrong.

I don't want and am not encouraging a flat-out answer for this. I'm hoping that someone here who is experienced with JavaScript can point me out to what I'm doing wrong in which portion of code. Hopefully I can figure it out myself, but right now I'm a bit stuck.

Help me, journeymen!

- Matt

Dan Drillich
03-02-2003, 07:26 AM
Please change the function to -


function gradeToNumber(letterGrade) {

var numGrade = 0;

switch (letterGrade) {
case "A":
numGrade = 4;
break;
case "B":
numGrade = 3;
break;
case "C":
numGrade = 2;
break;
case "D":
numGrade = 1;
break;
case "F":
numGrade = 0;
break;
default:
alert("Please enter in valid grade format (omit + and - and also capitalize your letter grade). Proper format, for example, would be \"A\", \"B\", \"C\", \"D\", or \"F\".");
break;
}
return numGrade;
}

Acrappa
03-02-2003, 02:14 PM
Thank you very much; that worked beautifully.

My 'switch' statement must have been tripping over my repeated use of 'letterGrade[inLoopCounter]'. I understood how the function worked before, but now I understand that you can use a different variable in it; using 'letterGrade[inLoopCounter]' was just messing up the code. It looked sloppy, too, so adding the 'numberGrade' variable made things much cleaner and actually worked. And then using 'return' to send the calculated result back to the array made things all dandy.

You rock. Thanks for your help.

Acrappa
03-02-2003, 03:21 PM
If anyone wants to see the final product, it's here:

- http://members.cox.net/acrappa/javascript/project5.html

Charles
03-03-2003, 05:20 AM
You could really simplify that function by using an associative array instead of that switch statement.

<script type="text/javascript">
<!--
function gradeToNumber(letterGrade) {return {A:4, B:3, C:2, D:1, F:0} [letterGrade]}

num = gradeToNumber('e');
if (!num) alert('Please enter in valid grade format (omit + and - and also capitalize your letter grade). Proper format, for example, would be "A", "B", "C", "D", or "F".');
// -->
</script>