...

View Full Version : Posting new Array values based on switch cases



sententia
07-17-2009, 12:33 AM
I was tasked with creating a CPA calculator in JavaScript.

I am currently struggling with the part where I will convert the letter grades to Number (a=4 b=3 etc...)

I have a switch case function that is working when I enter the spec. letter value, but it is not working when I try to use the array that I created.

I would be grateful if someone could take a look at this and let me know how I can get the total of the grade column to show up at the bottom after all the prompts have been run through and the letter grades have been converted to numbers.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0.1//EN">
<html>
<head>
<title>GPA CALCULATOR</title>
<link>
<style type="text/css">
</style>
<script language="JavaScript">
do { //beginning of do/while loop to make sure user enters their name
var userName = prompt("Please enter your name:", "")
}

while (!userName) //end of do/while loop to check if ans is blank

<!--
var courseCtr;
var courseAnswer;
var numCourses;

// set up one dimensional array
var courses = new Array();
courseCtr = 0;

var creditHours;
creditHours = 0;

do
{
// start columns in second dimension of the array
courses[courseCtr] = new Array();

// get values from user and put in array
courses[courseCtr][0] = prompt ("Please enter the name of the course.");
courses[courseCtr][1] = prompt ("Please enter the grade you earned.");
courses[courseCtr][2] = prompt ("Please enter the credit hours you earned.");

// accumulate the value of the letter grades




// accumulate the hours of credit earned
creditHours = creditHours + parseFloat(courses[courseCtr][2]);

// accumulate the quality points earned
//gradeValues = gradeValues + parseFloat(courses[courseCtr][1]);




// add one to the total number of courses
courseCtr++;
courseAnswer = confirm ("Would You like to add another course?");
} while (courseAnswer == true);

</script>




</head>

<body align="center">
<h2 align="center">
<script language="JavaScript" type="text/javascript">
document.write(userName +"'s Grade Point Average")
</script>
</h2>

<script language="JavaScript">

// set variable that is used as counter
numCourses = 0;

// print out header for content
document.write("<table bgcolor='yellow' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<tr>");
document.write("<th>Course Name</th><th align='center'>Grade</th><th align='center'>Credit Hours</th>");
document.write("</tr>");

// Loop through array displaying html and javascript values in the array
while (numCourses < courseCtr)
{
document.write("<tr>");
document.write("<td>");
document.write (courses[numCourses][0]);
document.write("</td>");
document.write("<td align='center'>");
document.write (courses[numCourses][1]);
document.write("</td>");
document.write("<td align='center'>");
document.write (courses[numCourses][2]);
document.write("</td>");
document.write("</tr>");

// increment the counter
numCourses++;
}



// finish the table of data and display the total value
document.write("</Table>");
document.write("<br>");
document.write("<table bgcolor='yellow' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<tr>");
document.write("<td>Total value</td><td align='center'>" + creditHours + "</td>");
document.write("</tr>");
document.write("</Table>");


function convertGrades(numeral)
{
//use numeral sent to function in translation to number
var numbervalue = 0;
switch (numeral)
{
case "A":
numbervalue = 4;
break;
case "B":
numbervalue = 3;
break;
case "C":
numbervalue = 2;
break;
case "D":
numbervalue = 1;
break;
case "F":
numbervalue = 0;
break
default:
alert("Pleaes enter a valid grade value (A-F)")
}
return numbervalue;
}

var testvalue = "B";
var actnumber = 0;

//call function with values it needs to convert into a number

actnumber = convertGrades(testvalue);


//-->
</script>

<script language="JavaScript">
document.write(creditHours);
</script>

<br><br>

<script language="JavaScript">

// use the returned value
document.write(actnumber);



</script>


</body>
</html>

tomws
07-17-2009, 02:58 AM
The convertGrades function does work for the test value you've given it. Now you just need to modify your code to loop through the array you built from user input and add up as you go along.

