Weighted average with final exam

I need to read 4 Notes with weights of 2, 3, 4 and 1, respectively, for each of these notes and I must show this average accompanied by the message " Media:". if this average is greater than or equal to 7.0, print the message "student approved.". If the calculated average is less than 5.0, print the message "student failed.". If the calculated average is a value between 5.0 and 6.9, including these, the program should print the message "student in examination.".

If the student is in the exam, read a value corresponding to the exam grade obtained by the student. Then print the message "exam grade:" accompanied by the typed grade. recalculate the average (add the exam score to the previously calculated average and divide by 2). and print the message " student approved."(if the final average is 5.0 or more ) or " failed student.", (if the average was 4.9 or less) . For these two cases (passed or failed after have taken exam) present in the last line a message "final average:" followed by the final average for this student.

Entradas: 2.0, 4.0, 7.5, 8.0 

3.4 (Nota do Exame) 
Saída:
Media: 5.4
Aluno em exame.
Nota do exame: 6.4
Aluno aprovado.
Media final: 5.9

This is my code so far:

let data = input.split(' ')
let exame = input.split('\n')


let N1 = parseFloat(data.shift());
let N2 = parseFloat(data.shift());
let N3 = parseFloat(data.shift());
let N4 = parseFloat(data.shift());
let notaExame = parseFloat(exame.shift())


let media = ((N1*2)+(N2*3)+(N3*4)+(N4*1))/10;
let mediaFinal = (parseFloat(exame) + parseFloat(media))/2

console.log("Media: " + media.toFixed(1))

if (media >= 7.0){
    console.log('Aluno aprovado.')
}else if (media.toFixed(1) >=5.0 && media.toFixed(1) <= 6.9){
    console.log('Aluno em exame.')
    console.log("Nota do exame: " + exame)
} else {
    console.log('Aluno Reprovado')
}
if (mediaFinal <= 4.9){
    console.log('Aluno reprovado.')
    console.log('Media final: ' + mediaFinal.toFixed(1))
} else {
    console.log('Aluno aprovado.')
    console.log('Media final: ' + mediaFinal.toFixed(1))
}

My main problem is that when the result is higher than 5.0 or 7.0, where the student is automatically passed or failed, the output is this:

Media: 7.3
Aluno aprovado.
Aluno aprovado.
Media final: 5.3

Ideally, the output is this:

Media: 7.3
Aluno aprovado.
Author: Augusto Vasques, 2020-11-30

1 answers

The first thing I want to say is that people are learning to ask for data that needs to be entered in a specific way and that's a plague. It is already complicated to type right more freely, having to write formatted is even worse. And people don't validate if they actually typed something valid. And they learn wrong that data entry is always beautiful and perfect.

That said, the logic is wrong because the utterance is not followed. If you follow it already gives you a better idea than you should. The exam grade should not even be asked if the student is not on the exam, because this grade does not exist. Programming is to understand the problem, it is to solve a real problem and not something fictional that does not exist in practice.

How to read the data only if necessary? It puts it within the conditional that states it needs to. Then obviously the comparison is that will determine if the final result will also be conditioned. Just interpret the utterance as it is written (when it is right, which seems to be the case).

let input = prompt("Entre com as notas")
let data = input.split(' ')
let N1 = parseFloat(data.shift());
let N2 = parseFloat(data.shift());
let N3 = parseFloat(data.shift());
let N4 = parseFloat(data.shift());
let media = (N1 * 2 + N2 * 3 + N3 * 4 + N4 * 1) / 10;
console.log("Media: " + media.toFixed(1))
if (media >= 7.0) {
    console.log('Aluno aprovado.')
}else if (media.toFixed(1) >=5.0 && media.toFixed(1) <= 6.9){
    console.log('Aluno em exame.')
    let notaExame = prompt("Entre com a nota do exame");
    let mediaFinal = (parseFloat(notaExame) + parseFloat(media))/2
    if (mediaFinal <= 4.9){
        console.log('Aluno reprovado.')
        console.log('Media final: ' + mediaFinal.toFixed(1))
    } else {
        console.log('Aluno aprovado.')
        console.log('Media final: ' + mediaFinal.toFixed(1))
    }
    console.log("Nota do exame: " + notaExame)
} else {
    console.log('Aluno Reprovado')
}

I put on GitHub for future reference.

let does not work in any browser.

Can organize better, and can validate all entries, but so solves.

 4
Author: Maniero, 2020-12-01 12:45:40