0

This is my first "program" or whatever you want to call it using JavaScript.

I need to modify the avg, total, and grade by using 2 functions: calcAVG and calcGrade. I cannot get the calcAVG function to work correctly. I can't figure out why the function won't modify the avg or total value. In the program, I can't modify the top 5 properties of the object (it's for an assignment).

Also the calcAVG function works when called in the console.log. So do I have some kind of syntax error?

var student = {
	fullName : 'Amanda Corbin',
	scores : [90,50,88,56,89],
	avg : 0,
	total : 0,
	grade : '',
	
	calcAvg : function(scores,avg,total) {
		for (var i = 0; i < this.scores.length; i++) {
			this.total += this.scores[i];
		}
		this.avg = ( this.total / this.scores.length );
		return (this.avg);
	}
	
	
	 calcGrade : function(avg,grade) {
		
		if (this.avg >=90){
			console.log("grade is A");
			this.grade = 'A';
		} else if (this.avg >= 80 && this.avg <90){
			console.log("grade is B");
			this.grade = 'B';
		} else if (this.avg >= 70 && this.avg <80){
			console.log("grade is C");
			this.grade = 'C';
		} else if (this.avg >= 60 && this.avg <70){
			console.log("grade is D");
			this.grade = 'D';
		} else if (this.avg <60){
			console.log("grade is F");
			this.grade = 'F';
		}
		
		
		return (this.grade);
	}


console.log(student);

console.log(student.calcAvg());
console.log(student.calcGrade());

3
  • 4
    1. no comma after calcAvg function 2. no closing } to close student. There's also no need for the function parameters; you're accessing the fields via this. And you can remove the parens around the values you're returning. Commented Jan 15, 2020 at 0:27
  • I cannot get the calcAVG function to work correctly - in what way is it incorrect? Commented Jan 15, 2020 at 0:33
  • @ jaromanda X I'm using notepad++ and chrome to make/view the program. When I use the console.log(student.calcAvg()); i get back 74.6 which is the correct value. I'm not sure why its not working for you. With the calcAVG function not working, the calcGrade one will not work. (i tried passing it the avg directly without the other function and had it work correctly for me). Anywhos thank you for the comment Commented Jan 15, 2020 at 1:35

3 Answers 3

1

You are missing a few commas and a closing };

var student = {
  calcAvg: function(scores, avg, total) {

  }, // missing a comma here
  calcGrade: function(avg, grade) {

  }, // missing a comma here
}; // missing a closing bracket here
Sign up to request clarification or add additional context in comments.

3 Comments

if that were the case, how can the calcAVG function works when called in the console.log
Sorry, didn't even check the functions. Maybe this with help javascript.info/object-methods
Thank you for the reply, but I still can't get it to work. I didn't even realise the missing closing bracket.
1

If you put your code on a editor you will see some missing parts. like , and }. Other thing is you don't need to use any parameters in those functions. you are not passing any parameters into those functions. there you use this keyword. that will pick the correct property of your student array.

var student = {
	fullName : 'Amanda Corbin',
	scores : [90,50,88,56,89],
	avg : 0,
	total : 0,
	grade : '',
	calcAvg : function() {
		for (var i = 0; i < this.scores.length; i++) {
			this.total += this.scores[i];
		}
		this.avg = ( this.total / this.scores.length );
		return (this.avg);
	},
	calcGrade : function() {
		
		if (this.avg >=90){
			console.log("grade is A");
			this.grade = 'A';
		} else if (this.avg >= 80 && this.avg <90){
			console.log("grade is B");
			this.grade = 'B';
		} else if (this.avg >= 70 && this.avg <80){
			console.log("grade is C");
			this.grade = 'C';
		} else if (this.avg >= 60 && this.avg <70){
			console.log("grade is D");
			this.grade = 'D';
		} else if (this.avg <60){
			console.log("grade is F");
			this.grade = 'F';
		}
		
		
		return (this.grade);
	}

}


console.log(student.calcAvg());
console.log(student.calcGrade());

Comments

0

Chris is right in the comment above.

Your final code should look like this

var student = {
    fullName : 'Amanda Corbin',
    scores : [90,50,88,56,89],
    avg : 0,
    total : 0,
    grade : '',

    calcAvg : function(scores,avg,total) {
        for (var i = 0; i < this.scores.length; i++) {
            this.total += this.scores[i];
        }
        this.avg = ( this.total / this.scores.length );
        return (this.avg);
    },


     calcGrade : function(avg,grade) {

        if (this.avg >=90){
            console.log("grade is A");
            this.grade = 'A';
        } else if (this.avg >= 80 && this.avg <90){
            console.log("grade is B");
            this.grade = 'B';
        } else if (this.avg >= 70 && this.avg <80){
            console.log("grade is C");
            this.grade = 'C';
        } else if (this.avg >= 60 && this.avg <70){
            console.log("grade is D");
            this.grade = 'D';
        } else if (this.avg <60){
            console.log("grade is F");
            this.grade = 'F';
        }


        return (this.grade);
    }
}


console.log(student);

console.log(student.calcAvg());
console.log(student.calcGrade());

Here is the output for the same on a coderpad sandbox link (Just copy-paste the above code and you can see the output.

enter image description here

6 Comments

if that were the case, how can the calcAVG function works when called in the console.log
Thank you for the reply, but I still can't get it to work. I will ask my professor tomorrow
@JaromandaX I have attached the working output in my answer above. I hope that helps.
I didn't know that you had to call each function in order to modify the avg, total, and grade. I assumed that when you call the student object, it would run the functions inside of the object. I got it to work now! thank you for taking the time to go through it.
@AmandaCorbin No worries. Happy to help! Keep experimenting and learning 👍
|

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.