sententia
07-17-2009, 05:46 AM
ummm... really a newb with JS, from what i take from your answer I should put the function inside the loop so that it converts all of the answers as it loops
?

tomws
07-17-2009, 01:59 PM
That's not what I meant, but you could do something like that. Either call the function on each pass of the loop, or paste just the entire logic of the function up in there. I'm assuming you're wanting to use this code to replace the manual entry of credit hours, yes?

sententia
07-17-2009, 06:26 PM
I actually want to convert the grades to numerical values...
say the user entered A, A, B, F that would be 4 + 4 + 3 + 0.
I would like to convert the grades as the loop goes... so that they add up at the end. I will then take this value and the credit hours to find the users GPA.

I have the credit hours figured out... the only issue I am having now is getting the grade values to convert as the loop goes.

Basically I think I need to get this value to represent the values of the grade aray.

var testvalue = "";
var actnumber = 0;

//call function with values it needs to convert into a number

actnumber = convertGrades(testvalue);


Here is the code again

Thanks again for all your help



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0.1//EN">
<html>
<head>
<title>GPA CALCULATOR</title>
<link>
<style type="text/css">
</style>
<script language="JavaScript">
do { //beginning of do/while loop to make sure user enters their name
var userName = prompt("Please enter your name:", "")
}

while (!userName) //end of do/while loop to check if ans is blank

<!--
var courseCtr;
var courseAnswer;
var numCourses;

// set up one dimensional array
var courses = new Array();
courseCtr = 0;

var creditHours;
creditHours = 0;

do
{
// start columns in second dimension of the array
courses[courseCtr] = new Array();

// get values from user and put in array
courses[courseCtr][0] = prompt ("Please enter the name of the course.");
courses[courseCtr][1] = prompt ("Please enter the grade you earned.");
courses[courseCtr][2] = prompt ("Please enter the credit hours you earned.");

// accumulate the value of the letter grades




// accumulate the hours of credit earned
creditHours = creditHours + parseFloat(courses[courseCtr][2]);

// accumulate the quality points earned
//gradeValues = gradeValues + parseFloat(courses[courseCtr][1]);




// add one to the total number of courses
courseCtr++;
courseAnswer = confirm ("Would You like to add another course?");
} while (courseAnswer == true);

</script>




</head>

<body align="center">
<h2 align="center">
<script language="JavaScript" type="text/javascript">
document.write(userName +"'s Grade Point Average")
</script>
</h2>

<script language="JavaScript">

// set variable that is used as counter
numCourses = 0;

// print out header for content
document.write("<table bgcolor='yellow' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<tr>");
document.write("<th>Course Name</th><th align='center'>Grade</th><th align='center'>Credit Hours</th>");
document.write("</tr>");

// Loop through array displaying html and javascript values in the array
while (numCourses < courseCtr)
{
document.write("<tr>");
document.write("<td>");
document.write (courses[numCourses][0]);
document.write("</td>");
document.write("<td align='center'>");
document.write (courses[numCourses][1]);
document.write("</td>");
document.write("<td align='center'>");
document.write (courses[numCourses][2]);
document.write("</td>");
document.write("</tr>");

// increment the counter
numCourses++;
}



// finish the table of data and display the total value
document.write("</Table>");
document.write("<br>");
document.write("<table bgcolor='yellow' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<tr>");
document.write("<td>Total value</td><td align='center'>" + creditHours + "</td>");
document.write("</tr>");
document.write("</Table>");


function convertGrades(numeral)
{
//use numeral sent to function in translation to number
var numbervalue = 0;
switch (numeral)
{
case "A":
numbervalue = 4;
break;
case "B":
numbervalue = 3;
break;
case "C":
numbervalue = 2;
break;
case "D":
numbervalue = 1;
break;
case "F":
numbervalue = 0;
break
default:
alert("Pleaes enter a valid grade value (A-F)")
}
return numbervalue;
}

var testvalue = "";
var actnumber = 0;

//call function with values it needs to convert into a number

