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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with arrays and functions

    Hi, new to this forum but have a quick question about arrays, I've been working on this all night, and I'm at a loss. I'm trying to calculate the gpa of a user by using arrays and functions. Everything seems to be working besides two things.
    1. The array that contains the class names (className[]) always includes an extra two variables: my sentinel value which is finish, and "undefined"
    2. The arrays at the end won't produce a number when multiplied together. creditData[] and qualityPoints[].


    Also, if you have any suggestions on my code, I'd appreciate it.



    <script type="text/javascript">
    <!--
    /****************************************************************
    ** PrintExplanation - prints a message containing instructions to the user without
    ** any variable needed.
    ** Inputs:None
    ** Outputs:None
    ****************************************************************/
    function PrintExplanation()
    {
    alert("This program will calculate your Grade Point Average for any given semester. Please enter only graded classes, no Pass/Fail Classes.");
    }




    /****************************************************************
    ** GetValidNumberInput - Error checks a number between two constraints.
    ** Inputs: promptString - allows the user to insert a string in order to be asked.
    ** lowerNum - the lower constraint you want to be compared.
    ** upperNum - the higher constraint you want to be compared.
    ** Outputs: numCredits
    ****************************************************************/

    function GetValidNumberInput(promptString, lowerNum, upperNum)
    {
    var promptString;
    var lowerNum;
    var upperNum;
    var numCredits;
    numCredits=prompt(promptString);
    while(numCredits<lowerNum || numCredits>upperNum)
    {
    numCredits=prompt(promptString);
    }
    numCredits=parseFloat(numCredits)
    return numCredits
    }


    /****************************************************************
    ** CalculateGPA - Calculates the users GPA
    ** Inputs: totalCredits: The total amount of credits determined by the user.
    ** totalQualityPoints: The total amount of quality points determined by the user.
    ** Outputs: gpa - the users final grade point average.
    ****************************************************************/
    function CalculateGPA(totalCredits, totalQualityPoints)
    {
    var totalCredits;
    var totalQualityPoints;
    var gpa;

    gpa=totalQualityPoints/totalCredits
    return gpa;
    }


    function GenerateGPAReport(totalCredits, totalQualityPoints)
    {

    }


    /****************************************************************
    ** ConvertGradeToPoints - Converts a letter given by the user into a number corresponding to grade points.
    ** Inputs: grade - the letter of the grade the student recieved in the course.
    ** Outputs: points - the number value of the grade that was input by the user.
    ****************************************************************/

    function ConvertGradeToPoints(grade)
    {
    var grade;
    var points;
    if(grade == "A" || grade== "a")
    {
    points=4
    }
    else if(grade == "B" || grade == "b")
    {
    points=3
    }
    else if(grade == "C" || grade == "c")
    {
    points=2
    }
    else if(grade == "D" || grade == "d")
    {
    points=1
    }
    else if(grade == "F" || grade == "f")
    {
    points=0
    }
    points=parseFloat(points);
    return points;
    }

    //-->
    </script>
    </head>

    <body>
    <script type="text/javascript">


    var promptString; /*Declares Variables*/
    var className;
    var numOfClasses;
    var grade;
    var semester;
    var numCredits;
    var lowerNum=1;
    var upperNum=4;
    var class1;
    var i=0;
    var counter=0;
    var totalCredits=0;
    var totalPoints=0;
    var grade;
    var counterCredits=0;
    var counterGrade=0;
    var points;
    var gpa;
    var qualityPoints;
    var creditData;
    var gradeData;
    var x=0;
    var totalQualityPoints=0;


    PrintExplanation();
    var className = new Array(i);
    semester=prompt("Please enter the semester you would like to calculate your GPA for.");
    document.write("Grade report for " + semester + "<br />");


    while(class1!="finish") /*Stores the names of the classes taken into an array*/
    {
    class1=prompt("Enter the Class name, or finish to end");
    className[i]=class1
    i++
    }


    while(counter<=i) /*writes the classes in the array from top to bottom*/
    {
    document.write(className[counter] + "<br />");
    counter++
    }

    var creditData = new Array(i)
    while(counterCredits<=i) /*counts the total number of credits for all the classes*/
    {
    numCredits=GetValidNumberInput("How many credits is " + className[counterCredits] + " class worth?",1,4)
    creditData[counterCredits]=numCredits
    totalCredits=totalCredits + numCredits
    counterCredits++
    totalCredits=parseFloat(totalCredits);
    }

    var gradeData = new Array(i) /*collects information about grades in an array*/
    while(counterGrade<i=)
    {
    points=prompt("What grade did you recieve in " + className[counterGrade] + "?");
    points=ConvertGradeToPoints(points)
    gradeData[counterCredits]=points
    totalPoints=totalPoints+points
    counterGrade++
    }



    while(x<=i)
    {
    qualityPoints=creditData[x]*gradeData[x]
    totalQualityPoints=totalQualityPoints + qualityPoints
    x++
    }

    gpa=CalculateGPA(totalCredits, qualityPoints)
    document.write(gpa);










    </script>
    </body>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    It cannot work as you have a syntax error:-
    while(counterGrade<i= )

    The default of all your promts is undefined.
    semester=prompt("Please enter the semester you would like to calculate your GPA for." , "" );

    When you declare a variable with var you wipe out any previous instance of it:-
    function ConvertGradeToPoints(grade) {
    var grade;

    A prompt dialog can only return a single value., that is the value entered by the user.
    numCredits=GetValidNumberInput("How many credits is " + className[counterCredits] + " class worth?",1,4)
    Here you have assigned "1" as the default value. The correct way is something like:-
    numCredits= prompt("How many credits is " + className[counterCredits] + " class worth?","");
    You must check on the validity of that value in script:-
    checkValidInput(numCredits); // function checkValidInputs checks for a number within a valid range.

    You have declared your array with a length of i, which has a value of 0.
    var className = new Array(i);

    There are probably other errors as well.

    Other comments:
    You have omitted the ; at the line ends in many places. It is true that the ; is not mandatory, but if you make a habit of leaving it out you will run into trouble one day.
    You should place the opening brace { on the same line as the if/else/while/do/function statement, not the next line. Again, you will run into trouble if you do not follow this advice (we had a case only the other day in this forum).

    Simplify your grades>points calculation:-

    Code:
    grade = grade.toLowerCase();
    var gradeslist = ["a","b","c","d","e"]
    var pointslist = [4,3,2,1,0]
    for (var i =0; i<gradeslist.length; i++) {
    if (gradeslist[i] == grade) {
    points = pointslist[i];
    }
    }
    Fix these errors/infelicities, try inserting some alerts at strategic positions to check on the values of your variables. If it still does not work then come back. But please see Forum Rule 1.5 re homework.

    BTW, when posting here please follow the posting guidelines and wrap your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the tips. I appreciate the help, I'm not a very experienced coder, and I know my code is not up to par as far as quality goes. Thanks for the help though.


  •  

    Posting Permissions

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