Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Posting new Array values based on switch cases

    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.

    Code:
    <!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>
    Last edited by sententia; 07-17-2009 at 01:04 AM.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    sententia (07-17-2009)

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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
    ?

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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?
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    sententia (07-17-2009)

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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

    Code:
    <!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>

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    sententia (07-17-2009)

  • #7
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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:

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

    Code:
    testvalue = (courses[courseCtr][1]);
    to get the grade values to add up?

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    sententia (07-17-2009)

  • #9
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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:

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

    to use this function as it loops

    Code:
    	// 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;
    }

  • #10
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by sententia View Post
    how can i get the values from:
    Code:
    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.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    sententia (07-17-2009)

  • #11
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    something like this

    Code:
    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

  • #12
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.
    Code:
    actnumber = actnumber + (convertGrades(courses[courseCtr][1]));
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    sententia (07-17-2009)

  • #13
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •