views:

75

answers:

2

I have a friend who has an assignment on arrays and because I lack experience in Javascript and she just needs some real quick help understanding how to implement a Javascript loop to store data in an array which converts a letter grade to a number. Can someone just guide her in a general direction?

https://docs.google.com/fileview?id=16uNNiooLalkm1QlszrqEPr2qqMGLjhrtQx7qCLw-7d2ftygre8GM6hyceJHj&hl=en\

Update: She states that she doesn't understand how to make it prompt again after the first time while storing data. Can someone just write a translation for a C++ code for do {}?

+1  A: 

Here's a more or less complete solution - but it doesn't output the results to the HTML page but outputs it with the alert boxes.

var done = false,
    classes = [],
    total_credits = 0,
    avg = 0;

while(!done){
    var class_name   = prompt("Enter class name"),
        letter_grade = prompt("Enter letter grade for "+class_name),
        credit_hours = prompt("Enter credit hours for "+class_name),
        number_grade = {"A":4,"B":3,"C":2,"D":1,"F":0}[letter_grade];
    if(class_name && letter_grade && credit_hours){
        classes.push({
           class_name: class_name,
           letter_grade: letter_grade,
           number_grade: number_grade,
           credit_hours: credit_hours
        });
        total_credits += parseInt(credit_hours,10);
        avg += number_grade*credit_hours;
    }else
        done = true;
}

avg = avg/total_credits;

for(var i=0; i<classes.length; i++){
    alert(classes[i].class_name + " | " +
          classes[i].letter_grade + " | " +
          classes[i].credit_hours);
}

alert("Total credits: " + total_credits);
alert("GPA: " + avg.toFixed(2));
Andris
No offense, seeing as this is a homework question, doing it for her is bad form.
George Marian
You practically fed her haha. but yeah, it was completely ambiguous and I'd have just given her the answer
danutenshu
I know doing other people's homework is not a good thing - that's why I didn't comment the code and used some structures that might not be easy to explain if you don't know what you are talking about (the assignment requires explaining the work, or at least that's what I understood from it)
Andris
A: 

Basically, she should use a while loop.

in (mostly) pseudocode:

more_entries = true;

while(more_entries)
{    
    response = prompt("Question for the user","");

    if (response == null)
    {
        more_entries = false;
    }
    else
    {
        // store value somewhere
    }    
}

Of course, this needs to be expanded to multiple prompts.

George Marian
thanks for putting up with this
danutenshu
@danutenshu You and your friend are quite welcome. I appreciate the fact that you're upfront about it being homework and didn't ask for the answer, but rather guidance.
George Marian