actnumber = convertGrades(testvalue);


//-->
</script>

<script language="JavaScript">
document.write(creditHours);
</script>

<br><br>

<script language="JavaScript">

// use the returned value
document.write(actnumber);



</script>


</body>
</html>

tomws
07-17-2009, 06:36 PM
You're on the right track. Just call the function up in the loop as you take the input. You even have a comment telling you where to place it: // accumulate the value of the letter grades
. Catch the return value and add it to the cumulative total.

sententia
07-17-2009, 07:30 PM
Thanks I now have the function working when I input... but as it loops through it is only writing the last grade's value that I put in.


I am using a parseFloat to add up the credit hours... should I use something similar when i try to add up the grade values:


function convertGrades(numeral)
{
//use numeral sent to function in translation to number
var numbervalue = 0;
switch (numeral)
{
case "A":
numbervalue = 4;
break;
case "B":
numbervalue = 3;
break;John

case "C":
numbervalue = 2;
break;
case "D":
numbervalue = 1;
break;
case "F":
numbervalue = 0;
break
//default:
//alert("Pleaes enter a valid grade value (A-F)")
}
return numbervalue;
}

testvalue = (courses[courseCtr][1]);

//call function with values it needs to convert into a number

actnumber = convertGrades(testvalue);



// accumulate the hours of credit earned
creditHours = creditHours + parseFloat(courses[courseCtr][2]);


should I something like:


creditHours = creditHours + parseFloat(courses[courseCtr][2]);

here


testvalue = (courses[courseCtr][1]);

to get the grade values to add up?

tomws
07-17-2009, 07:37 PM
You don't want to do anything with the testvalue line at the bottom. That's not where the calculation should be happening if you're adding up as you take input. Furthermore, if you're adding as you go along, then you should probably also calc the creditHours total as you go along. Use the return value from your function call to do the multiplication and addition.

sententia
07-17-2009, 08:58 PM
creditHours are already totalling up as I go...

I guess my main issue is to get the grades converting to numbers as they go, since testvalue should not be used.

how can i get the values from:


courses[courseCtr][1] = prompt ("Please enter the grade you earned.");


to use this function as it loops



// accumulate the value of the letter grades
function convertGrades(numeral)
{
//use numeral sent to function in translation to number
var numbervalue = 0;
switch (numeral)
{
case "A":
numbervalue = 4;
break;
case "B":
numbervalue = 3;
break;John

case "C":
numbervalue = 2;
break;
case "D":
numbervalue = 1;
break;
case "F":
numbervalue = 0;l

break
//default:
//alert("Pleaes enter a valid grade value (A-F)")
}
return numbervalue;
}

tomws
07-17-2009, 10:35 PM
how can i get the values from:

courses[courseCtr][1] = prompt ("Please enter the grade you earned.");
to use this function as it loops


You just call the function (convertGrades) and pass the argument you want converted. Quite easy. Just like you've done at the bottom with the test value.

BTW, in the code you just posted, I notice you have the comment "// accumulate the value of the letter grades" placed as if it might be a description of the function. That's not what it does, though, since it's just a converter.

sententia
07-17-2009, 11:27 PM
something like this


actnumber = (convertGrades(courses[courseCtr][1]));

where do place it though...
i have it below the convertGrades function and it is still only returning the most recent prompt entry from the array

tomws
07-17-2009, 11:30 PM
Yes, that's what it will do... unless you accumulate the numbers as the input loop progresses. Add the previous total just like you're doing on the creditHours variable.

actnumber = actnumber + (convertGrades(courses[courseCtr][1]));

sententia
07-17-2009, 11:58 PM
worked like a charm... sorry for the ineptitude on my part, but I really do appreciate the help. this is my first experience with javascript, and also my first experience asking for help ina a forum for an issue like this.

I am very pleasantly at how selfless and willing to help people are, I guess in the end it is all about the internet working at its optimal level though.

thank you so much for the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum