0

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 {}?

5
  • Well she will have to be more specific about where she is stuck. You won't find much help here if you simply link to the homework document. Commented Jul 11, 2010 at 7:07
  • Huh, what an awfully vague assignment. Did they mention how they want you to get input from the user? Commented Jul 11, 2010 at 7:07
  • maybe it's just a general assignment for CS, but using Javascript as the language, Javascript is getting very big as a General Programming language. Commented Jul 11, 2010 at 7:10
  • @Faisal Actually, it's not vague in that matter. It's just plain unrealistic. It states to use a JavaScript prompt. =/ That said, it is a homework assignment. Doing it the "proper" way may be too advanced for this point in the class. Commented Jul 11, 2010 at 7:14
  • Sheesh, I hope i don't get downvoted. I'm really trying my best here. Well, can someone just simply give her an idea of how to implement a link list and then receive inputs which then convert the grades into numbers (switch case) Commented Jul 11, 2010 at 7:20

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

3 Comments

No offense, seeing as this is a homework question, doing it for her is bad form.
You practically fed her haha. but yeah, it was completely ambiguous and I'd have just given her the answer
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)
0

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.

1 Comment

@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.