Calculate average between 3 notes

4

I need the user to enter 3 notes to calculate the media and report whether it is approved, disapproved, or recovered. 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> 
    
asked by anonymous 16.10.2016 / 00:41

3 answers

4

Your code has a sign reversed in the comparison:

Switch:

  

if (media =< 5) // wrong

by:

if ( media <= 5 )

Note: signs of > (major) and < (minor) are always before = strong .

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");
    }
}

Read the comparison operators .

    
16.10.2016 / 01:16
3

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

var idade = [];
for (var i = 0; i < 3; i++) {
    idade[i] = 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");
}
    
16.10.2016 / 01:26
2

Refining the Bigown 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");
}
    
16.10.2016 / 13:11