Javascript - Calculation of school grades with object use, is it possible?

Asked

Viewed 228 times

0

My intention is to make the famous algorithm of school grades to calculate the average student, which is usually done with arrays, but I want to know if it is also possible with use of objects, my attempt was this, but I’m getting Undefined as a result:

var alunos = []
var media = 0;
var soma = 0;

for (let i = 0; i <= 3; i++) {
    const aluno = {
        name: prompt('NOME: '),
        nota1: Number(prompt('NOTA 1:')),
        nota2: Number(prompt('NOTA 2:')),
        nota3: Number(prompt('NOTA 3:')),
        nota4: Number(prompt('NOTA 4:'))

    }
    alunos.push(aluno);
    soma += aluno.nota1.nota2.nota3.nota4
    media = soma / alunos.length;
    console.log(media);

    // zerando os dados para realizar o cálculo dos próximos alunos
    media = 0;
    soma = 0;
}

If the question is not clear, the objective of the algorithm is to receive the student’s name, his 4 grades and after that calculate his average.

  • Take a look at object breakdown, it will help a lot.

1 answer

2


You can use a function reduce allied to the destruction of the object:

const alunos = []

const calcular = ({ nota1, nota2, nota3, nota4 }) => [nota1, nota2, nota3, nota4].reduce((total, nota) => total + nota) / 4;

for (let i = 0; i <= 3; i++) {
  const aluno = {
    nome: prompt('NOME: '),
    nota1: Number(prompt('NOTA 1:')),
    nota2: Number(prompt('NOTA 2:')),
    nota3: Number(prompt('NOTA 3:')),
    nota4: Number(prompt('NOTA 4:')),
  };

  alunos.push(aluno);
  console.log(`${aluno.nome} ${calcular(aluno)}`);
}


reduce

The method reduce() performs a function reducer (provided by you) for each member of the array, resulting in a single return value.

Example:

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15


Assignment via structuring (destructuring assignment).

The syntax of assignment via structuring (destructuring assignment) is a Javascript expression that allows you to extract data from arrays or objects in different variables.

var a, b, rest;
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2

[a, b, ...rest] = [1, 2, 3, 4, 5];
console.log(a); // 1
console.log(b); // 2
console.log(rest); // [3, 4, 5]

({a, b} = {a:1, b:2});
console.log(a); // 1
console.log(b); // 2
  • Very confusing, but very interesting.

  • @Diegooliveira actually your code can be improved to simplify understanding. I just adapted your code to accept the average. I think in your case it would be better to have a function to fill the data, one to calculate the average and in reality I would not even use the notes as attributes, I would use an attribute "notes" that would be an array

  • That way I know how to do, I was only interested in knowing if it was also possible with objects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.