Andrew Makovii
Andrew Makovii

Reputation: 3

Object (name, surname, index number, table with grades) with a method that calculates the grade average

So I've been trying to solve this university task. I have an approximate understanding how to solve this, but I only came up with the solutions that either don't work or don't really correspond to the task requirements.

The task looks like this.

Create an object using Object initializer, assign properties that describe you (name, surname, index number, table with grades). Assign a method to the object that calculates the grade average

This is the solution that works but isn't exactly what supposed to be done.

      function Grade (math, science, programming, management){
    this.math = math;
    this.science = science;
    this.programming = programming;
    this.management = management;

    return (math + science + programming + management)/arguments.length;
}

    

const student = {
        name: 'John',
        surname: 'Smith', 
        index: 'x4sx4sd',
        grade: Grade(5,4,5,5)
    }

console.log(student);

This is my try that doesn't work but still in my opinion closer to the truth but yet without the average part

const student = {
        name: 'John',
        surname: 'Smith', 
        index: 'xxxxx',
        grade: 
        {
            math: 5,
            science: 5,
            language: 5,
            marketing: 5,
            management: 5

        },
        gradeAvg: function(grade){
            grade=this.grade;
            let total = 0;
            for (let value in object){
                total += grade[value]
            }
            return total;  
        }
    
    }

console.log(student);

I understand that it's a nooby question, I've tried to google as hard as I could but could find anything, any help would be appreciated.

Upvotes: 0

Views: 73

Answers (2)

Samuel Olekšák
Samuel Olekšák

Reputation: 388

So first of all you need to convert the grade object from Object to array so you can sum all the elements. Object.values does that, so {a: 1, b: 2} would become [1, 2]. Next you need to get the number of grades so you can then divide the sum to get the average. Last thing you need is a sum of the array we have created previously, reduce array method is used to reduce array into one value.

        get GradeAvg() {
            const gradesArray = Object.values(this.grade);
            const gradesCount = gradesArray.length;
            const gradesSum = gradesArray.reduce((sum, current) => {
                sum += current;
                return sum;
            })

            return gradesSum / gradesCount;
        }

Upvotes: 0

Kinglish
Kinglish

Reputation: 23664

Change your gradeAvg into a getter function which will execute when you load in the object. Also, to get the average (whch requires a count) you can access the count using Object.entries (which converts the object into an array) and then get the length

const student = {
  name: 'John',
  surname: 'Smith',
  index: 'xxxxx',
  grade: {
    math: 2,
    science: 5,
    language: 5,
    marketing: 1,
    management: 5
  },
  get gradeAvg() {
    let total = 0;
    for (let value in this.grade) {
      total += this.grade[value]
    }
    return total / Object.entries(this.grade).length;
  }
}

console.log(student);

Upvotes: 1

Related Questions