Average between 3 notes

Asked

Viewed 17,399 times

3

I need the user to enter 3 notes to calculate the average, and tell if he is approved, failed, or recovery. I believe the error is in the average itself.

<html> 
<head> 
<title>  </title>


<script type="text/javascript">

var media, n1, n2, n3 ;

n1= prompt ("Informe a primeira nota: ") ;
n1= eval (n1) ;

n2= prompt ("Informe a segunda nota: ") ;
n2= eval (n2) ; 

n3= prompt ("Informe a terceira nota: ") ; 
n3= eval (n3) ;

media = (n1 + n2 + n3)/3 ; 

if ( media >= 6 ) {
document.write ("Aprovado");

} else {

if ( media =< 5 ) {
document.write ("Reprovado");

} else {

document.write ("Recuperação");
}}

</script> 
</head> 
</html> 

4 answers

7

Your code has a signal that’s reversed in comparison:

Change:

if ( media =< 5 ) // wrong

for:

if ( media <= 5 )

Observing: the signs of > (greater) and < (minor) are always before the = (equal).

var media, n1, n2, n3 ;

n1= prompt ("Informe a primeira nota: ") ;
n1= eval (n1) ;

n2= prompt ("Informe a segunda nota: ") ;
n2= eval (n2) ; 

n3= prompt ("Informe a terceira nota: ") ; 
n3= eval (n3) ;

media = (n1 + n2 + n3)/3 ; 

if ( media >= 6 ) 
{
    console.log ("Aprovado");

} 
else 
{ 
    if ( media <= 5 ) 
    {
       console.log("Reprovado");
    }
    else 
    {
       console.log ("Recuperação");
    }
}

Give us a read on comparison operators.

6

Refining the Maniero code:

var ordinais = ['primeira', 'segunda', 'terceira'];
var media=0;

for (var i = 0; i < ordinais.length; i++) {
    var nota = prompt("Informe a " + ordinais[i] + " nota: ");
    if (nota === "" || !nota) nota = 0;
    media += parseFloat(nota,2);
}

media /= ordinais.length;

if (media >= 6) {
    document.write("Aprovado");
} else if (media <= 5) {
    document.write("Reprovado");
} else {
    document.write("Recuperação");
}

5

There is a syntax error using the comparison operator <= (smaller or equal). Following the line of your previous question:

var idade = [];
for (var i = 0; i < 3; i++) idade[i] = parseInt(prompt("Informe a primeira idade: "));
var media = 0;
for (var i = 0; i < 3; i++) media += idade[i];
media /= 3;
if (media >= 6) document.write("Aprovado");
else if (media <= 5) document.write("Reprovado");
else document.write("Recuperação");

I put in the Github for future reference.

I did not validate if the data was typed wrong.

-2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
   var media
   var n1
   var n2
   var n3

   n1 = prompt('informe primeira nota')
   parseFloat(n1)

   n2 = prompt('informe segunda nota')
   parseFloat(n2)

  n3 = prompt('informe terceira nota')
  parseFloat(n3)
  
    media = (n1 + n2 + n3)/3

    if (media >=6){
        alert('aprovado')
    } else {
        if (media <=5)
       alert('reptovado')   
    }

Browser other questions tagged

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