Web Analytics Made Easy -
StatCounter Adding an array and converting letters to numbers - how do i? - CodingForum

Announcement

Collapse
No announcement yet.

Adding an array and converting letters to numbers - how do i?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding an array and converting letters to numbers - how do i?

    This is what I want to be able to do: I want to add the total credit hours the user inputs and display it. As you notice, it loops, but how do I add the course[Info][2] values to each other?
    Also, how do I convert the letter grade (course[Info][1]) values into numbers and use it in a function to calculate GPA?

    This is what I have so far:

    var userName = prompt("Please enter your name.");
    var course = new Array();
    var Info = 0;

    do
    {

    course[Info] = new Array();
    course[Info][0] = prompt("What is the class name?");
    course[Info][1] = prompt("What is your letter grade?");
    course[Info][2] = prompt("How many credit hours was the class worth?");

    Info++;

    var courseAdd = prompt("Do you wish to add more classes?");
    } while (courseAdd == "yes" || courseAdd == "Yes");

    var numCourse = 0;

    document.write("<H2 align='center'>" + userName + "'s GPA</H2><br>");
    document.write("<table bgcolor='yellow' align='center' border='1' cellpadding='4' width='75%'>");
    document.write("<tr>");
    document.write("<td>Course</td><td align='center'>Course Grade</td><td align='center'>Course Hours</td>");
    document.write("</tr>");

    while (numCourse < Info)
    {
    document.write("<tr>");
    document.write("<td>");
    document.write (course[numCourse][0]);
    document.write("</td>");
    document.write("<td align='center'>");
    document.write (course[numCourse][1]);
    document.write("</td>");
    document.write("<td align='center'>");
    document.write (course[numCourse][2]);
    document.write("</td>");
    document.write("</tr>");

    // increment the counter
    numCourse++;
    }

  • #2
    It looks like you know some other language, but new in javascript. Let's see, at a first glance:

    1. no need to do to fire code lines. You may insert those code lines in to a function

    function blahblah(parameters_optional){
    ..
    ..
    }

    ... and fire this function when an event occures (onload, onclick, onmouseover.... and so on)

    2. document.write is a weak function, it will not change dinamically the content. It will work, yes, in your case and if a single input, yet...

    3. while ((courseAdd == "yes") || (courseAdd == "Yes")){
    ..
    ..
    }

    or better:

    while (courseAdd.toLowerCase() == "yes"){
    ..
    ..
    }

    in fact, as you have no loop to modify the "yes" parameter, the if condition it looks proper to me:

    if (courseAdd.toLowerCase() == "yes"){
    ..
    ..
    }
    else{...}

    while will do a loop each time, decreasing the code speed...

    3. to parse a string into an integer use parseInt()

    parseInt(s,b) s=string; b=num. base (default b=10)

    It will return an number if the string begin with a number, but till first character other than a number:

    parseInt('12a9') = 12;
    parseInt('12.09') = 12;
    parseInt('a12') = NaN;
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment

    Working...
    X