Nodejs - Make an algorithm that reads the height and enrollment of ten students. Show the enrollment of the highest student and the lowest student

Asked

Viewed 104 times

0

All I can do is show the height from the smallest to the largest, but I want to show you which license plates the heights belong to (undergraduate enrollment, higher student enrollment, etc)

const readlineSync = require('readline-sync');

nome = [];
height = [];
mat = [];

for (let i = 0; i <= 9; i++) {
    nome[i] = readlineSync.question('Nome do aluno: ')
    height[i] = parseFloat(readlineSync.question('Altura do aluno: '));
    mat[i] = parseFloat(readlineSync.question('Matrícula: '));
    console.log('\n');

};

for (let x = 0; x <= 9; x++) {
    console.log(`[NOME] -> ${nome[x]} -> [ALTURA] -> ${height[x]} -> [MATRÍCULA] -> ${mat[x]}`);
};

console.log(`\n[${height.sort()}] -> Do mais alto para o mais baixo`);
```


1 answer

2


First, how do you want to store multiple information from the same entity (in case a student), it is worth it to you to create a single array of objects instead of three arrays, one for each information.

Then you’ll have something like this:

const readlineSync = require('readline-sync');

const alunos = [];

for (let i = 0; i <= 9; i++) {
    // Criamos um novo objeto para o aluno.
    const aluno = {
      nome: readlineSync.question('Nome do aluno: ')
      height: parseFloat(readlineSync.question('Altura do aluno: '));
      mat: parseFloat(readlineSync.question('Matrícula: '));
    };

    // Inserimos o aluno no array de alunos:
    alunos.push(aluno);

    console.log('\n');
};

After that, to determine the students' objects with lower and higher height, one of the ways is to use a loop for together with two "external variables", to store the objects of the smallest and largest students. Something like this:

const alunos = [
  { nome: 'A', height: 4, mat: 'AAA' },
  { nome: 'B', height: 3, mat: 'BBB' },
  { nome: 'C', height: 5, mat: 'CCC' },
  { nome: 'D', height: 1, mat: 'DDD' },
  { nome: 'E', height: 2, mat: 'EEE' },
];

let maiorAluno = null;
let menorAluno = null;

for (const aluno of alunos) {
  // Caso nenhum aluno ainda tenha sido classificado como maior ou menor:
  if (!maiorAluno || !menorAluno) {
    if (!maiorAluno) maiorAluno = aluno;
    if (!menorAluno) menorAluno = aluno;
    
    continue;
  }
  
  if (aluno.height > maiorAluno.height) {
    maiorAluno = aluno;
  }
  
  if (aluno.height < menorAluno.height) {
    menorAluno = aluno;
  }
}

console.log('Maior aluno:', maiorAluno);
console.log('Menor aluno:', menorAluno);

Another option is to use sort, creating a property-based comparison function height of each object. Then capture the first and last elements. So:

const alunos = [
  { nome: 'A', height: 4, mat: 'AAA' },
  { nome: 'B', height: 3, mat: 'BBB' },
  { nome: 'C', height: 5, mat: 'CCC' },
  { nome: 'D', height: 1, mat: 'DDD' },
  { nome: 'E', height: 2, mat: 'EEE' },
];

const alunosOrdenadosPorAltura = alunos.sort((a, b) =>
  a.height - b.height // Irá ordenar do menor para o maior.
);

const maiorAluno = alunosOrdenadosPorAltura[0];
const menorAluno = alunosOrdenadosPorAltura[alunosOrdenadosPorAltura.length - 1];

console.log('Maior aluno:', maiorAluno);
console.log('Menor aluno:', menorAluno);

Browser other questions tagged

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