Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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);

about 4 years ago · Juan Pablo Isaza Relatório

0

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;
        }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda