Weighted average with final examination

Asked

Viewed 177 times

3

I need 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 "Approved Student" message. If the calculated average is less than 5.0, print the "Failed Student" message. If the calculated average is between 5.0 and 6.9, inclusive, the program must print the message "Student in exam.".

In case the student is in exam, read a value corresponding to the exam grade obtained by the student. Then print the "Exam Note: " message accompanied by the typed note. Recalculate the average (add the exam score to the previously calculated average and divide by 2). and print the message "Approved student." (if the final average is 5.0 or more ) or "Student failed." (if the average has been 4.9 or less). For these two cases (approved or failed after taking the exam) present in the last line a message "Final media: " 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

That’s 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 approved or flunked, the output is being that:

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

Ideally, this is the way out:

Media: 7.3
Aluno aprovado.
  • 1

    From what I understand are not all students who take the exam but only those who were with average >= 5 and < 7. Only these students will take the exam and will have the calculation of the final average.

  • Just change where you are let input = prompt("Entre com as notas") puts let input = require("fs").readFileSync("stdin", "utf8");.

  • I have to reverse your edit because the Stack Overflow is not a Forum, see in What is the Stack Overflow we are a questions and answers site. Our format does not accept a change of focus in the question once it has already obtained answers, it invalidates the answers and the votes. If you have a new question or ask the author of the reply in the comments or open new question.

1 answer

4

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 is 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 being said, the logic is wrong because the statement is not followed. If following it already gives a better idea of what to do. The exam grade should not even be requested if the student is not in exam, because that grade does not exist. Programming is understanding the problem, is solving a real problem and not something fictional that does not exist in practice.

How do you read the data only if it’s necessary? You put it within the condition that you need. Then, of course, the comparison will determine whether the final result will also be conditioned. Just interpret the statement 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 in the Github for future reference.

The let does not work in any browser.

You can organize it better, and you can validate all the entries, but you solve it.

  • Thank you so much for the clarifications, they were very helpful! I’m editing my question, I hope you can help me again.

  • 1

    Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

Browser other questions tagged

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