I can’t get the algorithm to ask for a certain range of values

Asked

Viewed 76 times

1

Guys, in this algorithm, he asks to be typed 4 notes that are between 0 and 10, disregard the lowest and make the average of the left, adding and dividing by 4.

I can do the average, but I couldn’t get it to be written down from 1 to 10...

If anyone can help! Thank you!!!

Make an algorithm to calculate the average of 4 notes from 0 to 10. However, disregarding the lowest note.

i=1;
menor = 9999999;
total = 0;

while(i<=4){

    var num = Number(prompt(`Digite a nota ${i} do aluno: `));
    i++;

    if(num < menor){
        menor = num;
    }

    total = total + num;

    media = (total-menor)/4;
}
alert(`A menor nota digitada foi: ${menor}`)
alert(`A media do aluno foi: ${media}`);
  • You need to create a specific function to capture the student’s grade. This way you can make a loop inside the other. do { num = prompt('...') } while (num > 10)

  • But if the student gets 10, 10, 10, 10 for this method he gets an average of 7.5 . Are you sure to divide by four?

3 answers

1


Dexter, there are several ways to do this, a very simple way, is to create a condition that stays in the loop and doesn’t increase its control variable:

i = 1;
menor = 9999999;
total = 0;

while(i<=4){

    var num = Number(prompt(`Digite a nota ${i} do aluno: `));

    //Valida a nota digitada, em caso de nota inválida,
    //não incrementa a variável i e retorna para o começo do loop (continue)
    if (num < 0 || num > 10) {
        continue;
    }

    i++;

    if(num < menor){
        menor = num;
    }

    total = total + num;

    media = (total-menor)/4;
}

alert(`A menor nota digitada foi: ${menor}`);
alert(`A media do aluno foi: ${media}`);

Following the idea of Valdeir Psr, you can create a function that takes the note and already validates it, keeping a loop while the note is invalid, also recommend this form, because the code is more organized:

//Função que solicita a nota e mantem o loop caso a nota seja inválida
function validaNotaDigitada(i) {
    let num = 0;

    do {
        num = prompt(`Digite a nota ${i} do aluno: `);
    } while (num < 0 || num > 10);

    return num;
}

i = 1;
menor = 9999999;
total = 0;

while(i<=4){

    var num = validaNotaDigitada(i);

    i++;

    if(num < menor){
        menor = num;
    }

    total = total + num;

    media = (total-menor)/4;
}

alert(`A menor nota digitada foi: ${menor}`);
alert(`A media do aluno foi: ${media}`);

Obs.: You mentioned that the lowest grade is 0 and also 1... So I don’t know what the lowest grade really is, the code I left as an example validates the lowest grade as 0 (zero).

  • Missed the Number of prompt within the function

  • Oops, true, I sent the note as parameter to the function, thank you very much Isac! =)

0

I would accumulate the notes and treat it out of the way:

var notas = [], media = 0, i = 1, total = 0, qte = 4;
while(i<=qte){

    var num = Number(prompt(`Digite a nota ${i} do aluno: `));

    //Valida a nota digitada, em caso de nota inválida,
    //não incrementa a variável i e retorna para o começo do loop (continue)
    //verifica se é positivo e se vai até 10.
    if (Math.sign(num) !== -1 && num <= 10) {
        notas.push(num);
          i++;
        continue;

    } else {
        num = Number(prompt(`Digite a nota ${i} do aluno entre 0-10: `));
    }
}
var menor = Math.min(...notas);

var media = notas.reduce(function(anterior, atual) {
  return anterior + atual;
}) / qte;
var media_menor = media - menor;
alert(`A menor nota digitada foi: ${menor}`);
alert(`A média do aluno foi: ${media}`)
alert(`A nota final do aluno é: ${media_menor}`)

0

You can sort out what is data collection and what is processing. In the question code this was a bit confusing.

At first, the algorithm collects the four notes and stores them in the array notas using the method Array.push().

Then start processing by identifying the smallest value with Math.min using notas as Rest parameter.

To find the total of the notes I applied the method Array.reduce().

//Definição de uma estrutura para os dados
var notas = [];

//coleta de dados
for (i = 1; i <= 4; i++) {
  notas.push(Number(prompt(`Digite a nota ${i} do aluno: `)));
}

//Processamento dos dados
menor = Math.min(...notas);
total = notas.reduce((acc, val) => acc + val);

media = (total - menor) / 4; // Tenho minhas dúvidas, eu dividiria por três

minhaMedia = (total - menor) / 3; //Mantive o enunciado e  criei minha média.

//Exibição dos dados processados
console.log(`A menor nota digitada foi: ${menor}`)
console.log(`A media do aluno foi: ${media}`);
console.log(`A media que gostaria de aplicar foi: ${minhaMedia }`);

Browser other questions tagged

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