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).
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)
– Valdeir Psr
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?
– Augusto Vasques